Skip to content

Instantly share code, notes, and snippets.

@gbrl
Last active April 27, 2016 17:39
Show Gist options
  • Save gbrl/a0757460cad0f7baa0e9c4fdb3eb98dd to your computer and use it in GitHub Desktop.
Save gbrl/a0757460cad0f7baa0e9c4fdb3eb98dd to your computer and use it in GitHub Desktop.
require 'pp'
def count_letters(stuff)
result = {}
individual_characters = []
individual_characters = stuff.split("")
individual_characters.each do |char|
next if char == " "
if result[char].nil?
result[char] = 1
else
result[char] = result[char] + 1
end
end
result
end
pp count_letters("lighthouse in the house...")
def count_letters_and_indices(stuff)
result = {}
individual_characters = []
individual_characters = stuff.split("")
individual_characters.each_with_index do |char,index|
next if char == " "
result[char] = [] if result[char].nil?
result[char] << index
end
result
end
pp count_letters_and_indices("lighthouse in the house...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment