Created
March 1, 2010 15:11
-
-
Save onsails/318444 to your computer and use it in GitHub Desktop.
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
# encoding utf-8 | |
ROWS = 2 | |
words = [] | |
maxlength = 0 | |
File.open("words.txt", "r") do |f| | |
f.each_line do |l| | |
words << ( l =~ /^\d+$/ ? l.to_i : l.chop ) | |
maxlength = l.length if maxlength < l.length | |
end | |
end | |
wpc = (words.length / ROWS.to_f).ceil # кол-во слов в колонке | |
words.sort! do |a,b| | |
if a.class != b.class | |
a = a.to_s | |
b = b.to_s | |
end | |
if a == b | |
0 | |
else | |
a > b ? 1 : -1 | |
end | |
end | |
columns = [] | |
ROWS.times do |i| | |
columns << words[i*wpc..wpc*(i+1)-1] | |
end | |
while (blanks = columns[0].count - columns[ROWS-1].count) > 1 | |
(ROWS-blanks).upto ROWS-2 do |i| | |
columns[i + 1].unshift columns[i].pop | |
end | |
end | |
for i in columns.first | |
columns.each do |column| | |
word = column[i - 1] | |
print word if word | |
(maxlength - word.to_s.length + 2).times { print " " } | |
end | |
puts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment