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

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...
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:...
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....
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
Circle the appropriate word or phrase in each of the italicized groups. Recently the price of...
Circle the appropriate word or phrase in each of the italicized groups. Recently the price of good A doubled. This resulted in the quantity of good B sold having a minimal (+5%) increase and the price of good B going up substantially (+50%). This suggests that goods A and B are [[ complimentary goods / substitute goods / luxury goods]]. And that, prior to the increase in good A’s price, production of good B was [[below capacity / at or...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT