PYTHON QUESTION: Write a function named longEnough that takes two parameters: 1)
ID: 3574658 • Letter: P
Question
PYTHON QUESTION:
Write a function named longEnough that takes two parameters:
1) s, a string
2) threshold, a non-negative integer
The function longEnough should return True if the length of s is at least threshold, and False otherwise
For example, the function call longEnough('Amen', 4) should return True.
Question 13b (14 points) Write a function named longWords that takes two parameters:
1) t, a string
2) i, a non-negative integer
The string t contains only lower case letters and white space.
The function longWords should return a dictionary in which the keys are all words in t that contain at least i characters. The value corresponding to each key is the number of times that that word occurs in t.
The function longWords should call the function longEnough to determine whether a word should be a key in the returned dictionary.
For example, the following is a correct input/output pair
>>> text = 'one fish two fish red fish blue fish'
>>> print(longWords(text, 4))
{'blue': 1, 'fish': 4}
Explanation / Answer
def longEnough(s,threshold):
if (len(s) >= threshold):
flag = 'True'
else:
flag = 'False'
return flag
from collections import Counter
def longWords(t,n):
words = t.split()
p=[]
for i in range(0,len(words)):
z=longEnough(words[i],n)
if (z=='True'):
p.append(words[i])
freqs = Counter(p)
print(freqs)
longWords('The quick brown fox jumps over the lazy dog lazy',4)
Note:- python fix indentation if some tab is missing while copying to text editor
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.