aboutsummaryrefslogtreecommitdiff
path: root/challenge-005
diff options
context:
space:
mode:
authorRyan Thompson <i@ry.ca>2019-12-17 16:45:28 -0600
committerRyan Thompson <i@ry.ca>2019-12-17 16:45:28 -0600
commita26d8716271b568c97c62ff33dafcc123fb1e33f (patch)
treee6dbea8dff7766376436c9d0200a971c6e2f8b08 /challenge-005
parenta3c82344c19bb927beed4b1b03a056d5e99db0ed (diff)
downloadperlweeklychallenge-club-a26d8716271b568c97c62ff33dafcc123fb1e33f.tar.gz
perlweeklychallenge-club-a26d8716271b568c97c62ff33dafcc123fb1e33f.tar.bz2
perlweeklychallenge-club-a26d8716271b568c97c62ff33dafcc123fb1e33f.zip
Solutions for challenges 001-005
Diffstat (limited to 'challenge-005')
-rw-r--r--challenge-005/ryan-thompson/README1
-rwxr-xr-xchallenge-005/ryan-thompson/perl5/ch-1.pl26
-rwxr-xr-xchallenge-005/ryan-thompson/perl5/ch-2.pl33
-rw-r--r--challenge-005/ryan-thompson/perl6/ch-1.p623
-rw-r--r--challenge-005/ryan-thompson/perl6/ch-2.p614
5 files changed, 97 insertions, 0 deletions
diff --git a/challenge-005/ryan-thompson/README b/challenge-005/ryan-thompson/README
new file mode 100644
index 0000000000..53b1e7cfa0
--- /dev/null
+++ b/challenge-005/ryan-thompson/README
@@ -0,0 +1 @@
+Solutions by Ryan Thompson.
diff --git a/challenge-005/ryan-thompson/perl5/ch-1.pl b/challenge-005/ryan-thompson/perl5/ch-1.pl
new file mode 100755
index 0000000000..c5f4d781f2
--- /dev/null
+++ b/challenge-005/ryan-thompson/perl5/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+#
+# ch-1.pl - Print all anagrams of a given word
+#
+# 2019 Ryan Thompson <rjt@cpan.org>
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+use feature 'fc';
+
+# Dictionary creation is O(n) on number of words. Anagram lookup is O(1).
+
+my %dict;
+for (<STDIN>) {
+ chomp;
+ my $key = join '', sort split '', fc;
+ $dict{$key} //= [ ];
+ push @{$dict{$key}}, fc($_);
+};
+
+my $word = shift // die "Usage: $0 <word>";
+my $word_key = join '', sort split '', fc($word);
+
+say for sort @{ $dict{$word_key} // die "$word is not in dictionary" };
diff --git a/challenge-005/ryan-thompson/perl5/ch-2.pl b/challenge-005/ryan-thompson/perl5/ch-2.pl
new file mode 100755
index 0000000000..c95cd95644
--- /dev/null
+++ b/challenge-005/ryan-thompson/perl5/ch-2.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/env perl
+#
+# ch-2.pl - Print "string of characters" with the most anagrams
+#
+# 2019 Ryan Thompson <rjt@cpan.org>
+
+# There's some ambiguity in the challenge description. I do not know what
+# "a sequence of characters" means in the context of this challenge, so I'm
+# going to write some general functions and then turn that into a specific
+# example. The general functions could, however, be used to analyze just
+# about any sequence of characters.
+
+use 5.010;
+use warnings;
+use strict;
+no warnings 'uninitialized';
+use feature 'fc';
+use List::Util qw< max >;
+
+# All the smarts are in the dictionary. We key on the sorted list of chars,
+# and append the original word to an array ref in the value. Thus all anagrams
+# of each other will have the same hash key.
+my %dict;
+for (<STDIN>) {
+ chomp;
+ my $key = join '', sort split '', fc;
+ $dict{$key} //= [ ];
+ push @{$dict{$key}}, fc($_);
+};
+
+# This gives all results in the event of a tie (the case with my dict)
+my $max = max map { scalar @$_ } values %dict;
+say for map { "<@$_>" } grep { @$_ == $max } values %dict;
diff --git a/challenge-005/ryan-thompson/perl6/ch-1.p6 b/challenge-005/ryan-thompson/perl6/ch-1.p6
new file mode 100644
index 0000000000..41608eabcc
--- /dev/null
+++ b/challenge-005/ryan-thompson/perl6/ch-1.p6
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl6
+
+# ch-1.p6 - Print all anagrams of a word
+#
+# Ryan Thompson <rjt@cpan.org>
+
+sub MAIN( Str $word ) {
+ # 25 seconds. I'm hoping this will get faster as Raku matures...
+ my %dict = % .classify-list: { word-key($_) }, $*ARGFILES.lines».fc;
+
+ .say for %dict{ word-key($word) };
+}
+
+sub word-key( Str $word ) { $word.fc.comb.sort.join }
+
+# Here's a cute-but-very-slow version. Instructive, but .permutations
+# really takes a long time on long words like 「irresistible」
+# (12! = 479,001,600 permutations)
+sub MAIN_cute_but_slow( Str $word ) {
+ my $perm = set $word.fc.comb.permutations».join.unique;
+ my $dict = set $*ARGFILES.lines».fc;
+ .say for ($dict ∩ $perm).keys.sort;
+}
diff --git a/challenge-005/ryan-thompson/perl6/ch-2.p6 b/challenge-005/ryan-thompson/perl6/ch-2.p6
new file mode 100644
index 0000000000..5aff9bec0e
--- /dev/null
+++ b/challenge-005/ryan-thompson/perl6/ch-2.p6
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl6
+
+# ch-2.p6 - Print word with highest anagram count
+#
+# Ryan Thompson <rjt@cpan.org>
+
+sub MAIN( ) {
+ my %dict = % .classify-list: { word-key($_) }, $*ARGFILES.lines».fc;
+
+ my $max = %dict.values».elems.max;
+ .say for %dict.values.grep: { .elems == $max };
+}
+
+sub word-key( Str $word ) { $word.fc.comb.sort.join }