Created
May 27, 2012 12:39
-
-
Save ymakino/2814068 to your computer and use it in GitHub Desktop.
A simple Ruby program to convert horizontal text to vertical text.
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
#!/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