Skip to content

Instantly share code, notes, and snippets.

@ymakino
Created May 27, 2012 12:39
Show Gist options
  • Save ymakino/2814068 to your computer and use it in GitHub Desktop.
Save ymakino/2814068 to your computer and use it in GitHub Desktop.
A simple Ruby program to convert horizontal text to vertical text.
#!/usr/bin/env ruby -Ku -w
def resize(a, blank, s)
num = s - a.size
return a if num==0
if num > 0
num.times { a.push(blank) }
else
num.times { a.pop }
end
return a
end
def fit_size(l, blank)
return [] if l.size == 0
s = l.map{ |v| v.length }.max
l.map { |a| resize(a, blank, s) }
end
def zip(l, blank)
return [] if l.size == 0
r = []
l = fit_size(l, blank)
l[0].size.times do |i|
v = []
l.size.times do |j|
v.push(l[j][i])
end
r.push(v)
end
r
end
def drop_tail(a, blank)
a.reverse.drop_while{|v| v==blank}.reverse
end
def trans(c)
c += " " if c.count(c) == 1
return c
end
def y2t(lines, blank, sep="")
ys = lines.reverse.map { |l| l.chomp.each_char.map { |c| trans(c) } }
ts = zip(ys, blank)
ts = ts.map{ |l| drop_tail(l, blank).join(sep) }
ts.join("\n")
end
puts y2t(STDIN.readlines, " ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment