Knowee
Questions
Features
Study Tools

Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. For example:>>> repeat_word_count("buffalo buffalo buffalo buffalo", 2)['buffalo']>>> repeat_word_count("one one was a racehorse two two was one too", 3)['one']>>> repeat_word_count("how much wood could a wood chuck chuck", 1)['a', 'chuck', 'could', 'how', 'much', 'wood']

Question

Write a function repeat_word_count(text, n) that takes a string text and a positive integer n, converts text into a list of words based on simple whitespace separation (with no removal of punctuation or changing of case), and returns a sorted list of words that occur n or more times in text. For example:>>> repeat_word_count("buffalo buffalo buffalo buffalo", 2)['buffalo']>>> repeat_word_count("one one was a racehorse two two was one too", 3)['one']>>> repeat_word_count("how much wood could a wood chuck chuck", 1)['a', 'chuck', 'could', 'how', 'much', 'wood']

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is a Python function that implements the described behavior:

def repeat_word_count(text, n):
    # Split the text into words
    words = text.split()
    
    # Create a dictionary to count the occurrences of each word
    word_counts = {}
    for word in words:
        if word in word_counts:
            word_counts[word] += 1
        else:
            word_counts[word] = 1
    
    # Create a list of words that occur n or more times
    repeated_words = [word for word, count in word_counts.items() if count >= n]
    
    # Sort the list of repeated words
    repeated_words.sort()
    
    return repeated_words

You can use this function like this:

print(repeat_word_count("buffalo buffalo buffalo buffalo", 2))  # Output: ['buffalo']
print(repeat_word_count("one one was a racehorse two two was one too", 3))  # Output: ['one']
print(repeat_word_count("how much wood could a wood chuck chuck", 1))  # Output: ['a', 'chuck', 'could', 'how', 'much', 'wood']

This problem has been solved

Solution 2

Sure, here is a Python function that implements the described behavior:

def repeat_word_count(text, n):
    # Split the text into words
    words = text.split()
    
    # Create a dictionary to count the occurrences of each word
    word_counts = {}
    for word in words:
        if word in word_counts:
            word_counts[word] += 1
        else:
            word_counts[word] = 1
    
    # Create a list of words that occur n or more times
    repeated_words = [word for word, count in word_counts.items() if count >= n]
    
    # Sort the list of repeated words
    repeated_words.sort()
    
    return repeated_words

You can use this function like this:

print(repeat_word_count("buffalo buffalo buffalo buffalo", 2))  # Output: ['buffalo']
print(repeat_word_count("one one was a racehorse two two was one too", 3))  # Output: ['one']
print(repeat_word_count("how much wood could a wood chuck chuck", 1))  # Output: ['a', 'chuck', 'could', 'how', 'much', 'wood']

This problem has been solved

Similar Questions

We are given a function countWordOccurrences that takes two string parameters sentence and word."Sentence" is a string made up of multiple words separated by spaces.We have to find how many times 'word' occurs in the given 'sentence'.The 'word' should be an exact match.Note: The case (uppercase/lowercase) of the letters in the word does NOT matter.InputFirst line contains space separated series of words which represents the string "sentence".Second line contains a single word "word".Constraints1 ≤ length(sentence), length(word) ≤ 104length(word) ≤ length(sentence)OutputWe have to return an integer which is the number of occurrences of the single word 'word' in the string 'sentence'.

Write a function top5_words(text) that takes a single argument text (a non-empty string), tokenises text into words based on whitespace (once again, without any stripping of punctuation or case normalisation), and returns the top-5 words as a list of strings, in descending order of frequency. If there is a tie in frequency at any point, the words with the same frequency should be sub-sorted alphabetically (e.g. if 'turtle' and 'grok' both occur 5 times, 'grok' should come first). If there are less than five distinct words in text, the function should return all words in descending order of frequency (with the same tie-breaking mechanism). For example:>>> top5_words("one one was a racehorse two two was one too")["one", "two", "was", "a", "racehorse"]>>> top5_words("buffalo buffalo buffalo chicken buffalo")["buffalo", "chicken"]>>> top5_words("the quick brown fox jumped over the lazy dog")["the", "brown", "dog", "fox", "jumped"]

Have the function LetterCount(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters return -1. Words will be separated by spaces.ExamplesInput: "Hello apple pie"Output: HelloInput: "No words"Output: -1

Create a simple function named wordCount that takes a sentence (String) separated with space and count the words in the sentence. The function will return the number of words in the given sentence.Use the skeleton file, to develop the function.FUNCTION wordCount(str IN VARCHAR2)Function name :   wordCountparameter  :  str datatype VARCHAR2Function return type : NumberNote:Do not change the function nameDo not change the argument count and orderDo not change the output text.You can query the table records using select statementDelimiter / is mandatory. If delimiter is missed, then while executing nothing will be displayedWhile executing, if you get WARNING: FUNCTION CREATED WITH COMPILATION ERRORS, then use SHOW ERRORS to display the errors in the function.Instructions:1. Create the function successfully2. Once the function is created, check the functionality of the function using appropriate select call3. DO NOT submit the select statement. Submit only the CREATE FUNCTION query.Sample Input and Output:If we execute the function with "HELLO WORLD" it should return 2.SubmitSaveExecutePrevious Submission

Your job is to write the function prevword_ave_len(word) which takes a single argument word (a str) and returns the average length (in characters) of the word that precedes word in the text each time it appears. That is, for each occurrence of word in the text, you are to determine the (single) word which precedes it, and calculate the average length of all those preceding words. If one of the occurrences of word happens to be the first word occurring in the text, the length of the preceding word for that occurrence should be counted as zero. In the instance that word doesn't occur in the text, the function should return -1. Note that we define a "word" to simply be a string that is delimited by "whitespace" (i.e. punctuation following a word is included as part of the word). Additionally, the casing in the original text (and in word) should be preserved.>>> prevword_ave_len('soul;')2.0>>> prevword_ave_len('whale')-1>>> prevword_ave_len('and')6.714285714285714

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.