Knowee
Questions
Features
Study Tools

Have the function LetterCount(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.ExamplesInput: "Hello apple pie"Output: HelloInput: "No words"Output: -1

Question

Have the function LetterCount(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.ExamplesInput: "Hello apple pie"Output: HelloInput: "No words"Output: -1

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python function that implements the described behavior:

def LetterCount(str):
    words = str.split()  # split the string into words
    max_count = 0  # initialize the maximum count of repeated letters
    max_word = ''  # initialize the word with the maximum count of repeated letters

    for word in words:  # iterate over each word
        for letter in word:  # iterate over each letter in the word
            letter_count = word.count(letter)  # count the occurrences of the letter in the word
            if letter_count > max_count:  # if this count is greater than the current maximum
                max_count = letter_count  # update the maximum count
                max_word = word  # update the word with the maximum count

    if max_count > 1:  # if there is a word with repeated letters
        return max_word  # return that word
    else:
        return -1  # otherwise, return -1

You can use this function like this:

print(LetterCount("Hello apple pie"))  # Output: Hello
print(LetterCount("No words"))  # Output: -1

This problem has been solved

Similar Questions

Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. For example:>>> repeat_word_count("buffalo buffalo buffalo buffalo", 2)['buffalo']>>> repeat_word_count("one one was a racehorse two two was one too", 3)['one']>>> repeat_word_count("how much wood could a wood chuck chuck", 1)['a', 'chuck', 'could', 'how', 'much', 'wood']

Have the function LongestWord(sen) take the sen parameter being passed and return the longest word in the string. If there are two or more words that are the same length, return the first word from the string with that length. Ignore punctuation and assume sen will not be empty. Words may also contain numbers, for example "Hello world123 567"

We are given a function countWordOccurrences that takes two string parameters sentence and word."Sentence" is a string made up of multiple words separated by spaces.We have to find how many times 'word' occurs in the given 'sentence'.The 'word' should be an exact match.Note: The case (uppercase/lowercase) of the letters in the word does NOT matter.InputFirst line contains space separated series of words which represents the string "sentence".Second line contains a single word "word".Constraints1 ≤ length(sentence), length(word) ≤ 104length(word) ≤ length(sentence)OutputWe have to return an integer which is the number of occurrences of the single word 'word' in the string 'sentence'.

Write a function top5_words(text) that takes a single argument text (a non-empty string), tokenises text into words based on whitespace (once again, without any stripping of punctuation or case normalisation), and returns the top-5 words as a list of strings, in descending order of frequency. If there is a tie in frequency at any point, the words with the same frequency should be sub-sorted alphabetically (e.g. if 'turtle' and 'grok' both occur 5 times, 'grok' should come first). If there are less than five distinct words in text, the function should return all words in descending order of frequency (with the same tie-breaking mechanism). For example:>>> top5_words("one one was a racehorse two two was one too")["one", "two", "was", "a", "racehorse"]>>> top5_words("buffalo buffalo buffalo chicken buffalo")["buffalo", "chicken"]>>> top5_words("the quick brown fox jumped over the lazy dog")["the", "brown", "dog", "fox", "jumped"]

Use python regular expression for completing the following code to find the number of words in a given string. The words are separated by either single space or single underscore character.Sample input:Have a nice_day!Sample output:4

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.