Created
March 15, 2014 06:03
-
-
Save djrodgerspryor/9562433 to your computer and use it in GitHub Desktop.
Quick, recursive generation of rows from Pascal's triangle.
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
def pascalRow(n): | |
" Returns the nth row of Pascal's triangle" | |
return [1] if n<=0 else reduce(lambda row, n: row[:-1] + [(row[-1] + n), n], pascalRow(n-1), [0]) | |
if __name__ == '__main__': # Module Test/Demo | |
print pascalRow(12) | |
print '\n'.join([str(n) + ' : ' + str(pascalRow(n)) for n in xrange(10)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment