Skip to content

Instantly share code, notes, and snippets.

@FioFiyo
Created March 31, 2016 16:16
Show Gist options
  • Save FioFiyo/e89ac11856460b823b829633d4f3e0f3 to your computer and use it in GitHub Desktop.
Save FioFiyo/e89ac11856460b823b829633d4f3e0f3 to your computer and use it in GitHub Desktop.
#COUNT LETTERS
def count_letters(sentence)
letters = {}
sentence = [sentence]
sentence.each do |words|
word = words.split('')
word.each do |letter|
# #if the array has letter then count an extra value for that letter
if letters[letter]
letters[letter] += 1
#Add the index in the array for new letter
else
letters[letter] = 1
end
end
word.each_with_index do |letter,index|
puts "Letter: #{letter} index: #{index} count: #{letters[letter]}"
end
end
#puts letters
end
count_letters('Lighthouse in the house...')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment