Created
April 26, 2012 01:27
-
-
Save zmcartor/2495033 to your computer and use it in GitHub Desktop.
GettysBurg Estimate
This file contains hidden or 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
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