aboutsummaryrefslogtreecommitdiff
path: root/challenge-005/paulo-custodio/awk
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-02-14 17:47:50 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-02-14 17:47:50 +0000
commitb6c6c3866bb435f0b79732716c3fc49bb5fead87 (patch)
treeeb77b2b61c35f1d0714176b6054f7100a81d5af2 /challenge-005/paulo-custodio/awk
parentef14c20e2e2d61abe4cc73e5389733122df02d42 (diff)
downloadperlweeklychallenge-club-b6c6c3866bb435f0b79732716c3fc49bb5fead87.tar.gz
perlweeklychallenge-club-b6c6c3866bb435f0b79732716c3fc49bb5fead87.tar.bz2
perlweeklychallenge-club-b6c6c3866bb435f0b79732716c3fc49bb5fead87.zip
Add Awk solution to challenge 005
Diffstat (limited to 'challenge-005/paulo-custodio/awk')
-rw-r--r--challenge-005/paulo-custodio/awk/ch-1.awk45
-rw-r--r--challenge-005/paulo-custodio/awk/ch-2.awk60
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-005/paulo-custodio/awk/ch-1.awk b/challenge-005/paulo-custodio/awk/ch-1.awk
new file mode 100644
index 0000000000..24251da5d9
--- /dev/null
+++ b/challenge-005/paulo-custodio/awk/ch-1.awk
@@ -0,0 +1,45 @@
+#!/usr/bin/gawk
+
+# Challenge 005
+#
+# Challenge #1
+# Write a program which prints out all anagrams for a given word. For more
+# information about Anagram, please check this wikipedia page.
+# create a hash of all words in dictionary where key is sorted list of letters
+# therefore two anagrams have the same key
+
+function alen(a, i, k) {
+ k = 0;
+ for(i in a) k++;
+ return k;
+}
+
+function join(array, start, end, sep, result, i) {
+ if (sep == "")
+ sep = " ";
+ else if (sep == SUBSEP) # magic value
+ sep = "";
+ result = array[start];
+ for (i = start + 1; i <= end; i++)
+ result = result sep array[i];
+ return result;
+}
+
+function word_key(word, i, letters, key) {
+ for (i = 1; i <= length(word); i++)
+ letters[i] = substr(word, i, 1);
+ asort(letters);
+ key = join(letters, 1, alen(letters), SUBSEP);
+ return key;
+}
+
+BEGIN {
+ word = tolower(ARGV[1]);
+ key = word_key(word);
+ while ((getline word < "words.txt") > 0 ) {
+ word = tolower(word);
+ if (word_key(word) == key)
+ print word;
+ }
+ exit 0;
+}
diff --git a/challenge-005/paulo-custodio/awk/ch-2.awk b/challenge-005/paulo-custodio/awk/ch-2.awk
new file mode 100644
index 0000000000..93706dff9d
--- /dev/null
+++ b/challenge-005/paulo-custodio/awk/ch-2.awk
@@ -0,0 +1,60 @@
+#!/usr/bin/gawk
+
+# Challenge 005
+#
+# Challenge #2
+# Write a program to find the sequence of characters that has the most anagrams.
+#
+# create a hash of all words in dictionary where key is sorted list of letters
+# therefore two anagrams have the same key
+
+function alen(a, i, k) {
+ k = 0;
+ for(i in a) k++;
+ return k;
+}
+
+function join(array, start, end, sep, result, i) {
+ if (sep == "")
+ sep = " ";
+ else if (sep == SUBSEP) # magic value
+ sep = "";
+ result = array[start];
+ for (i = start + 1; i <= end; i++)
+ result = result sep array[i];
+ return result;
+}
+
+function word_key(word, i, letters, key) {
+ for (i = 1; i <= length(word); i++)
+ letters[i] = substr(word, i, 1);
+ asort(letters);
+ key = join(letters, 1, alen(letters), SUBSEP);
+ return key;
+}
+
+BEGIN {
+ max_anagrams = 0;
+ while ((getline word < "words.txt") > 0) {
+ if (length(word) >= 2) {
+ if (!(word ~ /[^a-zA-Z]/)) {
+ key = word_key(tolower(word));
+ num_anagrams = ++anagrams[key];
+ if (num_anagrams > max_anagrams)
+ max_anagrams = num_anagrams;
+ }
+ }
+ }
+
+ num_found = 0;
+ for (key in anagrams) {
+ if (anagrams[key] == max_anagrams)
+ found[num_found++] = key;
+ }
+ asort(found);
+
+ print "Maximum of " max_anagrams " anagrams";
+ for (i in found)
+ print found[i];
+ exit 0;
+}