Question

In: Computer Science

Must be done in Ruby Histogram Create a Ruby program which reads a string of information...

Must be done in Ruby

Histogram
Create a Ruby program which reads a string of information and uses a Hash to keep track of letter frequencies and then displays a Histogram using this frequency information.

IN ORDER TO RECEIVE FULL CREDIT, YOUR CODE SHOULD PROPERLY PROCESS THE STRING INFORMATION, COUNTING FREQUENCIES BY USING A HASH.

A number of different program dialogues describe the program I am looking for.

Enter Data: supercalifragilisticexpialadocious
A - 4 - ****
C - 3 - ***
D - 1 - *
E - 2 - **
F - 1 - *
G - 1 - *
I - 6 - ******
L - 3 - ***
O - 2 - **
P - 2 - **
R - 2 - **
S - 3 - ***
T - 1 - *
U - 2 - **
X - 1 - *

Solutions

Expert Solution

Kindly find the program as below:

print "Enter Data : "

x = gets.chomp # used to get the input

h = Hash.new #creating Hash

# loop through each character and counting frequencies of them

x.each_char { |w|

if (w != ' ')

if h.has_key?(w)

h[w] = h[w] + 1

else

h[w] = 1

end

end

}

# sorting as per alphabet order and displaying the histogram as required

h.sort{|a,b| a[0]<=>b[0]}.each { |elem|

str =""

$i=1

begin

str = str + "*"

$i +=1

end until $i >elem[1]

puts "\"#{elem[0].capitalize}\" - #{elem[1]} - #{str}"

} # end of program

Find the screenshot s of the code

Output :


Related Solutions

Ruby programming Create a Ruby program which reads numbers, deposits them into an array and then...
Ruby programming Create a Ruby program which reads numbers, deposits them into an array and then calculates the arithmetic mean (otherwise known as the average), the median (that is, the number that splits the set of value in half with half being above and half being below the number), and the standard deviation (that is, the average of distance each number is from the mean then squared). Further information about these calculations can be found here: average: http://en.wikipedia.org/wiki/Average (add up...
Instructions: Create a Java program that reads a string entered by the user and then determines...
Instructions: Create a Java program that reads a string entered by the user and then determines and prints how many of each lowercase vowel (a, e, i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also, count and print the number of non-vowel characters. Example: User enters: "This house is beautiful." a: 1 e: 2 i: 3 o: 1 u: 2 non-vowel:10
Write a C++ program which reads a string, less than 10 characters long. This string represents...
Write a C++ program which reads a string, less than 10 characters long. This string represents an integer expressed in roman numbers. Let a function convert the number from roman to arabic form (i.e., our standard digits). Let then the main program writes out both forms. The roman numbers are written according to: M = 1000, D = 500, C =100, L=50, X=10, V=5, I=1. Examples: LXXXVII = 87 CCXIX = 219 MCCCLIV = 1354 MMDCLXXIII = 2673
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases...
MUST BE DONE IN C (NOT C++) Your create a program that can implement the cases in which the initial unit is Fahrenheit or something not recognizable. Your program should incorporate Fahrenheit to Celsius, Fahrenheit to Kelvin and unknown initial units (display an error message for this last one). You must use functions to calculate Fahrenheit degrees.
in. java Write a program that reads a string from the user, and creates another string...
in. java Write a program that reads a string from the user, and creates another string with the letters from in reversed order. You should do this using string concatenation and loops. Do not use any method from Java that does the work for you. The reverse string must be built. Do not just print the letters in reversed order. Instead, concatenate them in a string. --- Sample run: This program will create a string with letters in reversed order....
Create program which sorts letters of a string based on ASCII value. The program will then...
Create program which sorts letters of a string based on ASCII value. The program will then print the sorted string to stdout. Use C programming language. - Only use stdio.h - Input prompt should say "Enter string of your choice: " - Remove any newline \n from input string - Implement sorting operation as a function. Should use selection sort algorithm, but you may use a different algorithm - Output should print sorted string on new line Example:     Enter...
Create a complete java program called Week_Report. The program must include two array structures, a string...
Create a complete java program called Week_Report. The program must include two array structures, a string array called DaysOfWeek and a double array called Temp_Values. Store in the DaysOfWeek array the following values (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday). Store in the Temp_Values array the following (23.5, 34.0, 20.9, 45.7, 29.3, 34.5, 32.5). Using a for loop structure output the values for the two arrays. Day of the Week Temperature Values Monday 23.5 Tuesday 34.0 Wednesday 20.9 Thursday 45.7...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
In c++, write a program that reads a string consisting of a positive integer or a...
In c++, write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format.
Write a C++ program that reads a string from a text file and determines if the...
Write a C++ program that reads a string from a text file and determines if the string is a palindrome or not using stacks and queue
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT