Last active
April 9, 2016 12:25
-
-
Save rossta/f1576f6e944cdcfd0876907ccd4ae314 to your computer and use it in GitHub Desktop.
Solution to Ancient City Ruby 2016
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
# I gave a take about Enumerator at ACR so naturally | |
# I had to solve the Snake Case challenge using one. | |
module SnakeCase | |
module_function | |
# Returns the number of paths for a | |
# grid given by m x n blocks | |
# | |
# @param m [Fixnum] width | |
# @param n [Fixnum] length | |
# @return [Fixnum] number of paths | |
def paths(m, n) | |
pascal.take(m+n)[-1][n] | |
end | |
# Returns a sequence of arrays representing | |
# pascals triangle where the values in each | |
# "row" represent the sum of adjacent row values | |
# in the preceding row | |
# | |
# @return [Array] an array of arrays | |
def pascal(rows = [1]) | |
Enumerator.new do |y| | |
loop { y.yield(rows = ([0] + rows).zip(rows + [0]).map { |a, b| a + b }) } | |
end | |
end | |
end | |
SnakeCase.paths(10, 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment