I'd like feedback on my solution to the outlined programming challenge (medium level). I've tried it fast in any way I know how to, but, what might be a more efficient and/or pythonic solution?
[Letter Count II] 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.
import doctest
import logging
import timeit
from collections import Counter
from string import ascii_letters
def letter_count(s: str):
"""Return the word in s with the largest number of repeating letters
>>> letter_count(s="Today, is the greatest day ever!")
'greatest'
>>> letter_count(s="!!!Today, is the >>>>greatest day ever!")
'greatest'
>>> letter_count(s="!!!Today, is the greatest >>>> day ever!")
'greatest'
>>> letter_count(s="!!!Today, is the greatest >>>> day ever xx!")
'greatest'
>>> letter_count(s="")
-1
>>> letter_count(s="abcd efg hijk")
-1
"""
if s == "":
return -1
word_counters = dict((word, Counter(word)) for word in s.split(" "))
# tuple of (word(str), most common letter frequency(int))
most_repeating_word = ("", 0)
for word, counter in word_counters.items():
# most_common format = (letter(str), freq(int))
for most_common in counter.most_common():
if most_common[0] not in ascii_letters:
continue
if most_common[1] > most_repeating_word[1]:
most_repeating_word = (word, most_common[1])
break
if most_repeating_word[1] <= 1:
return -1
return "".join(l for l in most_repeating_word[0] if l in ascii_letters)
if __name__ == "__main__":
doctest.testmod()
print(timeit.timeit("letter_count(s='!!!Today, is the greatest >>>> day ever!')",
setup=("from __main__ import letter_count; from collections "
"import Counter; from string import ascii_letters"),
number=10000))