diff options
| author | dmaestro <dmaestro@cpan.org> | 2019-04-28 01:32:15 -0500 |
|---|---|---|
| committer | dmaestro <dmaestro@cpan.org> | 2019-04-28 01:40:27 -0500 |
| commit | b61cb208cb2f80c1ea25898ade4589c0b8e615c7 (patch) | |
| tree | 9061038f21754afe0b4aba380a2270735cc575a2 /challenge-005 | |
| parent | f6c4c5c206fd08f144985e6f16004e26640b13e0 (diff) | |
| download | perlweeklychallenge-club-b61cb208cb2f80c1ea25898ade4589c0b8e615c7.tar.gz perlweeklychallenge-club-b61cb208cb2f80c1ea25898ade4589c0b8e615c7.tar.bz2 perlweeklychallenge-club-b61cb208cb2f80c1ea25898ade4589c0b8e615c7.zip | |
Solutions to Challenge 5 for Doug Schrag
Diffstat (limited to 'challenge-005')
| -rw-r--r-- | challenge-005/doug-schrag/perl6/ch-1.p6 | 16 | ||||
| -rw-r--r-- | challenge-005/doug-schrag/perl6/ch-2.p6 | 43 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-005/doug-schrag/perl6/ch-1.p6 b/challenge-005/doug-schrag/perl6/ch-1.p6 new file mode 100644 index 0000000000..49d5720639 --- /dev/null +++ b/challenge-005/doug-schrag/perl6/ch-1.p6 @@ -0,0 +1,16 @@ +use v6; + +sub MAIN(Str $word, :$word-file) { + my $file = .IO with $word-file; + my Set $words = Set.new(.lines.sort) with $file; + my &is-word = $words.defined + ?? -> $w { $w (elem) $words } + !! -> $w { True }; + for $word.comb.permutations.unique(:with(&[eqv])) { + with .join { + .say if .&is-word + } + } +} + + diff --git a/challenge-005/doug-schrag/perl6/ch-2.p6 b/challenge-005/doug-schrag/perl6/ch-2.p6 new file mode 100644 index 0000000000..4482e93431 --- /dev/null +++ b/challenge-005/doug-schrag/perl6/ch-2.p6 @@ -0,0 +1,43 @@ +use v6; + +sub MAIN(:$word-file is copy, :$lengthy) { + my $file = .IO with $word-file; + my Set $words = Set.new(.lines.sort) with $file; + return unless $words; + + my &is-word = $words.defined + ?? -> $w { $w (elem) $words } + !! -> $w { True }; + my %counts; + for $words.keys -> $word { + my $norm = normalize-anagram($word); + %counts{ $norm }++; + } + + my $max = %counts.pairs.max({ .value }).value; + my @patterns = %counts.pairs.grep(*.value == $max)>>.key; + for @patterns { + .say; + " $_".say for .&anagrams; + } + + if ($lengthy) { + say %counts.grep({ + .value > 1 + && .key.chars > @patterns.max(*.chars).chars + }); + } + + sub anagrams ($word) { + gather + for $word.comb.permutations.unique(:with(&[eqv])) { + with .join { + .take if .&is-word + } + } + } + sub normalize-anagram(Str $word) { + return $word.comb.sort.join; + } +} + |
