Created
March 16, 2014 02:36
-
-
Save tom-galvin/9577764 to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge #??? Solution (Pascal's Pyramid)
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
# reddit.com/r/DailyProgrammer | |
# Solution to Challenge #???: Pascal's Pyramid | |
def fac(n) | |
n < 2 ? 1 : n * fac(n - 1) | |
end | |
def tri(n) | |
(0..n).to_a | |
.map {|x| fac(n) / (fac(x) * fac(n - x))} | |
end | |
def pym(n) | |
tri(n).to_a | |
.map.with_index {|x, i| tri(i).map {|y| y * x}} | |
end | |
# most code after this is to make it look pretty | |
size = gets.chomp.to_i | |
layer = pym(size - 1) | |
largest = layer.flatten.reduce {|x, y| x > y ? x : y }.to_s.length | |
puts layer | |
.map {|a| a.map {|n| n.to_s.center(largest, ' ')}.join(' ').center(size * largest + size - 1, ' ') } | |
.join "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment