Skip to content

Instantly share code, notes, and snippets.

@onsails
Created March 1, 2010 15:11
Show Gist options
  • Save onsails/318444 to your computer and use it in GitHub Desktop.
Save onsails/318444 to your computer and use it in GitHub Desktop.
# 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