Skip to content

Instantly share code, notes, and snippets.

@charmainetham
Created April 28, 2016 02:55
Show Gist options
  • Save charmainetham/e7ba302be293ef924ba7adbabbb7c74a to your computer and use it in GitHub Desktop.
Save charmainetham/e7ba302be293ef924ba7adbabbb7c74a to your computer and use it in GitHub Desktop.
Character counting
#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