aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-28 13:56:05 +0100
committerAbigail <abigail@abigail.be>2021-01-28 13:56:05 +0100
commiteda4318efda4548b503c49d8c297047100a5bc27 (patch)
tree7f31f5a53d64ca6fd9a6f1b77edd7c7059ca7c4e /challenge-003/abigail
parent62ad2b057e0439e47759711b6fc7dfcffcecf348 (diff)
downloadperlweeklychallenge-club-eda4318efda4548b503c49d8c297047100a5bc27.tar.gz
perlweeklychallenge-club-eda4318efda4548b503c49d8c297047100a5bc27.tar.bz2
perlweeklychallenge-club-eda4318efda4548b503c49d8c297047100a5bc27.zip
Python solution for week 3, part 2
Diffstat (limited to 'challenge-003/abigail')
-rw-r--r--challenge-003/abigail/README.md1
-rw-r--r--challenge-003/abigail/python/ch-2.py46
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md
index f0b9e8222f..0a87821699 100644
--- a/challenge-003/abigail/README.md
+++ b/challenge-003/abigail/README.md
@@ -43,3 +43,4 @@ rows from the command line. The Pascal Triangle should have at least
* [Lua](lua/ch-2.lua)
* [Node.js](node/ch-2.js)
* [Perl](perl/ch-2.pl)
+* [Python](python/ch-2.py)
diff --git a/challenge-003/abigail/python/ch-2.py b/challenge-003/abigail/python/ch-2.py
new file mode 100644
index 0000000000..fe5b1aa5e3
--- /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-1.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
+