131k views
3 votes
Implement the function calcWordFrequencies() that uses a single prompt to read a list of words (separated by spaces). Then, the function outputs those words and their frequencies to the console.

Ex: If the prompt input is:

hey hi Mark hi mark
the console output is:

hey 1
hi 2
Mark 1
hi 2
mark 1

User GrayFace
by
5.2k points

2 Answers

7 votes

Final answer:

To count word frequencies in Python, use the input() function to collect user input, split the input into words, use a dictionary to track frequencies, then iterate and print each word with its frequency.

Step-by-step explanation:

To implement the function calcWordFrequencies that reads a list of words from the user and outputs the frequency of each word, you can follow these steps in Python:

  1. Use the input() function to prompt the user to enter a list of words separated by spaces.
  2. Create an empty dictionary to keep track of word frequencies.
  3. Split the input string into words using the split() method.
  4. Iterate over the list of words and for each word:
  5. If the word is already in the dictionary, increment its frequency.
  6. If the word is not in the dictionary, add it with a frequency of 1.
  7. Iterate over the dictionary and print each word along with its frequency.

Here is an example code snippet that demonstrates these steps:

def calcWordFrequencies():
text_input = input("Enter words separated by spaces: ")
frequencies = {}
for word in text_input.split():
if word in frequencies:
frequencies[word] += 1
else:
frequencies[word] = 1
for word, frequency in frequencies.items():
print(word, frequency)
calcWordFrequencies()
This function will output the frequency of each word entered by the user, satisfying the prompt's requirement.
6 votes

Final answer:

The function calcWordFrequencies prompts for input, splits the input into words, counts their occurrences using an object, and logs each word along with its frequency to the console.

Step-by-step explanation:

To implement the function calcWordFrequencies that reads a list of words separated by spaces and outputs their frequencies, you can use a programming language like JavaScript. The function will prompt the user for input, then split the input string by spaces to get an array of words. After that, the function will count the occurrences of each word using an object to store the frequencies, and finally, it will log the words and their respective frequencies to the console.

Here is an example of how this function might look:

function calcWordFrequencies() {
var input = prompt('Enter a list of words separated by spaces:');
var words = input.split(' ');
var frequencies = {};
for (var i = 0; i < words.length; i++)
for (var word in frequencies) {
console.log(word + ' ' + frequencies[word]);
}
}
Note: The example above converts all words to lowercase to count 'Mark' and 'mark' as two occurrences of the same word. You can remove the toLowerCase call if you want to distinguish between capitalized and lowercase words.

User Young Emil
by
5.9k points