From d48b695c863bfbf01418e8c5eef1db6f45b45e67 Mon Sep 17 00:00:00 2001 From: Abigail Date: Fri, 12 Feb 2021 16:52:56 +0100 Subject: GNU AWK solution for week 4, part 2 --- challenge-004/abigail/README.md | 5 ++++ challenge-004/abigail/awk/ch-2.gawk | 54 +++++++++++++++++++++++++++++++++++++ challenge-004/abigail/t/ctest.ini | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 challenge-004/abigail/awk/ch-2.gawk diff --git a/challenge-004/abigail/README.md b/challenge-004/abigail/README.md index 500d34b5ea..951045328c 100644 --- a/challenge-004/abigail/README.md +++ b/challenge-004/abigail/README.md @@ -40,5 +40,10 @@ each letter only once (though there can be duplicates and you can use each of them once), you don't have to use all the letters. (Disclaimer: The challenge was proposed by Scimon Proctor) +### Notes +We will assume the word list is passed in with a '-f' parameter. +The sets of letters are read from standard input. + ### Solutions +* [GNU AWK](awk/ch-2.gawk) * [Perl](perl/ch-2.pl) diff --git a/challenge-004/abigail/awk/ch-2.gawk b/challenge-004/abigail/awk/ch-2.gawk new file mode 100644 index 0000000000..11c73f7913 --- /dev/null +++ b/challenge-004/abigail/awk/ch-2.gawk @@ -0,0 +1,54 @@ +#!/opt/local/bin/gawk + +# +# See ../README.md +# + +# +# Run as: awk -f ch-2.gawk < input-file +# + + +BEGIN { + # + # Parse command line + # + for (i = 1; i < ARGC; i ++) { + if (ARGV [i] == "-f") { + filename = ARGV [i + 1] + } + } + ARGC = 0 + + # + # Read in the words + # + i = 1 + while ((getline word < filename) > 0) { + words [i ++] = word + } + + # + # So we do lower case matching. Note that this is a GNU AWK extension + # + IGNORECASE = 1 +} + + +{ + # + # Split each line into characters. Then, for each word in + # the word list, remove each of the characters from the split + # line. If we end up with an empty string, we have a winner. + # + split ($0, chars, "") + for (i = 1; i <= length (words); i ++) { + copy = words [i] + for (j = 1; j <= length (chars); j ++) { + sub (chars [j], "", copy) + } + if (length (copy) == 0) { + print words [i] + } + } +} diff --git a/challenge-004/abigail/t/ctest.ini b/challenge-004/abigail/t/ctest.ini index 558a282cc3..6675a345b1 100644 --- a/challenge-004/abigail/t/ctest.ini +++ b/challenge-004/abigail/t/ctest.ini @@ -1,11 +1,11 @@ [names] 1-1 = Pi, 3242 digits 2-1 = Small wordlist +2-2 = Large wordlist [challenges/1] extra_tests = Check_Program_Size - [2-1] args = -f t/words.txt -- cgit