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,...
What is wrong with this header file? The program wont compile but if i transfer the...
What is wrong with this header file? The program wont compile but if i transfer the body of the header file into the main file it will compile just fine. This is the error message that pops up: [Error] 'cout' was not declared in this scope // Specification file for the Account class. #ifndef ITEM_H #define ITEM_H #include <string> using std::string; class Can {    private:        double price = 0.0;        string company;        string content;...
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...
With the code given write python code that prints the probability of getting a flush when...
With the code given write python code that prints the probability of getting a flush when you run 10**5 trails. this is what i have so far but it says that isFlush is not defined. why? # Print out probability that a 5-card hand has all the same suit #seed(0) num_trials = 10**5 trials = [dealHand for k in range(num_trials)] # 5 card hand prob = sum([l for h in trials if isFlush(h)])/num_trials # sum the list of numbers that...
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...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Write a C++ Function that finds the 4 values that are adjacent of one another (Vertical,...
    asked 3 minutes ago
  • 5.11 Suppose that each of the following Update operations is applied directly to the database state...
    asked 12 minutes ago
  • Consider the following relations for a database that keeps track of business trips of salespersons in...
    asked 27 minutes ago
  • Artificial Intelligence Question. This topic is about Heuristic search and stochastic methods. I will like it...
    asked 49 minutes ago
  • The following are programming examples in which multithreading provides better performance than a single-threaded solution: a....
    asked 58 minutes ago
  • The CASE structure is a selection structure, however not all selection structures may be represented as...
    asked 1 hour ago
  • A man stands on the roof of a building of height 15.1 m and throws a...
    asked 1 hour ago
  • What is the role of benzyltrimethylammonium hydroxide in the synthesis of tetraphenylcyclopentadienone, and why is it...
    asked 1 hour ago
  • Read the following description of the MHC-PMA (Mental Health Care-Patient Management Application) then answer the questions...
    asked 1 hour ago
  • How does your worldview influence the level of apprehension or attitudes you feel when communicating with...
    asked 1 hour ago
  • 11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" program....
    asked 1 hour ago
  • 2.Mike has asked you to come in and explain the strengths and weaknesses of objective versus...
    asked 1 hour ago