Created
December 4, 2016 11:36
-
-
Save kirill-fedyanin/63297de764d75ef0954291c5f1d078b9 to your computer and use it in GitHub Desktop.
This file contains 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
def justify(text, width) | |
text.split.reduce([[]]) do |lines, word| | |
line = lines.last | |
if (spaces = width - line.join(' ').length) > word.length | |
line << word | |
else | |
spaces.times { |i| line[i % (line.count - 1)] += ' ' } if line.count > 1 | |
lines << [word] | |
end | |
lines | |
end.map { |line| line.join(' ') }.join("\n") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment