From 94066ce3eb7bbfcf7da711e6e9f703df791a48de Mon Sep 17 00:00:00 2001 From: Abigail Date: Thu, 28 Jan 2021 01:58:14 +0100 Subject: AWK solution for week 3, part 2 --- challenge-003/abigail/README.md | 1 + challenge-003/abigail/awk/ch-2.awk | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge-003/abigail/awk/ch-2.awk diff --git a/challenge-003/abigail/README.md b/challenge-003/abigail/README.md index 818ae3c474..3f6c33f85f 100644 --- a/challenge-003/abigail/README.md +++ b/challenge-003/abigail/README.md @@ -37,4 +37,5 @@ rows from the command line. The Pascal Triangle should have at least [wikipedia](https://en.wikipedia.org/wiki/Pascal%27s_triangle) page. ### Solutions +* [AWK](awk/ch-2.awk) * [Perl](perl/ch-2.pl) diff --git a/challenge-003/abigail/awk/ch-2.awk b/challenge-003/abigail/awk/ch-2.awk new file mode 100644 index 0000000000..1689ce668f --- /dev/null +++ b/challenge-003/abigail/awk/ch-2.awk @@ -0,0 +1,33 @@ +#!/usr/bin/awk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.awk < input-file +# + +{ + row [1] = 1 # 0'th row + printf "%d\n", row [1] + + for (r = 1; r <= $0; r ++) { + # + # Calculate the new row, and print it + # + for (i = 1; i <= length (row) + 1; i ++) { + new [i] = (i == 1 ? 0 : row [i - 1]) +\ + (i == length (row) + 1 ? 0 : row [i]) + printf ("%s%d", i == 1 ? "" : " ", new [i]) + } + printf "\n"; + + # + # Copy the new row to the current row + # + for (i = 1; i <= length (new); i ++) { + row [i] = new [i] + } + } +} -- cgit