Skip to content

Instantly share code, notes, and snippets.

@itsbth
Created March 27, 2014 16:35
Show Gist options
  • Save itsbth/9811873 to your computer and use it in GitHub Desktop.
Save itsbth/9811873 to your computer and use it in GitHub Desktop.
def diff(old, new)
old.each_index{|idx|
if old[idx].hash != new[idx].hash
puts ">>> #{idx}: #{old[idx]}"
puts "<<< #{idx}: #{new[idx]}"
end
}
if old.length < new.length
puts "<<< #{new.length - 1}: #{new.last}"
end
puts
end
def top_3_words(text)
words = []
top = {1 => 0}
text.scan /\b(?:[[:alpha:]]|')+\b/i do |word|
word.downcase!
old = words.map(&:clone)
if idx = words.find_index{|h|h[:word] == word}
words[idx][:count] += 1
if idx == 0
top[words[idx][:count] - 1] += 1
top[words[idx][:count]] = idx
elsif words[idx - 1][:count] < words[idx][:count]
np = top[words[idx][:count] - 1]
words[idx], words[np] = words[np], words[idx]
top[words[np][:count] - 1] += 1
top[words[np][:count]] ||= np
else
top[words[idx][:count] - 1] += 1
top[words[idx][:count]] = idx
end
else
words.push(:word => word, :count => 1)
end
end
words.first(3).map{|w|w[:word]}
end
p top_3_words("In a village of La Mancha, the name of which I have no desire to call to
mind, there lived not long since one of those gentlemen that keep a lance
in the lance-rack, an old buckler, a lean hack, and a greyhound for
coursing. An olla of rather more beef than mutton, a salad on most
nights, scraps on Saturdays, lentils on Fridays, and a pigeon or so extra
on Sundays, made away with three-quarters of his income.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment