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...
Python Text processing/masks Write a function called unique words that takes a phrase as an input...
Python Text processing/masks Write a function called unique words that takes a phrase as an input string and returns a list of the unique words found in that phrase. The words should be listed in alphabetical order. Scriipt for ATOM or REPL, NOT PYTHON SHELL
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...
7. Write a function that accepts a sentence as the argument and converts each word to...
7. Write a function that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY.
Write a function in python that accepts a sentence as the argument and converts each word...
Write a function in python that accepts a sentence as the argument and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin, you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: English: I SLEPT MOST OF THE NIGHTPIG LATIN: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY. Please, I want the coding indented and then the...
Write a recursive method that takes a String argument and recursively prints out each word in...
Write a recursive method that takes a String argument and recursively prints out each word in the String on a different line. Note: You will need to use methods such as indexOf() and substring() within the String class to identify each word and the remaining string. For example, if the input was “the cat purred”, then the method would print the following to the Java console: the cat purred Challenge Problem Write a recursive method that takes a string as...
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 program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
Write a program that takes a string from the user, identifies and counts all unique characters...
Write a program that takes a string from the user, identifies and counts all unique characters in that given string. You are bound to use only built-in string functions where necessary. For identification of unique characters and for counting of the characters make separate functions. For character identification Develop a program that takes a string argument, and returns an array containing all unique characters. For character counting Develop a program that takes an array returned from above function as an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT