aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/paulo-custodio/python/ch-2.py
blob: d97750c634b55c7a1d10592768a7e059bbee74d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/env python

# Challenge 003
#
# Challenge #2
# Create a script that generates Pascal Triangle. Accept number of rows from
# the command line. The Pascal Triangle should have at least 3 rows. For more
# information about Pascal Triangle, check this wikipedia page.

import sys

def draw_pascal(rows):
    data = [1]
    for row in range(1,rows+1):
        # print current row
        print(" "*(rows-row) + " ".join([str(x) for x in data]))

        # compute next row
        nxt = [1]
        for col in range(0, len(data)-1):
            nxt.append(data[col] + data[col+1])
        nxt.append(1)
        data = nxt

# main
draw_pascal(int(sys.argv[1]))