Consider a list of subject, grade pairs. E.g.
[("Latin", "A"), ("French", "A*"), ("Math", "B"), ("Latin", "A*")]
The task is to return the same list but with each subject given at most once. Where a subject occurred more than once originally, the returned list should have the highest grade for that subject.
Using the UK system, "A*" is a better grade than "A" and of course "A" is better than "B" etc. Only "A" can be followed by an asterisk.
The output for this case should be:
[("Latin", "A*"), ("French", "A*"), ("Math", "B")]
Another example:
[("Latin", "A*"), ("French", "A*"), ("Math", "B"), ("French", "A*"), ("Math", "C")]
The output should be:
[("Latin", "A*"), ("French", "A*"), ("Math", "B")]
Your returned output list can be in any order you like.
This is code-golf, so the shortest code in bytes wins.
Languages I am hoping for:
- Python, because I wrote a terrible solution in python which inspired this question. Done.
- C. Because real men/women code in C when it makes no sense for the problem at hand. Done.
- Julia. Because it's an awesome language.
Input assumptions
You can assume there are no more than 10 subject/grade pairs, the input is printable ASCII and there are no spaces within the subject names.