Exercise 5:23 in "Complete Python Mastery" course

Just a comment on the common interview question Mosh gave. In his solution, he used a dictionary and went through a loop and incremented each char by 1 when found. Then he reverse sorted and popped the first one.

I have a few problems with that method. What if there are two chars or more that are used at the same frequency, this misses it. What if the space or other non-letter character is the most used, would you want that reported, because they said the most used letter.

This is how I solved it:

from itertools import count


sentence = "This is a really common interview question"
x = 0
most_used = []
for y in sentence:
    if (not y.isalpha()):
        continue  # Ignore all non letter chars
    if (sentence.count(y) > x):
        x = sentence.count(y)
        most_used.clear()
        most_used.append(y)
    elif (sentence.count(y) == x) and (y not in most_used):
        most_used.append(y)

if len(most_used) == 1:
    print(
        f"The letter that is most used: '{most_used[0]}'. It was used {x} times")
else:
    print(f"The letters most used are: {most_used}. They are used {x} times")

Wow, really helpful comment. I’m taking a beginner level coding class and you come on and tell me my code smells. No kidding genius, if I was a competent programmer I wouldn’t be taking this course would I? How about you go comment on something you can bring value to instead of finding things to trash. Tell me why my points are incorrect (I don’t care about the data structure used, tell me why I am wrong that finding 2 chars that were both used the same number of times, not reporting non-alpha). In other words, address my points, don’t just insult me.

By the way, if I was the moderator I would kick you off this site. Insulting beginner coders is a good way for Mosh to lose business.

1 Like