Last active
June 23, 2016 00:24
-
-
Save Leejojo/1c960706d56a20b5d72eff6053454cc7 to your computer and use it in GitHub Desktop.
character_counting
This file contains 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
def count_letters | |
puts "Tell me something" | |
phrase = gets.chomp | |
result = phrase.scan(/\w/) | |
hash = Hash.new(0) | |
result.inject(hash) {|key, value| | |
hash[value] += 1 | |
} | |
p hash | |
# puts phrase.scan(/\w/).inject(Hash.new(0)){|k, v| k[v] += 1; k} | |
end | |
count_letters | |
=begin | |
phrase.scan will scan each character in the phrase(aka string) | |
(/\w/) will look for any alphanumeric character | |
.inject returns the characters into a new hash | |
=end | |
def find_indices | |
puts "Tell me" | |
input = gets.chomp | |
result = input.scan(/\w/) | |
hash = Hash.new | |
result.each_with_index do |char, index| | |
hash[char] = index | |
end | |
p hash | |
end | |
find_indices |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment