Created
September 3, 2015 14:50
-
-
Save heavymetta/98b5aba2ed450e2626a5 to your computer and use it in GitHub Desktop.
Character.Counting
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
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