Question

Write a ruby program to find the number of occurrences of a given word in a...

Write a ruby program to find the number of occurrences of a given word in a string. I managed to come up with this solution.

class count
  
def words(string)

words = string.downcase.split(/[\s,]+/).map {|w| remove_punctuation(w)}

frequency = Hash.new(0)

words.each do |word|
frequency[word] += 1
end

return frequency
end

but I need the format to be


expected: [["love", 3], ["one", 1], ["the", 2], ["red", 1], ["to", 4], ["light", 1] , ["green", 1], ["leads", 3],, ["all", 2], ["you", 1]]

obteve: {"light" => 1, "love" => 3, "all" => 2, "one" => 1, "leads" => 3, "red" => 1, "green" => 1, "o" => 2, "para" => 4, "você" => 1}

E deixe-me mostrar a você as informações na ordem em que esta frase está, com meu resultado aparecendo em uma ordem totalmente diferente.

Homework Answers

Know the answer?
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
Similar Questions
Write a program in python reverse each and every word of the sentence using function taking...
Write a program in python reverse each and every word of the sentence using function taking string as input parameter from user and passing that string into function and also explain the logic ======================================= input: 'Exam is gonna be good' output: 'maxE si annog eb doog' ================ i am trying like this but getting confused '.'join([w::-1] for w in s.split()]) i need more focus on explanations because it is making me confuse i am not clear with concepts so please...
Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated...
Python 3 Implement the function condense, which takes a "passage" (a string containing multiple sentences separated by the '\n' character), and returns a condensed version of that passage based on the following logic: the frequency with which each word appears in the entire passage is computed a score for each sentence in the passage is computed by summing the frequency of each word in the sentence the sentences with the top 3 scores (we assume there are no ties) are...
Project File Processing. Write a program that will read in from input file one line at...
Project File Processing. Write a program that will read in from input file one line at a time until end of file 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 either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces,...
write a code in python Write the following functions below based on their comments. Note Pass...
write a code in python Write the following functions below based on their comments. Note Pass is a key word you can use to have a function the does not do anything. You are only allowed to use what was discussed in the lectures, labs and assignments, and there is no need to import any libraries. #!/usr/bin/python3 #(1 Mark) This function will take in a string of digits and check to see if all the digits in the string are...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
This is C. Please write it C. 1) Prompt the user to enter a string of...
This is C. Please write it C. 1) Prompt the user to enter a string of their choosing. Store the text in a string. Output the string. (1 pt) Ex: Enter a sample text: we'll continue our quest in space. there will be more shuttle flights and more shuttle crews and, yes, more volunteers, more civilians, more teachers in space. nothing ends here; our hopes and our journeys continue! You entered: we'll continue our quest in space. there will be...
convert to python 3 from python 2 from Tkinter import * # the blueprint for a...
convert to python 3 from python 2 from Tkinter import * # the blueprint for a room class Room(object): # the constructor def __init__(self,name,image): # rooms have a name, exits (e.g., south), exit locations (e.g., to the south is room n), # items (e.g., table), item descriptions (for each item), and grabbables (things that can # be taken and put into the inventory) self.name = name self.image = image self.exits = {} self.items = {} self.grabbables = [] # getters...
1) 2 point charges are separated by a distance of 8 cm. The left charge is...
1) 2 point charges are separated by a distance of 8 cm. The left charge is 48 mC and the right charge is -16mC. Using a full sheet of paper: draw the 2 charges separated by 8cm, centered in the sheet. (if you are missing a ruler estimate 8cm as ⅓ a paper sheet length). [6] a) Draw field lines to indicate the electric fields for this distribution. [4] b) Draw 3 equipotential surfaces, 1 each, that pass: -Through the...
Write a program in python that reads in the file quiztext (txt) and creates a list...
Write a program in python that reads in the file quiztext (txt) and creates a list of each unique word used in the file, then prints the list. Generate a report that lists each individual word followed by the number of times it appears in the text, for example: Frequency_list = [('was', 3), ('bird',5), ('it', 27)….] and so on. Note: Notice the structure: a list of tuples. If you're really feeling daring, Google how to sort this new list based...