Write a function word_counts

Assignment Help Programming Languages
Reference no: EM13324583

Write a function word_count(s) that parses the string s into words and returns an array (of arrays of the form [word, count]) representing the frequency of each word. Words are case-insensitive:

'Hello', 'HELLO', and 'hello' are instances of the same word. It is helpful to write helper functions in addition to word_count.

The following processes the first paragraph of Alice in Wonderland. Note that res2 is an array of those words that occur at least twice in this paragraph.

some_text = <<SAMPLE_TEXT

Alice was beginning to get very tired of sitting by her sister on the bank,and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it," and what is the use of a book," thought Alice "without pictures or conversation?"

SAMPLE_TEXT

res = word_count(some_text)
print res, "\n\n"
res2 = res.select { |pair| pair[1] > 1 }
res2.sort.each do |key, count|
puts "#{key}: #{count}"
end


[["of", 3], ["or", 3], ["the", 3], ["alice", 2], ["and", 2], ["book", 2], ["had", 2], ["her", 2], ["it", 2], ["pictures", 2], ["sister", 2], ["to", 2], ["was",2], ["a", 1], ["bank", 1], ["beginning", 1], ["but", 1], ["by", 1], ["conversation", 1], ["conversations", 1], ["do", 1], ["get", 1], ["having", 1], ["in", 1],["into", 1], ["is", 1], ["no", 1], ["nothing", 1], ["on", 1], ["once", 1], ["peeped", 1], ["reading", 1], ["she", 1], ["sitting", 1], ["thought", 1], ["tired",1], ["twice", 1], ["use", 1], ["very", 1], ["what", 1], ["without", 1]]

alice: 2
and: 2
book: 2
had: 2
her: 2
it: 2
of: 3
or: 3
pictures: 2
sister: 2
the: 3
to: 2
was: 2

Reference no: EM13324583

Questions Cloud

Find the heat absorbed by the gas during this process : A gas is allowed to expand isothermally until it reaches its final volume and its pressure is 2.97 atm. Find the heat absorbed by the gas during this process
In which sentence is the clause used as an adverb : Each one of the following sentences contains a clause. In which sentence is the clause used as an adverb?
Employees earnings and taxes : Employees Earnings and Taxes
Explain the solubility product constant for agcl : The solubility product constant for AgCl at 298 K is 1.82 × 10-10. Calculate E° for the process
Write a function word_counts : Write a function word_counts that parses the string s into words and returns an array (of arrays of the form [word, count]) representing the frequency of each word. Words are case-insensitive:
Find how far do you go during the whole trip : Starting from rest, you move with a constant acceleration of 2.1 m/s2 for 15.0 s and then move with an acceleration of -2.1 m/s2 for another 15.0 s. How far do you go during the whole trip
What gain or loss must be recognized on the distribution : What gain or loss must be recognized on the distribution, and who must recognize it?
Calculate the new volume of the gas : An ideal gas occupies a volume of 2.85 m3 at STP. The pressure of the gas in increased to 3.71 atm. Calculate the new volume of the gas
What impact did early white immigrants into tennessee : What impact did early white "immigrants" into Tennessee have on the imperial wars in North America prior to and during the American War of Independence?

Reviews

Write a Review

Programming Languages Questions & Answers

  What is access control

What is access control? What is a data classification model? How is data classification different from a clearance leve?

  Create two global varibles to declare an empty array

Need to create two global varibles. The first currentTab, which will reference the current menu tab being dispalyed with a initial value of null, the second variable is maxZ which will store the z-index and it's initial value is 1.

  Program a robot to run mile-long steeplechase

Program a robot to run mile-long steeplechase where steeples are made from beepers instead of wall segmesnts. the robot must jumb the steeples in this race by picking the beepers that make up the steeples.

  Design a benefits election system

Individual project for this course is in form of Service Request from Huffman Trucking. It is essential for student to access Service Request: SR-ht-001. Design a Benefits Election System.

  Write procedure to scan string for first occurrence

Write procedure called Str_nextword to scan string for first occurrence of a certain delimiter character and replaces the delimiter with a null byte.

  Write a single line for body of constructor to create object

Create the constructor Orb(int xSpeed, int ySpeed). Write a single line for the body of the constructor,which creates a new Velocity object.

  Write down public static method using public interface

Using only public interface of UnorderedLinkedList class, write down a method: public static void reverse(UnorderedLinkedList staff) that reverses the entries in a linked list.

  Program using ten radio buttons to show five states

It is program using 10 Radio Buttons to show 5 States and 5 Capitals to user's interface. Also, there is text box to print "Correct" and "Incorrect" when Sate and the Capital is a correct match.

  Develop procedure to returns recent order information

Develop a procedure that returns the most recent order information for aparticular basket. This procedure must determine most recent stage entry from the BB_BASKETSTATUS table

  Write client-side javascript function to convert ampersands

Your job is to write a client-side JavaScript function that converts all ampersands in a form field to the text string " and " when the form loses focus on the textbox in question.

  Basic user interface-accept multi-level menu driven modules

Use any programming language or package to design basic User Interface which will be able to accept multi-level menu driven modules. Password should not be displayed.

  Write function is pseudocode to calculate area of triangle

Write down the function is pseudocode called calculate area which calculates the area of a triangle. Algorithm for area computation is this: area=1/2*base*height.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd