Last active
February 8, 2017 19:31
-
-
Save iturgeon/3dc366ebb23f665bf98dd055e7a3d029 to your computer and use it in GitHub Desktop.
Whiteboard wednesday - print stairs
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
# n | 0 1 2 3 4 5 | |
# i |____________ | |
# 1 | _ _ _ _ _ # | |
# 2 | _ _ _ _ # # | |
# 3 | _ _ _ # # # | |
# 4 | _ _ # # # # | |
# 5 | _ # # # # # | |
# 6 | # # # # # # | |
def print_stairs(n) | |
for i in 1..n | |
spaces = " " * (n - i) | |
pounds = "#" * i | |
p spaces + pounds | |
end | |
end | |
def print_stairs_golf(n) | |
(1..n).each { |i| p " " * (n - i) + "#" * i } | |
end | |
print_stairs(5) | |
print_stairs_golf(5) | |
# Let's try overloading operators for the hell of it | |
class Stairs | |
def *(n) | |
rows = (1..n).collect { |i| " "*(n - i)+"#"*i } | |
rows.join("\n") | |
end | |
end | |
stairs = Stairs.new | |
puts stairs * 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment