Created
April 28, 2016 02:55
-
-
Save charmainetham/e7ba302be293ef924ba7adbabbb7c74a 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
#Counting the number of letters | |
def count_letters(words) | |
count = Hash.new(0) | |
words.gsub(" ","").split('').each do |letters| | |
count[letters] += 1 | |
end | |
count | |
end | |
puts count_letters("lighthouse in the house...") | |
#Returning indices | |
def indices_letters(word) | |
indices = Hash.new{|hash, key| hash[key] = [] } | |
word.downcase.gsub(" ","").split('').each_with_index do |char, index| | |
indices[char] << index | |
end | |
indices | |
end | |
puts indices_letters(" Lighthouse in the house ..") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment