Skip to content

Instantly share code, notes, and snippets.

@deathbob
Created October 26, 2012 15:47
Show Gist options
  • Save deathbob/3959533 to your computer and use it in GitHub Desktop.
Save deathbob/3959533 to your computer and use it in GitHub Desktop.
short version
irb --noprompt ✹
input_string = %{Your program will be responsible for analyzing a small chunk of text, the text of this entire dailyprogrammer description. Your task is to output the distinct words in this description, from highest to lowest count with the number of occurrences for each. Punctuation will be considered as separate words where they are not a part of the word.
The following will be considered their own words: " . , : ; ! ? ( ) [ ] { }
For anything else, consider it as part of a word.
Extra Credit:
Process the text of the ebook Metamorphosis, by Franz Kafka and determine the top 10 most frequently used words and their counts. (This will help for part 2) }
input_string.split(' ').inject(Hash.new{|hash, key| hash[key] = 0}){|memo, x| memo[x] += 1; memo}.sort{|a, b| b[1] <=> a[1]}.map{|x| x[0]}.take(10)
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment