Pybites Logo

Most identical letters in a word

Level: Intermediate (score: 3)

In this Bite, you will write a function called max_letter_word() to identify the first word with the most repeated letter in the supplied text, and return a tuple with the word, the letter, and the count of that letter in the word. For example:

>>> max_letter_word('The studio was filled with the rich scent of roses')
('filled', 'l', 2)

For this Bite you may like to use features like built-ins and comprehensions, and you might also find something useful in the collections module that helps (hint, it's Counter).

For a given letter your solution should not differentiate by case, thus the 'S' and 's' in 'Sister' count as the same letter: str.casefold() is perhaps the best way to achieve this since it has the added advantage of expanding some tricky characters to their constituent letters. For example:

>>> "Schloß".casefold()
schloss

Words are separated by white space; hyphenated words count as a single word, as do abbreviated words. Anything containing letters (including non-English letters) can be assumed to be a word, but don't count its non-letter symbols, digits etc. Leading or trailing non-letter symbols, digits etc (including ' and -) should be removed from the returned word.

Do review the Bite's tests for further example texts with their expected output; also to identify edge cases and see what behaviour is expected for invalid input.

Have fun with the power of Python!