Skip to content

Instantly share code, notes, and snippets.

@cody-code-wy
Last active May 25, 2016 17:18
Show Gist options
  • Save cody-code-wy/4ef09d19b0c55dbcd5fa11f52cae6d7f to your computer and use it in GitHub Desktop.
Save cody-code-wy/4ef09d19b0c55dbcd5fa11f52cae6d7f to your computer and use it in GitHub Desktop.
def count_letters(letter)
hash = Hash.new { |hash,key| hash[key] = [] }
letter.split("").each_with_index() { |x,i|
hash[x] << i
}
hash
end
puts count_letters("lighthouse in the house...")
@cody-code-wy
Copy link
Author

Output is:

{"l"=>[0], "i"=>[1, 11], "g"=>[2], "h"=>[3, 5, 15, 18], "t"=>[4, 14], "o"=>[6, 19], "u"=>[7, 20], "s"=>[8, 21], "e"=>[9, 16, 22], " "=>[10, 13, 17], "n"=>[12], "."=>[23, 24, 25]}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment