Created
February 4, 2012 12:19
-
-
Save mathie/1737500 to your computer and use it in GitHub Desktop.
Given a string, tell me how many of each character I'll need. Surprisingly useful when trying to typeset a sentence and check to see if you've enough type!
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
#!/usr/bin/env ruby | |
puts "Gies a phrase" | |
str = STDIN.readline.chomp | |
characters = Hash.new(0) | |
str.each_char do |char| | |
characters[char] += 1 | |
end | |
characters.sort_by { |k, v| v }.reverse.each do |key, val| | |
puts "#{key}: #{val}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just because I'm in a pub and therefore can't resist evil things like inject and chaining enumerable methods together...