I am a undergraduate student who is new here and loves programming. I meet a problem in practice and I want to ask for help here.
Given a string an integer n, return the nth most common word and it's count, ignore capitalization.
For the word, make sure all the letters are lowercase when you return it!
Hint: The split() function and dictionaries may be useful.
Example:
Input: "apple apple apple blue BlUe call", 2
Output: The list ["blue", 2]
My code is in the following:
from collections import Counter
def nth_most(str_in, n):
split_it = str_in.split(" ")
array = []
for word, count in Counter(split_it).most_common(n):
list = [word, count]
array.append(count)
array.sort()
if len(array) - n <= len(array) - 1:
c = array[len(array) - n]
return [word, c]
The test result is like in the following:
Traceback (most recent call last):
File "/grade/run/test.py", line 10, in test_one
self.assertEqual(nth_most('apple apple apple blue blue call', 3), ['call', 1])
File "/grade/run/bin/nth_most.py", line 10, in nth_most
c = array[len(array) - n]
IndexError: list index out of range
As well as
Traceback (most recent call last):
File "/grade/run/test.py", line 20, in test_negative
self.assertEqual(nth_most('awe Awe AWE BLUE BLUE call', 1), ['awe', 3])
AssertionError: Lists differ: ['BLUE', 2] != ['awe', 3]
First differing element 0:
'BLUE'
'awe'
I don't know what's wrong with my code.
Thank you very much for your help!