Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the most frequent letter in a string?

Tags:

python

class MyString:
    def __init__(self, myString):
        self.__myString = myString

    def countWord(self):
         count = len(self.__myString.split())
         return count

    def findMostFrequentChar(self):
        # ?

I need to implement findMostFrequenctChar. The only hint she gave us was that we needed to make 2 lists. and this is where she lost me.

Here's the code that calls the function:

def main():
    aString = MyString("This is a super long long long string. Please help count me")
    print("There are", aString.countWord(), "words in the string.")

    count, letter = aString.findMostFrequentChar()
    print("The most frequent character is", letter, "which appeared", count, "times")

main()
like image 781
AmbieGirl Avatar asked Nov 12 '17 17:11

AmbieGirl


People also ask

How do you count occurrences in a string?

Count Number of Occurrences in a String with .count() method. The method takes one argument, either a character or a substring, and returns the number of times that character exists in the string associated with the method.

How do you count the most frequent letters in Python?

Method 2 : Using collections.Counter() + max() The most suggested method that could be used to find all occurrences is this method, this actually gets all element frequency and could also be used to print single element frequency if required. We find maximum occurring character by using max() on values.

How do you count specific occurrences of character in a string?

The string count() method returns the number of occurrences of a substring in the given string. In simple words, count() method searches the substring in the given string and returns how many times the substring is present in it.

How do you count the number of times a letter occurs in a string?

str. count(a) is the best solution to count a single character in a string. But if you need to count more characters you would have to read the whole string as many times as characters you want to count.


1 Answers

The only hint she gave us was that we needed to make 2 lists. and this is where she lost me.

That seems to imply that you cannot use collections.Counter, and probably not even dictionaries.

If we can assume that letters are defined as the English alphabet, then one list is enough. In that case, you could create a list of 26 items, all initialized to 0. Then you could iterate over the characters of the string, and for each letter of the English alphabet, increment the count of the n-th item in the list, where n is the index of the letter in the alphabet.

Create a list of 26 zeros:

counts = [0] * 26

Loop over the characters of the input string s:

for c in s:

Check that the character is a letter:

if 'a' <= c.lower() <= 'z'

Calculate the 0-based index of the letter in the alphabet and increment the count:

index = ord(c.lower()) - ord('a')
counts[index] += 1

Once you have the counts, you can find the index that has the maximum value (left for you as an exercise), and get the corresponding character with chr(index + ord('a')).

like image 169
janos Avatar answered Sep 28 '22 23:09

janos