Last active
July 20, 2016 19:04
-
-
Save kaiguogit/9648659b505b2fdf95adad9748a2213e to your computer and use it in GitHub Desktop.
character counting
This file contains hidden or 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(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