Python: Most frequent character counter

5.5k Views Asked by At

I am trying to create a program that asks a user to input a string and then displays the most frequently occurring characters in the string. I cannot use built in features such as dictionary. I think my problem now is that I have an infinite loop. How can I fix this? Here is my code:

string = input('Enter a string:')

#space for most popular character
pop_char = ''
#initialize number of times character appears
num = 0
#initialize index
index = 0

# pick one character at a time
for ch in string:
    #initialize character counter
    cc=0

#go through entire string and compare the character to the string characters
    while index < len(string):

        #if they are the same (converted to lower case), tally one to cc
        if ch.lower() == string[index].lower():
            cc = cc + 1
            index = index + 1

        #if count is greater than or equal to previous count, use this for
        #popular character
        if cc >= num:
            num = cc
            pop_char = ch

        else:
            index = index + 1
print('The most frequently occuring letter(s):', pop_char)
3

There are 3 best solutions below

2
On BEST ANSWER

if cc >= num: should be if cc > num:

On your first iteration, the first letter will equal the first letter of the string (obviously) and you will enter if ch.lower() == string[index].lower():. This will set cc to 1 which will, in turn, set num to 1 as well.

As they are both equal, as long as the second letter is not the same as the first, you will have an infinite loop that only enters the if cc >= num section and never update index past 1.

0
On

I think your issue is that you are changing the index value in two separate if clauses and you are using a >= instead of a > to check for the frequent character.

Imagine the very first letter running through your loop. On the first pass through, it will compare it to itself and increment the index and cc. cc is then updated to 1 and num is updated to 1. On the next run through, your letter doesn't match the second letter, but it does pass the second if case, guaranteeing that none of your values change and your index does not increment. I suggest changing cc >= num to cc > num.

4
On

If you cannot use any methods at all just use a double for loop:

def most_common():
    s = input('Enter a string:').lower()
    coun = 0
    best = ""
    for ch in s:
        cn = 0
        for c in s:
            cn += ch == c # will be True or False 1/0
        if cn >= coun: 
            if cn == coun: # catch letters that have equal count
                if ch in best: # avoid adding same char twice
                    continue
                best += ch # id equal and not already added concat to the stirng
            else:
                best = ch # else just set best
            coun = cn
    return 'The most frequently occuring letter(s): {}'.format(best)

Which works for characters that have equal counts:

In [14]: most_common()
Enter a string:foobar
Out[14]: 'The most frequently occuring letter(s): o'

In [15]: most_common()
Enter a string:foobb
Out[15]: 'The most frequently occuring letter(s): ob'