Skip to content

Instantly share code, notes, and snippets.

@heavymetta
Created September 3, 2015 14:50
Show Gist options
  • Save heavymetta/98b5aba2ed450e2626a5 to your computer and use it in GitHub Desktop.
Save heavymetta/98b5aba2ed450e2626a5 to your computer and use it in GitHub Desktop.
Character.Counting
def count_letters(list)
variable = Hash.new
list.each do |word|
word.split('').each do |letter|
variable[letter] = variable[letter].nil? ? 1 : variable[letter] + 1
end
end
variable
end
def count_index(list)
# we want to count where in the index the letter shows up
# {"l" => [0, 16]}
""
value = Hash.new
list.each do |word|
word.split('').each_with_index do |item, index|
if item != ' '
if value.has_key?(item)
value[item].push(index)
else
value[item] = [index]
end
end
end
end
value
end
puts count_letters(["lighthouse in the house..."])
puts count_index(["lighthouse in the house..."])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment