Last active
October 5, 2017 01:21
-
-
Save rdeva31/cf6b8fd1bf7bbd5bd900d5401ffcffcc 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
#!/usr/bin/ruby | |
# | |
# Vertical print | |
# | |
# 1. Print a word vertically. e.g. "one" should print as: | |
# | |
# o | |
# n | |
# e | |
# | |
# 2. Given a list of words, print them vertically side by side, e.g. ["one", "two" .... "eight", "nine", "ten"] should print as | |
# | |
# o t t f f s s e n t | |
# n w h o i i e i i e | |
# e o r u v x v g n n | |
# . . e r e . e h e . | |
# . . e . . . n t . . | |
# | |
# (added dots for visual distinction--not required) | |
# | |
# Sample impl: | |
# | |
WORDS = %w{one two three four five six seven eight nine ten} | |
DEFAULT_CHAR = ' ' | |
def naive(words) | |
longest_word = words.map {|w| w.length}.max | |
longest_word.times { |i| | |
words.each { |w| | |
print %Q[#{i < w.length ? w[i] : DEFAULT_CHAR} ] | |
} | |
puts | |
} | |
end | |
def slightly_less_naive(words) | |
index = 0 | |
while true do | |
printed = false | |
words.each do |w| | |
print "#{w[index] or DEFAULT_CHAR} " | |
printed |= !w[index].nil? | |
end | |
index += 1 | |
puts | |
break if not printed | |
end | |
end | |
naive(WORDS) | |
puts "--" * WORDS.length | |
slightly_less_naive(WORDS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment