diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-01-09 17:17:09 +0100 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-01-09 18:35:35 +0100 |
| commit | 691a848021e90da8f8fb35dfdffc41d54c5a1bdb (patch) | |
| tree | df98be6a7cceab7921c9decd07944cd787194bfe /challenge-094/jo-37 | |
| parent | 3e42a8e7ab9f8e43eed7f07a3f76e3e8ab3e561a (diff) | |
| download | perlweeklychallenge-club-691a848021e90da8f8fb35dfdffc41d54c5a1bdb.tar.gz perlweeklychallenge-club-691a848021e90da8f8fb35dfdffc41d54c5a1bdb.tar.bz2 perlweeklychallenge-club-691a848021e90da8f8fb35dfdffc41d54c5a1bdb.zip | |
Command line handling for task 1
Diffstat (limited to 'challenge-094/jo-37')
| -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; +} |
