diff options
| author | John Barrett <john@jbrt.org> | 2019-04-25 00:05:07 +0100 |
|---|---|---|
| committer | John Barrett <john@jbrt.org> | 2019-04-25 00:05:07 +0100 |
| commit | 3d23875d0ec4a40ae44eebb642559cc81e402dd6 (patch) | |
| tree | cabad83886ce713085727fcec990e6bf4cab337c /challenge-005 | |
| parent | 0527cda37094b391eb39858f2a5ebfde09d86f11 (diff) | |
| download | perlweeklychallenge-club-3d23875d0ec4a40ae44eebb642559cc81e402dd6.tar.gz perlweeklychallenge-club-3d23875d0ec4a40ae44eebb642559cc81e402dd6.tar.bz2 perlweeklychallenge-club-3d23875d0ec4a40ae44eebb642559cc81e402dd6.zip | |
A pass at Week 5 part 2
Diffstat (limited to 'challenge-005')
| -rwxr-xr-x | challenge-005/john-barrett/perl5/ch-1.pl | 2 | ||||
| -rwxr-xr-x | challenge-005/john-barrett/perl5/ch-2.pl | 27 |
2 files changed, 28 insertions, 1 deletions
diff --git a/challenge-005/john-barrett/perl5/ch-1.pl b/challenge-005/john-barrett/perl5/ch-1.pl index f1dfd6309e..6da945d981 100755 --- a/challenge-005/john-barrett/perl5/ch-1.pl +++ b/challenge-005/john-barrett/perl5/ch-1.pl @@ -17,4 +17,4 @@ sub is_anagram { open my $fh, '<:encoding(UTF-8)', '/usr/share/dict/words'; chomp( my @dict = <$fh> ); -printf "$_\n" for grep { is_anagram( $_ ) } @dict; +print "$_\n" for grep { is_anagram( $_ ) } @dict; diff --git a/challenge-005/john-barrett/perl5/ch-2.pl b/challenge-005/john-barrett/perl5/ch-2.pl new file mode 100755 index 0000000000..020c35ee33 --- /dev/null +++ b/challenge-005/john-barrett/perl5/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use autodie; + +sub sortedlc { join '', sort { $a cmp $b } split '', lc $_[0] } + +open my $fh, '<:encoding(UTF-8)', '/usr/share/dict/words'; +chomp( my @dict = <$fh> ); + +my %signatures; +$signatures{ sortedlc( $_ ) }++ for @dict; +my %by_count = reverse %signatures; + +my $max = ( sort { $b <=> $a } keys %by_count)[0]; +my $word = $by_count{$max}; +my $wordlength = length $word; + +sub is_anagram { + my ( $dictword ) = @_; + return 0 if length $dictword != $wordlength; + $word eq sortedlc $dictword; +} + +print "(A) longest anagram set:\n"; +print "$_\n" for grep { is_anagram( $_ ) } @dict; |
