From 691a848021e90da8f8fb35dfdffc41d54c5a1bdb Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sat, 9 Jan 2021 17:17:09 +0100 Subject: Command line handling for task 1 --- challenge-094/jo-37/perl/ch-1.pl | 61 ++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 18 deletions(-) mode change 100644 => 100755 challenge-094/jo-37/perl/ch-1.pl (limited to 'challenge-094') diff --git a/challenge-094/jo-37/perl/ch-1.pl b/challenge-094/jo-37/perl/ch-1.pl old mode 100644 new mode 100755 index a6725762ba..0e8dc4860b --- 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(<[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; +} -- cgit