In my code below are print instructions to help show how this works. I run it in an IPython laptop. My conclusion is as follows:
[1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1]
Besides the fact that the output looks as it should, the calculation method actually follows the original definition of the Pascal Triangle, where each line is built from the one that was above. (Some methods use combinatorics / factorials that can give the correct answer, but do not follow the original spirit of Pascal Triangle, afaik.)
Here is the code:
def MakeTriangle(numberOfRows): """the print statements are not required. They are just to help see how it works.""" triangle=[[1]]
To begin to understand how this works, pay attention to the following simple cases of the above zip and map functions:
x = [1, 2, 3] y = [4, 5, 6] zipped = zip(x, y) print zipped
Using the print statements in the MakeTriangle function, you get this output to help you learn how the code works:
triangle[-1] is [1] zipperd: [(0, 1), (1, 0)] triangle[-1] is [1, 1] zipperd: [(0, 1), (1, 1), (1, 0)] triangle[-1] is [1, 2, 1] zipperd: [(0, 1), (1, 2), (2, 1), (1, 0)] triangle[-1] is [1, 3, 3, 1] zipperd: [(0, 1), (1, 3), (3, 3), (3, 1), (1, 0)] triangle[-1] is [1, 4, 6, 4, 1]
Read the docs below looking at this conclusion and this will start to make sense:
If this still doesn't make sense, stick with these print statements in a for MakeTriangle loop:
print "[0] + triangle[-1] is ", [0] + triangle[-1] print "triangle[-1] + [0] is ", triangle[-1] + [0]
Note that aligned (centered) printing is done using the examples from http://docs.python.org/2/library/string.html#format-examples .
See also fooobar.com/questions/1314106 / ...