diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-04-28 17:09:31 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-04-28 17:09:31 +0100 |
| commit | 5e2d3a25ce91071c9ee4e7b1269db428ea9db8de (patch) | |
| tree | 208ac39f0fc00cc00f9de1db088a1f290427ca8d /challenge-005 | |
| parent | c7232efc6a894a31ce8955a8eaaac7c5fde3147b (diff) | |
| parent | 74dd46f18a725cfe920bfa362fd1ddc3377143c8 (diff) | |
| download | perlweeklychallenge-club-5e2d3a25ce91071c9ee4e7b1269db428ea9db8de.tar.gz perlweeklychallenge-club-5e2d3a25ce91071c9ee4e7b1269db428ea9db8de.tar.bz2 perlweeklychallenge-club-5e2d3a25ce91071c9ee4e7b1269db428ea9db8de.zip | |
Merge pull request #101 from kianmeng/master
Challenge #5 answers and challenge #4 blog link
Diffstat (limited to 'challenge-005')
| -rw-r--r-- | challenge-005/kian-meng-ang/ch-1.pl | 40 | ||||
| -rw-r--r-- | challenge-005/kian-meng-ang/ch-2.pl | 44 |
2 files changed, 84 insertions, 0 deletions
diff --git a/challenge-005/kian-meng-ang/ch-1.pl b/challenge-005/kian-meng-ang/ch-1.pl new file mode 100644 index 0000000000..7bf4e81372 --- /dev/null +++ b/challenge-005/kian-meng-ang/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use 5.010; +use autodie; +use strict; +use warnings; +use utf8; +use Carp; + +carp 'missing word and dictionary file' if (@ARGV != 2); +my ($word, $dict) = @ARGV; +my $word_hash = join '', sort split //, $word; +my $word_length = length $word_hash; +my @anagrams; + +open my $fh, '<:encoding(UTF-8)', $dict; +while (my $dword = <$fh>) { + chomp $dword; + next if (length $dword != $word_length); + + my $dword_hash = join '', sort split //, $dword; + next if ($dword_hash ne $word_hash); + next if ($dword eq $word); + + push @anagrams, $dword; +} +close $fh; + +say sprintf 'Word: %s', $word; +say sprintf 'Anagrams: %s', join q|, |, @anagrams; + +1; + +__END__ + +$ perl ch-1.pl elbow /usr/share/dict/words +Word: elbow +Anagrams: below, bowel + +# vi:et:sw=4 ts=4 ft=perl diff --git a/challenge-005/kian-meng-ang/ch-2.pl b/challenge-005/kian-meng-ang/ch-2.pl new file mode 100644 index 0000000000..18e659b925 --- /dev/null +++ b/challenge-005/kian-meng-ang/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use 5.010; +use autodie; +use strict; +use warnings; +use utf8; +use Carp; +use List::Util qw(max); + +carp 'missing dictionary file' if (@ARGV != 1); +my ($dict) = @ARGV; +my %anagram_list; + +open my $fh, '<:encoding(UTF-8)', $dict; +while (my $dword = <$fh>) { + chomp $dword; + + my $dword_hash = join '', sort split //, $dword; + push @{ $anagram_list{$dword_hash} }, $dword; +} +close $fh; + +my $max_anagram_count = max(map { scalar @{$_} } values %anagram_list); + +foreach my $anagrams (values %anagram_list) { + next if (scalar @{$anagrams} != $max_anagram_count); + say sprintf 'Total anagrams: %d', scalar @{$anagrams}; + say sprintf 'Anagrams: %s', join q|, |, @{$anagrams}; + say q||; +} + +1; + +__END__ + +$ perl ch-2.pl /usr/share/dict/words +Total anagrams: 7 +Anagrams: carets, caster, caters, crates, reacts, recast, traces + +Total anagrams: 7 +Anagrams: pares, parse, pears, rapes, reaps, spare, spear + +# vi:et:sw=4 ts=4 ft=perl |
