diff options
| author | Ryan Thompson <i@ry.ca> | 2019-12-17 16:45:28 -0600 |
|---|---|---|
| committer | Ryan Thompson <i@ry.ca> | 2019-12-17 16:45:28 -0600 |
| commit | a26d8716271b568c97c62ff33dafcc123fb1e33f (patch) | |
| tree | e6dbea8dff7766376436c9d0200a971c6e2f8b08 /challenge-005 | |
| parent | a3c82344c19bb927beed4b1b03a056d5e99db0ed (diff) | |
| download | perlweeklychallenge-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/README | 1 | ||||
| -rwxr-xr-x | challenge-005/ryan-thompson/perl5/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-005/ryan-thompson/perl5/ch-2.pl | 33 | ||||
| -rw-r--r-- | challenge-005/ryan-thompson/perl6/ch-1.p6 | 23 | ||||
| -rw-r--r-- | challenge-005/ryan-thompson/perl6/ch-2.p6 | 14 |
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 } |
