Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+10 votes

Is there a way I can sort the dictionary so that it will pick the letter that comes first alphabetically if they have the same count? I know we did this in class, but made it a list instead. Can I keep it in a dictionary, or will my for loop have to go through a list instead?

asked in old_CSC201 by (1 point)

1 Answer

+2 votes

No, you can't sort a ditionary.

Looping through the keys in the dicionary, allows you to get to the value which is the count of the letter. You should be able to use our basic idea of finding a max, saving the letter along with the updated value of the max.

What if you find a second value which has the same max for its count? Then check if that letter is earlier in the alphabet. Letters are really numbers "under the hood" and therefore can be compared using < and >.

Another strategy that is less efficient loops through the dictionary more than once.

answered by (1 point)
...