Skip to content

Instantly share code, notes, and snippets.

@iturgeon
Last active February 8, 2017 19:31
Show Gist options
  • Save iturgeon/3dc366ebb23f665bf98dd055e7a3d029 to your computer and use it in GitHub Desktop.
Save iturgeon/3dc366ebb23f665bf98dd055e7a3d029 to your computer and use it in GitHub Desktop.
Whiteboard wednesday - print stairs
# 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