diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 17:47:50 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-02-14 17:47:50 +0000 |
| commit | b6c6c3866bb435f0b79732716c3fc49bb5fead87 (patch) | |
| tree | eb77b2b61c35f1d0714176b6054f7100a81d5af2 /challenge-005 | |
| parent | ef14c20e2e2d61abe4cc73e5389733122df02d42 (diff) | |
| download | perlweeklychallenge-club-b6c6c3866bb435f0b79732716c3fc49bb5fead87.tar.gz perlweeklychallenge-club-b6c6c3866bb435f0b79732716c3fc49bb5fead87.tar.bz2 perlweeklychallenge-club-b6c6c3866bb435f0b79732716c3fc49bb5fead87.zip | |
Add Awk solution to challenge 005
Diffstat (limited to 'challenge-005')
| -rw-r--r-- | challenge-005/paulo-custodio/awk/ch-1.awk | 45 | ||||
| -rw-r--r-- | challenge-005/paulo-custodio/awk/ch-2.awk | 60 | ||||
| -rw-r--r-- | challenge-005/paulo-custodio/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-005/paulo-custodio/t/test-1.yaml | 30 | ||||
| -rw-r--r-- | challenge-005/paulo-custodio/t/test-2.yaml | 11 | ||||
| -rw-r--r-- | challenge-005/paulo-custodio/test.pl | 47 |
6 files changed, 149 insertions, 48 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; +} diff --git a/challenge-005/paulo-custodio/perl/ch-1.pl b/challenge-005/paulo-custodio/perl/ch-1.pl index 18fe86f780..c2dedd115b 100644 --- a/challenge-005/paulo-custodio/perl/ch-1.pl +++ b/challenge-005/paulo-custodio/perl/ch-1.pl @@ -3,11 +3,11 @@ # 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. +# 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 - use strict; use warnings; use 5.030; diff --git a/challenge-005/paulo-custodio/t/test-1.yaml b/challenge-005/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..63708186b7 --- /dev/null +++ b/challenge-005/paulo-custodio/t/test-1.yaml @@ -0,0 +1,30 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: + args: binary + input: + output: | + |binary + |brainy +- setup: + cleanup: + args: live + input: + output: | + |evil + |levi + |live + |veil + |vile +- setup: + cleanup: unlink "words.txt"; + args: casper + input: + output: | + |capers + |crapes + |parsec + |pacers + |recaps + |scrape + |spacer + |casper diff --git a/challenge-005/paulo-custodio/t/test-2.yaml b/challenge-005/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..f63585ea79 --- /dev/null +++ b/challenge-005/paulo-custodio/t/test-2.yaml @@ -0,0 +1,11 @@ +- setup: 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); + cleanup: unlink "words.txt"; + args: + input: + output: | + |Maximum of 8 anagrams + |aceprs + |aels + |aelst + |aerst + |egor diff --git a/challenge-005/paulo-custodio/test.pl b/challenge-005/paulo-custodio/test.pl index 8952319125..a61c28ebb7 100644 --- a/challenge-005/paulo-custodio/test.pl +++ b/challenge-005/paulo-custodio/test.pl @@ -5,49 +5,4 @@ use warnings; use Test::More; use 5.030; -# build list of words for testing -ok 0==system("aspell -d en dump master | aspell -l en expand > words.txt"); - -is capture("perl perl/ch-1.pl binary"), <<END; -binary -brainy -END - -is capture("perl perl/ch-1.pl live"), <<END; -evil -levi -live -veil -vile -END - -is capture("perl perl/ch-1.pl casper"), <<END; -capers -crapes -parsec -pacers -recaps -scrape -spacer -casper -END - - -is capture("perl perl/ch-2.pl"), <<END; -Maximum of 8 anagrams -aceprs -aels -aelst -aerst -egor -END - -unlink "words.txt"; -done_testing; - -sub capture { - my($cmd) = @_; - my $out = `$cmd`; - $out =~ s/[ \t\v\f\r]*\n/\n/g; - return $out; -} +require '../../challenge-001/paulo-custodio/test.pl'; |
