aboutsummaryrefslogtreecommitdiff
path: root/challenge-003/abigail/lua/ch-2.lua
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2021-02-22 17:45:14 +0800
committer冯昶 <fengchang@novel-supertv.com>2021-02-22 17:45:14 +0800
commit572994875f5972916deabeb84f3648ac4640107b (patch)
tree1458cfc22c7b80dd025415f73e7c808814bb0db7 /challenge-003/abigail/lua/ch-2.lua
parent7021a6ee78be5c0a1bc1a30db583004a88fc7a15 (diff)
parent2c26164a5a90aa14a19078d845769d3ec9fbb5ae (diff)
downloadperlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.tar.gz
perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.tar.bz2
perlweeklychallenge-club-572994875f5972916deabeb84f3648ac4640107b.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-003/abigail/lua/ch-2.lua')
-rw-r--r--challenge-003/abigail/lua/ch-2.lua51
1 files changed, 51 insertions, 0 deletions
diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..0e30b0a9c6
--- /dev/null
+++ b/challenge-003/abigail/lua/ch-2.lua
@@ -0,0 +1,51 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+
+--
+-- Iterate over the input
+--
+for line in io . lines () do
+ local rows = tonumber (line)
+
+ --
+ -- Create and print row 0
+ --
+ local row = {1}
+ print "1"
+
+ local r
+ for r = 1, rows do
+ --
+ -- Create a new row
+ --
+ local new = {}
+ for i = 1, r + 1 do
+ sum = 0;
+ if i > 1
+ then sum = sum + row [i - 1]
+ io . write (" ")
+ end
+ if i <= r
+ then sum = sum + row [i]
+ end
+ --
+ -- Assign new element, and print it
+ --
+ new [i] = sum
+ io . write (sum)
+ end
+ io . write ("\n")
+ --
+ -- New row becomes current row
+ --
+ row = new
+ end
+end