Skip to content

Instantly share code, notes, and snippets.

@kaiguogit
Last active July 20, 2016 19:04
Show Gist options
  • Save kaiguogit/9648659b505b2fdf95adad9748a2213e to your computer and use it in GitHub Desktop.
Save kaiguogit/9648659b505b2fdf95adad9748a2213e to your computer and use it in GitHub Desktop.
character counting
# def count_letters(str)
# count={}
# str.strip.each_char do |char|
# count[char] = 0 unless count.include?(char)
# count[char] += 1
# end
# count
# end
def count_letters(str)
location={}
str.strip.split('').each_with_index do |char, index|
location[char] = [] unless location.include?(char)
location[char] << index
end
location
end
puts count_letters("lighthouse in the house...").inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment