Question

In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq)....

In python please.

Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner.

Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter words to the function wordsOfFreqency(words, freq) should be a list data structure.

If two strings in the list words contain the same sequence of characters but are different case, they are considered the same. For example, we will consider the strings UCONN and uconn to be the same.

Test cases 1-4

Example 1:

Input:

unus duo Duo tres Tres tRes
1

Output:

unus

Example 2:

Input:

unus duo Duo tres Tres tRes
2

Output:

duo
Duo

Example 3: Input:

unus Duo duo tres Tres tRes
2

Output:

Duo
duo

Compare Example 3 and Example 4. Note that the order of the output strings Duo, duo is the same as they appeared in the input.

Example 4: Input:

tres Tres tRes Duo duo unus 
2

Output:

Duo
duo

For simplicity, you can assume that there will be only one distinct string for each frequency. That means, the input will NOT be like this.

unus duo Duo two Two tres Tres tRes
2

Hint: For this lab, you may need to use the dictionary data structure and a string method.

For Fun (Optional): Try adding the following statement at the end of your code, run the code, and see what does it print.

print(wordsOfFreqency.__doc__)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Code:

def wordsOfFreqency(words, freq):
   """ Function that reads series of words and an integer and returns list of words with the given frequency """
   # Dictionary that holds the word and frequency
   wordDict = {}
  
   # Iterating over each word
   for word in words:
       # Checking for existence
       if word.lower() in wordDict.keys():
           # Incrementing count
           wordDict[word.lower()] += 1
       # Adding a new key
       else:
           wordDict[word.lower()] = 1
      
   # List that holds the result
   res = []
      
   # Comparing each word with freq
   for word in words:
       # Comparing frequency
       if wordDict[word.lower()] == freq:
           res.append(word)
  
   # Returning list
   return res
          
  
def main():
   """ Reading input from user and calling function """
   # Reading input
   ip = input("\nEnter your input: ")
  
   # Forming list of words and frequency
   words = []
  
   # Splitting and iterating over input
   fields = ip.strip().split()
   words = fields[:-1]
   freq = int(fields[-1])
  
   # Calling function
   res = wordsOfFreqency(words, freq)
  
   print("\nResult: ", end="")
  
   # Printing result
   for word in res:
       print(word, end=" ")
  
   print("\n")
      
  
# Calling main function
main()

_________________________________________________________________________________________________________________________

Code Screenshot:

__________________________________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two...

    Question / of 2. Case insensitive string compare: [40 marks/ Write a function that compares two strings while ignoring the case of alphabetical characters. Your program will take two strings as input from the user and use your function to compare them. Assume a maximum C-string size of 1000 characters. Make sure your code works for any input number, not just the test cases. Your code will be tested on other test cases not listed here. Please properly comment your...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list...

    C++ SECTION 1) (15 points) Write a full C++ program that reads an integer, a list of words, and a character. The integer signifies how many words are in the list. The output of the program is every word in the list that contains the character at least once. Assume at least one word in the list will contain the given character. Ex: If the input is: 4 hello zoo sleep drizzle z then the output is: 200 drizzle To...

  • Write a Python program (remove_first_char.py) that removes the first character from each item in a list...

    Write a Python program (remove_first_char.py) that removes the first character from each item in a list of words. Sometimes, we come across an issue in which we require to delete the first character from each string, that we might have added by mistake and we need to extend this to the whole list. Having shorthands (like this program) to perform this particular job is always a plus. Your program should contain two functions: (1) remove_first(list): This function takes in a...

  • PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """...

    PYTHON PLEASE Write a function to censor words from a sentence code: def censor_words(sentence, list_of_words): """ The function takes a sentence and a list of words as input. The output is the sentence after censoring all the matching words in the sentence. Censoring is done by replacing each character in the censored word with a * sign. For example, the sentence "hello yes no", if we censor the word yes, will become "hello *** no" Note: do not censor the...

  • (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

    (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons (;) or pound signs (#) should be ignored. All empty lines must be ignored Lines containing a string between square brackets are sections. Within each section, lines may contain numbers or strings. For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that. Example input: ; this is a...

  • write a program in C Write a program to implement the following requirement: The program will...

    write a program in C Write a program to implement the following requirement: The program will read from standard input any text up to 10, 900, characters and store each unique word (any string that does not contain any whitespace) into a node of a linked list, following the following Node struct: struct NODE { char *word; struct NODE * prev; Note that each node must store a unique word, i.e., no word is stored in more than one node....

  • write a function firstLetterWords(words) that takes as a parameter a list of strings named words and...

    write a function firstLetterWords(words) that takes as a parameter a list of strings named words and returns a dictionary with lower case letters as keys. But now associate with each key the list of the words in words that begin with that letter. For example, if the list is ['ant', 'bee', 'armadillo', 'dog', 'cat'], then your function should return the dictionary {'a': ['ant', 'armadillo'], 'b': ['bee'], 'c': ['cat'], 'd': ['dog']}.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT