This is an excercise from the book Python Workout by R.M Lerner.
I'm struggling to return a maximum value of the following code:
def most_repeating_word(ls):
listed = [x for x in ls.split()]
outcome = {}
for word in listed:
max_char = []
char_set = []
for char in word:
if char not in char_set:
char_set.append(char)
count = word.count(char)
max_char.append((char,count))
outcome[word] = (max_char)
return max(outcome, key = lambda x: x[1])
most_repeating_word('this is an elementary test example')
The function should return the word with highest repeating element, in this case "elementary" with e=3, but it returns "example".
I think the reason is that it compares second letters of the words, and returns 'x' as the max. I fail to understand how to access the tuples values. Using outcome.values
in the return part does not help.
CodePudding user response:
We can use for loop
on outcome
dictionary to check which letter in which word repeated the most.
The code
def most_repeating_word(ls):
listed = [x for x in ls.split()]
outcome = {}
for word in listed:
max_char = []
char_set = []
for char in word:
if char not in char_set:
char_set.append(char)
count = word.count(char)
max_char.append((char,count))
outcome[word] = (max_char)
repeating_word = ''
repeating_letter = ''
max_char_count = 0
for key in outcome:
for letter in outcome[key]:
if letter[1] > max_char_count:
repeating_word = key
repeating_letter = letter[0]
max_char_count = letter[1]
return [repeating_word, repeating_letter, max_char_count]
data = most_repeating_word('this is an elementary test example')
print(f"The word {data[0]} ham most repeated letter {data[1]} which occurs {data[2]} times")
Output
The word elementary ham most repeated letter e which occurs 3 times
CodePudding user response:
Since outcome
is a dictionary of lists of tuples, you need two max()
to find the word with the highest repeating element. The first max()
finds the count of the highest repeating element in a word, and the second max()
finds the word with the highest count.
Preserving the data structure you use, the key
argument needs to look like this: lambda word_maxchar: max(word_maxchar[1], key = lambda char_count: char_count[1])[1]
. The whole code would look like this:
def most_repeating_word(ls):
listed = [x for x in ls.split()]
outcome = {}
for word in listed:
max_char = []
char_set = []
for char in word:
if char not in char_set:
char_set.append(char)
count = word.count(char)
max_char.append((char,count))
outcome[word] = (max_char)
return max(outcome.items(), key = lambda word_maxchar: max(word_maxchar[1], key = lambda char_count: char_count[1])[1])[0]
most_repeating_word('this is an elementary test example')
Also note that in order to iterate over both keys and values of a dictionary, you need to use outcome.items()
.
With a little modification of your data structure, we can achieve slightly better readability of the code:
def most_repeating_word(ls):
listed = [x for x in ls.split()]
outcome = {}
for word in listed:
max_char = []
char_set = []
for char in word:
if char not in char_set:
char_set.append(char)
count = word.count(char)
max_char.append((char,count))
outcome[word] = max(max_char, key = lambda char_count: char_count[1])
return max(outcome.items(), key = lambda x: x[1][1])[0]
most_repeating_word('this is an elementary test example')