aboutsummaryrefslogtreecommitdiff
path: root/challenge-005
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-04-25 10:51:17 +0100
committerGitHub <noreply@github.com>2019-04-25 10:51:17 +0100
commit67ffd652022d3371f116954930fd041bd63a79b3 (patch)
tree84e07acb60e94fea7e826f2c4096ef500f0963ef /challenge-005
parent858d9799376e1cc7cb2a0c60b1f1d1e53e81810f (diff)
parentb1acdbbefdbea5a4db9aeec47f7a9a53610c9035 (diff)
downloadperlweeklychallenge-club-67ffd652022d3371f116954930fd041bd63a79b3.tar.gz
perlweeklychallenge-club-67ffd652022d3371f116954930fd041bd63a79b3.tar.bz2
perlweeklychallenge-club-67ffd652022d3371f116954930fd041bd63a79b3.zip
Merge pull request #93 from jbarrett/jbarrett/challenge5
Week 5
Diffstat (limited to 'challenge-005')
-rwxr-xr-xchallenge-005/john-barrett/perl5/ch-1.pl20
-rwxr-xr-xchallenge-005/john-barrett/perl5/ch-2.pl27
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-005/john-barrett/perl5/ch-1.pl b/challenge-005/john-barrett/perl5/ch-1.pl
new file mode 100755
index 0000000000..6da945d981
--- /dev/null
+++ b/challenge-005/john-barrett/perl5/ch-1.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use autodie;
+
+my $word = sortedlc( $ARGV[0] );
+my $wordlength = length $word;
+
+sub sortedlc { join '', sort { $a cmp $b } split '', lc $_[0] }
+
+sub is_anagram {
+ my ( $dictword ) = @_;
+ return 0 if length $dictword != $wordlength;
+ $word eq sortedlc $dictword;
+}
+
+open my $fh, '<:encoding(UTF-8)', '/usr/share/dict/words';
+chomp( my @dict = <$fh> );
+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..0235c417bc
--- /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 from '$word':\n";
+print "$_\n" for grep { is_anagram( $_ ) } @dict;