Question

In: Computer Science

Add a new function that takes a phrase as an argument and counts each unique word...

Add a new function that takes a phrase as an argument and counts each unique word in the phrase. The function should return a list of lists, where each sub-list is a unique [word, count] pair. Hint: A well-written list comprehension can solve this in a single line of code, but this approach is not required.

Solutions

Expert Solution

Here is the required solution in python

here is the code

def fun(phrase):
#find unique words
originallist=list(phrase.split(" "))
uniqueword=set(originallist)
#create a empty list
lis=[]
#fill the entries in the list
for x in uniqueword:
frequency=0
for i in range(len(originallist)):
if x==originallist[i]:
frequency=frequency+1
lis.append([x,frequency])
#return the lis
return lis
#test case   
phrase="Add a new function that takes a phrase as an argument and counts each unique word in the phrase"
print(fun(phrase))
  


Related Solutions

Write a function bracket_by_len that takes a word as an input argument and returns the word...
Write a function bracket_by_len that takes a word as an input argument and returns the word bracketed to indicate its length. Words less than five characters long are bracketed with << >>, words five to ten letters long are bracketed with (* *), and words over ten characters long are bracketed with /+ +/. Your function should require the calling function to provide as the first argument, space for the result, and as the third argument, the amount of space...
using Dr java Objective: Write a program that takes a phrase and then counts the number...
using Dr java Objective: Write a program that takes a phrase and then counts the number of vowels (case does not matter) in the phrase. It then should display all of the vowels in sorted ascending order according to their count. Only consider {AEIOU} as the vowels. Hint: It may be a good idea to keep track and sort two arrays: Has the vowels in alphabetic order Has the number of said vowels Whenever one would swap then it swaps...
IN PYTHON Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized...
IN PYTHON Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized words separated by spaces, and returns a new string consisting of those words but with the only the last letter in each word uppercase. You can assume that the string contains only letters and spaces, no punctuation. Examples: >>> capitalize_last('tEst WiTH rANdoM capITaliZATioN') 'tesT witH randoM capitalizatioN' >>> capitalize_last('') '' >>> capitalize_last('i am the senate') 'I aM thE senatE'
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
Write a function called HowMany(), which counts the occurrences of the second argument which is a...
Write a function called HowMany(), which counts the occurrences of the second argument which is a single character in the first argument which is a string. This function should have 2 arguments, the first one is a string and the second argument is a character. For example, the following function : i = count("helloyoutwo", 'o'); would return i= 3. Test your function in a complete code. Language: c++
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is...
PYTHON: Write a function named is_palindrome() that returns boolean True if a word or phrase is a palindrome, and boolean False if it is not. The palindromes for this problem are contained in the list below. You must use this list in your solution. Use a for loop to retrieve each palindrome and test it. Your script should test each string in the list of palindromes below. Be aware there's a second list of palindromes embedded in the palindromes list....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT