aboutsummaryrefslogtreecommitdiff
path: root/challenge-003
diff options
context:
space:
mode:
authorAbigail <abigail@abigail.be>2021-01-28 12:19:18 +0100
committerAbigail <abigail@abigail.be>2021-01-28 12:19:18 +0100
commit3a0ad850aa19fad89a422abe0e45989fe4448137 (patch)
tree9358f235febbb323d99f5e43308da54f9596a5d8 /challenge-003
parent7260520cd91bd98c143d5717f57ea0281852ac8f (diff)
downloadperlweeklychallenge-club-3a0ad850aa19fad89a422abe0e45989fe4448137.tar.gz
perlweeklychallenge-club-3a0ad850aa19fad89a422abe0e45989fe4448137.tar.bz2
perlweeklychallenge-club-3a0ad850aa19fad89a422abe0e45989fe4448137.zip
Lua solution for week 3, part 2
Diffstat (limited to 'challenge-003')
-rw-r--r--challenge-003/abigail/README.md1
-rw-r--r--challenge-003/abigail/lua/ch-2.lua49
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md
index a8af310c3b..4ceda5bbbf 100644
--- a/challenge-003/abigail/README.md
+++ b/challenge-003/abigail/README.md
@@ -39,4 +39,5 @@ rows from the command line. The Pascal Triangle should have at least
### Solutions
* [AWK](awk/ch-2.awk)
* [C](c/ch-2.c)
+* [Lua](lua/ch-2.lua)
* [Perl](perl/ch-2.pl)
diff --git a/challenge-003/abigail/lua/ch-2.lua b/challenge-003/abigail/lua/ch-2.lua
new file mode 100644
index 0000000000..321d7d25d1
--- /dev/null
+++ b/challenge-003/abigail/lua/ch-2.lua
@@ -0,0 +1,49 @@
+#!/opt/local/bin/lua
+
+--
+-- See ../README.md
+--
+
+--
+-- Run as: lua ch-2.lua < input-file
+--
+
+--
+-- Create and print row 0
+--
+local row = {1}
+print "1"
+
+--
+-- Iterate over the input
+--
+for line in io . lines () do
+ local rows = tonumber (line)
+ 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