aboutsummaryrefslogtreecommitdiff
path: root/challenge-005
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-28 09:21:31 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2019-04-28 09:21:31 +0100
commit8a3358350d1ab099f75db3386155336951aa9ec8 (patch)
tree158879e8462862158b11c45812e562ac0d2e0056 /challenge-005
parent826f3d46d156740ed7e173e933ba8d896665486f (diff)
downloadperlweeklychallenge-club-8a3358350d1ab099f75db3386155336951aa9ec8.tar.gz
perlweeklychallenge-club-8a3358350d1ab099f75db3386155336951aa9ec8.tar.bz2
perlweeklychallenge-club-8a3358350d1ab099f75db3386155336951aa9ec8.zip
- Added solutions by Athanasius.
Diffstat (limited to 'challenge-005')
-rw-r--r--challenge-005/athanasius/perl5/ch-1.pl64
-rw-r--r--challenge-005/athanasius/perl5/ch-2.pl75
2 files changed, 139 insertions, 0 deletions
diff --git a/challenge-005/athanasius/perl5/ch-1.pl b/challenge-005/athanasius/perl5/ch-1.pl
new file mode 100644
index 0000000000..af9bb1f8b6
--- /dev/null
+++ b/challenge-005/athanasius/perl5/ch-1.pl
@@ -0,0 +1,64 @@
+#!perl
+
+use strict;
+use warnings;
+use Const::Fast;
+
+# Downloaded from https://crosswordman.com/wordlist.html:
+const my $WORDFILE => 'UK Advanced Cryptics Dictionary.txt';
+const my @DEFAULT => qw( parses );
+
+$| = 1;
+
+MAIN:
+{
+ my $dict = init_dict();
+
+ # Challenge 1
+
+ find_anagrams($dict, @ARGV ? @ARGV : @DEFAULT);
+}
+
+
+sub find_anagrams
+{
+ my ($dict, @input) = @_;
+
+ for my $word (@input)
+ {
+ my $target = $word =~ s/[^A-Za-z]//gr;
+ my $key = join '', sort split //, $target;
+ my @anagrams = $dict->{$key}->@*;
+ @anagrams = grep { $_ ne $target } @anagrams;
+
+ if (@anagrams)
+ {
+ printf "\nFound %d anagrams of '%s':\n%s\n", scalar @anagrams,
+ $word, join(', ', @anagrams);
+ }
+ else
+ {
+ printf "\nNo anagrams of '%s' found\n", $word;
+ }
+ }
+}
+
+sub init_dict
+{
+ my %dict;
+
+ open(my $fh, '<', $WORDFILE)
+ or die "Cannot open file '$WORDFILE' for reading, stopped";
+
+ while (<$fh>)
+ {
+ next if 1 .. / ^ -+ $ /x; # Skip header
+ chomp;
+ push $dict{ join '', sort split //, $_ }->@*, $_;
+ }
+
+ close $fh
+ or die "Cannot close file '$WORDFILE', stopped";
+
+ return \%dict;
+}
diff --git a/challenge-005/athanasius/perl5/ch-2.pl b/challenge-005/athanasius/perl5/ch-2.pl
new file mode 100644
index 0000000000..34d1b90609
--- /dev/null
+++ b/challenge-005/athanasius/perl5/ch-2.pl
@@ -0,0 +1,75 @@
+#!perl
+
+use strict;
+use warnings;
+use Const::Fast;
+
+# Downloaded from https://crosswordman.com/wordlist.html:
+const my $WORDFILE => 'UK Advanced Cryptics Dictionary.txt';
+const my @DEFAULT => qw( parses );
+
+$| = 1;
+
+MAIN:
+{
+ my $dict = init_dict();
+
+ # Challenge 2
+
+ find_most_anagrams($dict);
+}
+
+sub find_most_anagrams
+{
+ my ($dict) = @_;
+ my $max = 0;
+ my @max_keys;
+
+ for my $key (keys %$dict)
+ {
+ my $count = scalar $dict->{$key}->@*;
+ if ($count >= $max)
+ {
+ @max_keys = () if $count > $max;
+ $max = $count;
+ push @max_keys, $key;
+ }
+ }
+
+ if (scalar @max_keys == 1)
+ {
+ my $key = $max_keys[0];
+ printf "\nThe sequence of characters with the most anagrams is '%s' " .
+ "with %d:\n%s\n", $key, $max, join(', ', $dict->{$key}->@*);
+ }
+ else
+ {
+ printf "\nThere are %d character sequences that produce %d anagrams " .
+ "each:\n", scalar @max_keys, $max;
+ for my $key (sort @max_keys)
+ {
+ printf "\n'%s' produces:\n%s\n", $key,
+ join( ', ', $dict->{$key}->@* );
+ }
+ }
+}
+
+sub init_dict
+{
+ my %dict;
+
+ open(my $fh, '<', $WORDFILE)
+ or die "Cannot open file '$WORDFILE' for reading, stopped";
+
+ while (<$fh>)
+ {
+ next if 1 .. / ^ -+ $ /x; # Skip header
+ chomp;
+ push $dict{ join '', sort split //, $_ }->@*, $_;
+ }
+
+ close $fh
+ or die "Cannot close file '$WORDFILE', stopped";
+
+ return \%dict;
+}