diff options
| author | Abigail <abigail@abigail.be> | 2021-02-12 16:52:56 +0100 |
|---|---|---|
| committer | Abigail <abigail@abigail.be> | 2021-03-04 18:57:26 +0100 |
| commit | d48b695c863bfbf01418e8c5eef1db6f45b45e67 (patch) | |
| tree | 2ea4860e4c01ebee3e50940f1515910565a24cf5 /challenge-004 | |
| parent | e464bb0392db89a0da6ce92273c25c9de64f4726 (diff) | |
| download | perlweeklychallenge-club-d48b695c863bfbf01418e8c5eef1db6f45b45e67.tar.gz perlweeklychallenge-club-d48b695c863bfbf01418e8c5eef1db6f45b45e67.tar.bz2 perlweeklychallenge-club-d48b695c863bfbf01418e8c5eef1db6f45b45e67.zip | |
GNU AWK solution for week 4, part 2
Diffstat (limited to 'challenge-004')
| -rw-r--r-- | challenge-004/abigail/README.md | 5 | ||||
| -rw-r--r-- | challenge-004/abigail/awk/ch-2.gawk | 54 | ||||
| -rw-r--r-- | challenge-004/abigail/t/ctest.ini | 2 |
3 files changed, 60 insertions, 1 deletions
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
|
