23 - Exercise in Python Course

sentence = “This is a common interview question”

Write a program to find the most repeated characters in this sentence.
That is correct answer by MOSH:

I know that is the best option. I wasn’t able to use all that thing same as Mosh without using Google.
I want You to check my answer which took me 5 minutes.
Would that be correct as well…??
What should I change?

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)

First off, any time you are writing code on this platform, you want to use a code fence surrounding your code so that the indentation, etc is preserved. Like this:

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

print (winner)
```

Now, I am not at all certain if that was the correct indentation, but if it was, I do not have great confidence in the solution. What exactly is summary variable supposed to be tracking? You are iterating through the string multiple times (which is wasteful) since you call the count method for every letter.

Personally, I like a simplified version where you only have to iterate through the string once because it is pretty easy to keep track of the most frequent character you have seen so far in addition to the frequency dictionary. Like you, I also think spaces should not be included in the count since they are not really “letters” though you would have to check with the interviewer if you were asked in a real interview.

import collections

String = “This is a common interview question”

print(collections.Counter(String).most_common(1)[0])

I just googled this and found it on stack overflow, it surprises me he did not show this sollution as that is much simpler, perhaps the course is outdated?

Interview questions will challenge you to implement things using basic APIs rather than using tools that will do all of the work for you. It is not outdated, just that there are pre built solutions for this particular problem (and many others). The idea he was going for in the course is more or less what they would expect you to do in an interview. For example, an interviewer would expect you to implement a sorting algorithm on your own rather than using a tool that sorts for you.

Hey thanks for the reply friend. Btw I am confused about something in this problem, he writes within the for loop ‘‘if char in char_frequency’’ which is something I don’t get because char_frequency is empty, he created that as an empty dictionary so how could there be something in it? This is just a very confusing solution to me.

I understand the confusion because there are two very similar lines.

The first is this:

for char in sentence:

That one is iterating through every letter in the sentence.

The second one is similar, but different:

if char in char_frequency:

This is checking if the character we are currently looking at in the for loop is already in our frequency dictionary at that iteration. On each iteration through the loop the dictionary gets updated so while it may be empty during the first time we go through the body of the loop, by the second time it contains at least the first letter we encountered (and by the third character it may contain 2 entries).

Try walking it through a simple case to convince yourself:

If the sentence is “a cat” then here will be each iteration:

  1. char is set to ‘a’; this is not in the dictionary yet so we add an entry for ‘a’ set to the value 1.
  2. char is set to ’ ’ (the space); this is not in the dictionary - which now contains an entry for (‘a’:1) - so we add an entry for the space set to the value 1.
  3. char is set to ‘c’; this is not in the dictionary - which contains two entries (‘a’: 1) and (’ ', 1) - so we add an entry for ‘c’ set to the value 1.
  4. char is set to ‘a’; this is in the dictionary so we get the current value (which is 1) and increment it; now the entry for ‘a’ is 2.
  5. char is set to ‘t’; this is not in the dictionary - which now contains (‘a’: 2), (’ ', 1), and (‘c’, 1) - so we add an entry for ‘t’ set to the value 1.

You see how in each iteration the dictionary has different contents?

After the loop completes, the dictionary contains the entries: (‘a’: 2), (’ ', 1), (‘c’, 1), and (‘t’, 1).

Hey man I understand it better now, thank you for your explanation I apreciate the effort you put into it. Cheers!

1 Like