Last active
December 12, 2015 07:18
-
-
Save jaydonnell/4735159 to your computer and use it in GitHub Desktop.
ruby code
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
# let's count words | |
a = ['the quick brown fox jumped over the log', 'the slow brown fox ran fast', 'the blue fast cat jumped'] | |
a.reduce({}) { |m, o| | |
o.split(' ').each{ |w| | |
m.key?(w) ? m[w] = m[w]+ 1 : m[w] = 1 | |
} | |
m | |
}.sort { |a,b| b[1] <=> a[1] } | |
# => [["the", 4], ["brown", 2], ["fast", 2], ["jumped", 2], ["fox", 2], ["log", 1], ["slow", 1], ["ran", 1], ["over", 1], ["cat", 1], ["blue", 1], ["quick", 1]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment