Created
December 24, 2017 20:04
-
-
Save theHamdiz/76d339da95bad159eec1bf8448e7d4b2 to your computer and use it in GitHub Desktop.
Ruby script to calculate the frequency of appearance of specific characters in a text, as well as mentioning the letters that were excluded by the author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts 'Provide the sentence you want to perform letter frequency on' | |
text = gets.chomp # gets the sentence to iterate | |
puts "Performing on: #{text[0..6]}..." | |
text.downcase! | |
freqs = {} # creating a hash to iterate 'text' | |
freqs.default = 0 | |
count = 0 # number of charachters in 'text' | |
# iterating through text | |
text.each_char do |char| | |
freqs[char] += 1 | |
count += 1 | |
end | |
unmentioned = [] | |
('a'..'z').each do |x| | |
if freqs[x].zero? | |
unmentioned << x | |
next | |
else | |
puts "#{x} : #{freqs[x]}" | |
end | |
end | |
# prints frequency of the characters in 'text' | |
puts "Your sentence consists of #{count} characters" | |
puts "Those letters were not mentioned #{unmentioned}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment