diff options
Diffstat (limited to 'challenge-094')
| -rwxr-xr-x[-rw-r--r--] | challenge-094/jo-37/perl/ch-1.pl | 61 |
1 files changed, 43 insertions, 18 deletions
diff --git a/challenge-094/jo-37/perl/ch-1.pl b/challenge-094/jo-37/perl/ch-1.pl index a6725762ba..0e8dc4860b 100644..100755 --- a/challenge-094/jo-37/perl/ch-1.pl +++ b/challenge-094/jo-37/perl/ch-1.pl @@ -1,28 +1,53 @@ -#!/usr/bin/perl +#!/usr/bin/perl -s use v5.16; use Test2::V0; use experimental 'postderef'; +our $examples; + +run_examples() if $examples; # does not return + +say(<<EOS), exit unless @ARGV; +Usage: $0 [-examples] [word ...] + +-examples + runs the given examples + +word ... + use given words as input +EOS + +# Apply "anagroup" to @ARGV and convert the result into the requested +# format. +say '[ ' . + (join ', ', map { + '(' . (join ', ', map {qq{"$_"}} @$_) . ')' + } anagroup(@ARGV)) . + ' ]'; + # Group given strings by anagrams. sub anagroup { - # Hash to collect anagrams by a canonical key. - my %anagroup; - - # Split strings into characters, sort and rejoin to gain a - # "canonical anagram", decorate each string with its canonical - # anagram and collect the strings within the prepared hash by - # canonical key. - push $anagroup{$_->[0]}->@*, $_->[1] - foreach map {[join('', sort {$a cmp $b} split //), $_]} @_; - - # Sort the canonical anagrams and retrieve the corresponding string - # lists. (The sort is required for a stable result only.) - map {$anagroup{$_}} sort keys %anagroup; + # Hash to collect anagrams by a canonical key. + my %anagroup; + + # Split strings into characters, sort and rejoin to gain a + # "canonical anagram", decorate each string with its canonical + # anagram and collect the strings within the prepared hash by + # canonical key. + push $anagroup{$_->[0]}->@*, $_->[1] + foreach map {[join('', sort split //), $_]} @_; + + # Sort the canonical anagrams and retrieve the corresponding string + # lists. (The sort is required for a stable result only.) + map {$anagroup{$_}} sort keys %anagroup; } -is [anagroup qw(opt bat saw tab pot top was)], - [[qw(bat tab)], [qw(saw was)], [qw(opt pot top)]], 'Example 1'; -is [anagroup 'x'], [['x']], 'Example 2'; +sub run_examples { + is [anagroup qw(opt bat saw tab pot top was)], + [[qw(bat tab)], [qw(saw was)], [qw(opt pot top)]], 'Example 1'; + is [anagroup 'x'], [['x']], 'Example 2'; -done_testing; + done_testing; + exit; +} |
