This is Ruby Language Problem Directions: Please complete the following assignme
ID: 3685354 • Letter: T
Question
This is Ruby Language Problem
Directions:
Please complete the following assignment to signify your completion of Unit 8. All programming projects need to be completed and submitted electronically, following the Electronic Submission Guidelines discussed in class.
Background:
The purpose of this assignment is to get practice working with hashes. Hashes are very powerful ways of keeping track of information. The programming project below will ask you to learn some of these new capabilities.
Project 1: 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 - *
Explanation / Answer
Hi below i have explained an did a sample work for your reference,
# Method name: item_counts
# Input: An arbitrary array
#
# Returns: A hash where every item is a key whose value is the number of times
# that item appears in the array
#
# Prints: Nothing
# Here are some examples:
# item_counts(["I", "am", "Sam", "I", "am"])
# # => {"I" => 2, "am" => 2, "Sam" => 1}
#
# item_counts([10, 20, 10, 20, 20])
# # => {10 => 2, 20 => 3}
#
# In short, item_counts(array) tells us how many times each item appears
# in the input array.
def item_counts(array)
counts = {} # Initialize counts to an empty Hash
array.each do |item|
# Add code here to modify the "counts" hash accordingly
# You'll need to handle two cases:
# 1. The first time we've seen a particular item in the array
# 2. The second-or-later time we've seen a particular item in the array
end
counts # This returns the "counts" hash
end
# "p" prints something to the screen in a way that's friendlier
# for debugging purposes than print or puts.
p item_counts([1,2,1,2,1]) == {1 => 3, 2 => 2}
p item_counts(["a","b","a","b","a","ZZZ"]) == {"a" => 3, "b" => 2, "ZZZ" => 1}
p item_counts([]) == {}
p item_counts(["hi", "hi", "hi"]) == {"hi" => 3}
p item_counts([true, nil, "dinosaur"]) == {true => 1, nil => 1, "dinosaur" => 1}
p item_counts(["a","a","A","A"]) == {"a" => 2, "A" => 2}
# Each of the lines above will print out "true" or "false" and collectively
# act as a sanity check. Remember that conceptually "x == y"
# means "are x and y equal?"
#
# That is, when you run the code, if any lines print out "false"
# then you know something is off in your code.
#
# This does *not* mean that your code is perfect if each line
# prints out "true."" For example,
# 1. We might have missed a corner case
# 2. The code does what it should, but is conceptually confused
# 3. Something else we haven't though of
Sample Run :
example output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.