Last active
April 27, 2016 17:39
-
-
Save gbrl/a0757460cad0f7baa0e9c4fdb3eb98dd to your computer and use it in GitHub Desktop.
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
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