Skip to content

Instantly share code, notes, and snippets.

@deathbob
Created October 26, 2012 15:46
Show Gist options
  • Save deathbob/3959523 to your computer and use it in GitHub Desktop.
Save deathbob/3959523 to your computer and use it in GitHub Desktop.
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) };nil
array_from_input_string_split_on_spaces = input_string.split(' ');nil
hash_with_default_value_for_missing_keys = Hash.new{|hash, key| hash[key] = 0};nil
hash_of_words_and_counts = array_from_input_string_split_on_spaces.inject(hash_with_default_value_for_missing_keys){ |hash, word| hash[word] += 1; hash };nil
# when you use some methods from the Enumerable module on a Hash, it coerces the hash into an array of two dimensional Arrays.
# For example
foo = {:c => 1, :a => 9, :b => 3}
foo.to_a
foo.sort
foo.sort{|a, b| a[1] <=> b[1]}
# `sort` does this also.
# Effectively, the following line is converting the hash to an array of two dimensional arrays, and sorting it on the second dimension of each one of the little 2-d arrays inside the main array.
# (array indexing starts at zero, so 1 is the second thing in an array)
sorted_array = hash_of_words_and_counts.sort{|a, b| b[1] <=> a[1]};nil
top_ten = sorted_array.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