Skip to content

Instantly share code, notes, and snippets.

@RobertCam
Created September 30, 2015 22:09
Show Gist options
  • Save RobertCam/93ddf80536d2768fb889 to your computer and use it in GitHub Desktop.
Save RobertCam/93ddf80536d2768fb889 to your computer and use it in GitHub Desktop.
Character Counting
def char_count(list)
letters = {}
list.each do |word|
word.split('').each { |letter| letters.has_key?(letter) ? letters[letter] += 1 : letters[letter] =1}
end
letters = letters.sort_by {|a,b| b}
letters.reverse!
print letters
end
def letter_index(sentence)
indices = Hash.new {|k, i| k[i] = []}
sentence.split('').each_with_index do |letter, index|
indices[letter].push(index)
end
indices
end
puts char_count(['apples', 'oranges', 'hipsters', 'are', 'same'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment