5- Data Structures 23 Exercise

Hello world

I was able to answer the exercise using the following code where also Mosh presents an alternative solution using dictionaries:

sentence = "This is a common interview question"

sentence_unpack = [*sentence]

count = [(sentence_unpack.count(x), x) for x in sentence_unpack]

max(sorted(list(set(count)), reverse = True), key = lambda x: x[0])

However this solution and Mosh solution eliminate ties.

Is there a way to solve the exercise without eliminating ties and doing automatically? That is sorting a list with tuples and obtaining the maximum values without eliminating ties. Therefore obtaining (5, ‘i’) and (5, ’ ')

For example I search for the max function and I obtain the following, Built-in Functions — Python 3.9.2 documentation :

“… If multiple items are maximal, the function returns the first one encountered. …”

So I didn’t find I way to find all the maximum values when you obtain ties.

I know this is not efficient when you have something like [5, 3, 4, 5] but in the case of [(5, “a”), (3, “b”), (4, “c”), (5, “e”)] it make sense to obtain all the maximum values if you use only the first component of the tuple to find the maximum values.

Hey, did you ever solve this? I have been trying to have a go at Unit 5, Vid 23 like you and am having some trouble.

Dunno if that is right.
That is my first try :

sentence = “This is a common interview question”
winner = " "
summary = 0
for item in sentence.replace(" ", “”):
number = sentence.count(item)
if number > summary:
summary += number
winner = item, number

print(winner)