diff options
| author | 冯昶 <seaker@qq.com> | 2021-03-15 18:13:51 +0800 |
|---|---|---|
| committer | 冯昶 <seaker@qq.com> | 2021-03-15 18:13:51 +0800 |
| commit | 8b6be37fe4dac8b4c6489a95e55514b76b298d15 (patch) | |
| tree | ae36c8ec2c71f606c0e36adaa19dba366a68a0b4 /challenge-003/abigail/python/ch-2.py | |
| parent | 865acfd056fb6f409ec6b1a81d60b931cbcb69fe (diff) | |
| parent | c9aec2da6bcb04b488183f09ca94bee488557aff (diff) | |
| download | perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.gz perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.tar.bz2 perlweeklychallenge-club-8b6be37fe4dac8b4c6489a95e55514b76b298d15.zip | |
Merge branch 'master' of github.com:seaker/perlweeklychallenge-club
Diffstat (limited to 'challenge-003/abigail/python/ch-2.py')
| -rw-r--r-- | challenge-003/abigail/python/ch-2.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-003/abigail/python/ch-2.py b/challenge-003/abigail/python/ch-2.py new file mode 100644 index 0000000000..9370561d8b --- /dev/null +++ b/challenge-003/abigail/python/ch-2.py @@ -0,0 +1,46 @@ +#!/opt/local/bin/python + +# +# See ../READ.md +# + +# +# Run as python ch-2.py < input-file +# + +import fileinput +import sys + +# +# Iterate over the input +# +for line in fileinput . input (): + rows = int (line) + + # + # Create the first row, and print it + # + row = [1] + sys . stdout . write (str (1) + "\n") + + for r in range (1, rows + 1): + # + # Create a new row + # + new = [None] * (r + 1) # In Python, arrays don't grow automatically + for i in range (r + 1): + sum = 0 + if i > 0: + sum = row [i - 1] + sys . stdout . write (" ") + if i < r: + sum = sum + row [i] + new [i] = sum + sys . stdout . write (str (sum)) + sys . stdout . write ("\n") + + # + # New row becomes current row + # + row = new + |
