Categories
Coderbyte Data Structures and Algorithms Easy Python

[Coderbyte] – Letter Count I [Easy]

This version of letter count is a challenge I have enjoyed solving because I have found a solution shorter and easier to understand than most of the top answers from Coderbyte.  The challenge requirements can be seen below:

Have the function LetterCountI(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: “Today, is the greatest day ever!” should return greatest because it has 2 e’s (and 2 t’s) and it comes before ever which also has 2 e’s. If there are no words with repeating letters return -1. Words will be separated by spaces. 

Use the Parameter Testing feature in the box below to test your code with different arguments.

The first step when I encounter challenges that require me to work with the words from a given String is to split the String in a list of words. For this problem, I have also set a counter for the number of repeated letters and a variable that is assigned the word with the most repeated letters. These two variables are used in the two for loops. The first three lines of code do that:

    words = s.split()
    count = 0
    mostRepeatedLetters = ""

The next step is to iterate over the list of words. I need to take each word from the list of words individually. The second for loop is required to check what letter is repeated and how many times it is repeated in each word. That is done by the two for loops below:

    words = s.split()
    count = 0
    mostRepeatedLetters = ""

    for word in words:
        for char in word:

The most substantial part of the answer is below. The solution makes use of a Python method called count(). This method is an inbuilt function that takes a substring and returns the number of occurrences of the given substring in the String.  For instance, if the count() method with the substring “l” as an argument is called on the String “hello”, it will return two because the letter “l” occurs two times. 

“hello”.count(“l”) -> 2.

This is what the code below does. For each word, it counts how many times each character from that word occurs in that word. If a letter appears two times or more, then it means the letter is repeated. Do not forget that the variable called count is a counter. That is, it is assigned the highest number of times a letter occurred in any word. The variable called mostRepeatedLetters is assigned the word where the most occurrences of a letter were found.

    for word in words:
        for char in word:
            if word.count(char) > count:
                count = word.count(char)
                mostRepeatedLetters = word

After all the words are checked, the mostRepeatedLetters variable is assigned the word with the most repeated letters. The count variable is assigned the biggest number of times a letter has occurred in any word.

If we run the solution with the string “Today, is the greatest day ever!”, the variable mostRepeatedLetters is assigned the word “greatest” and the count variable is assigned the number two because it was two repeating letters.

My full solution can be seen below:

def LetterCountI(s):
    words = s.split()
    count = 0
    mostRepeatedLetters = ""
    
    for word in words:
        for char in word:
            if word.count(char) > count:
                count = word.count(char)
                mostRepeatedLetters = word
    
    if count < 2: return '-1'
    else: return mostRepeatedLetters

The official Coderbyte solution is as follows:

def LetterCountI(str): 
  
  # list of words
  words = str.split(" ")
  
  # the table that will contain each word with a key and value pair
  # being the characters and the number of times they appeared
  # e.g. {hello: {h: 1, e: 1, l: 2, o: 1}}
  table = {}
  
  for i in range(0, len(words)):
    
    # create new entry in table with the key 
    # being this word
    thisWord = words[i]
    table[thisWord] = {}
    
    # create a key/value pair that will store
    # the highest letter count for each word
    table[thisWord]["highest"] = 0
    
    # loop through each character in word and
    # store number of times each letter appears
    for c in range(0, len(words[i])):
      
      # see if this character already exists in table for
      # this word: if so add 1 to the count, if not set count = 1
      thisChar = thisWord
      if thisChar in table[thisWord]:
        table[thisWord][thisChar] += 1
      else:
        table[thisWord][thisChar] = 1
        
      # update the highest letter count for each 
      # new letter in the iteration
      if table[thisWord][thisChar] &amp;gt; table[thisWord]["highest"]:
        table[thisWord]["highest"] = table[thisWord][thisChar]
        
    # setup a new object that will store the word
    # with the highest number of repeating characters
    answer = {"word": '', "count": 1}
    
    # now determine what word has the highest number 
    # of repeating letters by accessing the "highest"
    # property of each word in the table
    for w in table:
      if table[w]["highest"] &amp;gt; answer["count"]:
        answer["count"] = table[w]["highest"]
        answer["word"] = w
      
  if answer["count"] == 1:
    return -1
  else:
    return answer["word"]

Leave a Reply

Your email address will not be published. Required fields are marked *