Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program is written in Ruby language. 1. (8 Points) function #1: Ask the user to type 2 lines (e.g., before going to the next

This program is written in Ruby language.

image text in transcribedimage text in transcribed

1. (8 Points) function #1: Ask the user to type 2 lines (e.g., before going to the next line the user will hit the 'Enter' key, etc.) on keyboard, and your program should save the lines to a file named "savedFile.txt". 2. (8 Points) function #2: Ask the user to type the name of a file. If the file contains Java or Python, then print "The file is interesting". If the file contains Ruby" or "ruby", then print "The file is extraordinary". Otherwise, print "The file is so so". 3. (8 Points) function #3: Print the string The square of integer x is y" 100 times while substituting x by numbers from 1 to 100 whereas y is xa. 4. (16 Points) function #4: Let the user pick a number (say x) between 10 and 100. Now your program tosses a coin x times. In particular, your program can contain a loop and in each iteration it randomly makes a choice: head (represented by 0) or tail (represented by 1), and stores the outcome (0 or 1) in an array. After the iterations, traverse the array and count how many heads and tails were generated. Also, report the ratio of number of heads and number of tails. 5. (20 Points) function #5: Go to http://www.textfiles.com/stories/ and check that this site hosts multiple stories while each story is in a textfile. Download a textfile of your choice, which has atleast 1000 words, and save the file as story.txt. Your program needs to read this file and process it to collect some statistics. In particular, report the total number words in the story, the number of distinct words, the third- most frequent word and its frequency. Hints: You may use Array and Hash data structures as they are available in Ruby. You may design a regular expression to define a word. def function1 # Complete this function end def function2 # Complete this function end def function3 # Complete this function end def function4 # Complete this function end def function5 # Complete this function aFile = File.new("story.txt", "r") words = [] freq = Hash.new(0) aFile.each do |line| end end # Now call the functions as the following. function1 function2 function3 function4 function5 1. (8 Points) function #1: Ask the user to type 2 lines (e.g., before going to the next line the user will hit the 'Enter' key, etc.) on keyboard, and your program should save the lines to a file named "savedFile.txt". 2. (8 Points) function #2: Ask the user to type the name of a file. If the file contains Java or Python, then print "The file is interesting". If the file contains Ruby" or "ruby", then print "The file is extraordinary". Otherwise, print "The file is so so". 3. (8 Points) function #3: Print the string The square of integer x is y" 100 times while substituting x by numbers from 1 to 100 whereas y is xa. 4. (16 Points) function #4: Let the user pick a number (say x) between 10 and 100. Now your program tosses a coin x times. In particular, your program can contain a loop and in each iteration it randomly makes a choice: head (represented by 0) or tail (represented by 1), and stores the outcome (0 or 1) in an array. After the iterations, traverse the array and count how many heads and tails were generated. Also, report the ratio of number of heads and number of tails. 5. (20 Points) function #5: Go to http://www.textfiles.com/stories/ and check that this site hosts multiple stories while each story is in a textfile. Download a textfile of your choice, which has atleast 1000 words, and save the file as story.txt. Your program needs to read this file and process it to collect some statistics. In particular, report the total number words in the story, the number of distinct words, the third- most frequent word and its frequency. Hints: You may use Array and Hash data structures as they are available in Ruby. You may design a regular expression to define a word. def function1 # Complete this function end def function2 # Complete this function end def function3 # Complete this function end def function4 # Complete this function end def function5 # Complete this function aFile = File.new("story.txt", "r") words = [] freq = Hash.new(0) aFile.each do |line| end end # Now call the functions as the following. function1 function2 function3 function4 function5

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 2012 Proceedings Part 2 Lnai 7197

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284892, 978-3642284892

More Books

Students also viewed these Databases questions

Question

Graph each function over the interval-2 Answered: 1 week ago

Answered: 1 week ago