Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

l. (8 Points) function #1: Ask the user to type 4 lines (e.g., before going to the next line the user will hit the Enter

image text in transcribed
image text in transcribed
l. (8 Points) function #1: Ask the user to type 4 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 "myFile.txt" 2. (8 Points) function #2: Ask the user to type the name of a file. If the file contains "Java" or "Javascript". then print "The file is interesting. If the file contains "Ruby" or "ruby", then print "The file is awesome Otherwise, print "The file is ok". 3. (8 Points) function #3: Print the string "The cube of integer x is y" 100 times while substituting x by numbers from 2 to 101 while y is x3. (16 Points) function #4: Let theuser pick a number (say x) between 50 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 1) or tail (represented by 0), 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. 4 5. (20 Points) function #5: Go to http://www.textfiles.com/stories/ and check that this site l 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. Disclaimer: we did not really check whether this website contains any improper story or language. If you find something improper please ignore this site and use some other source 1 def function1 # Complete this function 4 end 6 def function2 # Complete this function 9 end 10 11 def function3 12.| # Complete this function 13 14 end 15 16 def function4 171 # complete this function 18 19 end 20 21 def function5 22 # Complete this function 23 aFile File.new("story.txt", "r") 24 wo rds= [] 25 freqHash.new (0) 26 aFile.each do |line | 27 28 29 end 30 31 32 end 34 # Now call the functions as the following. 35 function1 36 function2 37 function3 38 function4 39 function5 40 l. (8 Points) function #1: Ask the user to type 4 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 "myFile.txt" 2. (8 Points) function #2: Ask the user to type the name of a file. If the file contains "Java" or "Javascript". then print "The file is interesting. If the file contains "Ruby" or "ruby", then print "The file is awesome Otherwise, print "The file is ok". 3. (8 Points) function #3: Print the string "The cube of integer x is y" 100 times while substituting x by numbers from 2 to 101 while y is x3. (16 Points) function #4: Let theuser pick a number (say x) between 50 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 1) or tail (represented by 0), 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. 4 5. (20 Points) function #5: Go to http://www.textfiles.com/stories/ and check that this site l 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. Disclaimer: we did not really check whether this website contains any improper story or language. If you find something improper please ignore this site and use some other source 1 def function1 # Complete this function 4 end 6 def function2 # Complete this function 9 end 10 11 def function3 12.| # Complete this function 13 14 end 15 16 def function4 171 # complete this function 18 19 end 20 21 def function5 22 # Complete this function 23 aFile File.new("story.txt", "r") 24 wo rds= [] 25 freqHash.new (0) 26 aFile.each do |line | 27 28 29 end 30 31 32 end 34 # Now call the functions as the following. 35 function1 36 function2 37 function3 38 function4 39 function5 40

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

Students also viewed these Databases questions