Skip to content

Instantly share code, notes, and snippets.

@zmcartor
Created April 26, 2012 01:27
Show Gist options
  • Save zmcartor/2495033 to your computer and use it in GitHub Desktop.
Save zmcartor/2495033 to your computer and use it in GitHub Desktop.
GettysBurg Estimate
lines = File.new('gettys').gets
lineno = 0
short = {0=>[]}
lines.split(' ').each do |word|
#count up the present line and include spaces. Take into account the current space count
#and the impact of adding the new word + 1 space.
if ( ( (short[lineno].inject(0){|sum, ele| sum+=ele.size} ) + (word.size+1 + (short[lineno].count-1)) ) <= 13)
short[lineno] = short[lineno].push word
else
lineno+=1
short[lineno] = []
short[lineno] = short[lineno].push word
end
end
p short
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment