From ba08d9700618c5b65cac87f70517c40dd7af5429 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Sun, 2 Jan 2022 22:19:37 +0000 Subject: separate out stringify --- challenge-145/james-smith/perl/ch-2.pl | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/challenge-145/james-smith/perl/ch-2.pl b/challenge-145/james-smith/perl/ch-2.pl index 9748778fbf..6e10b86065 100644 --- a/challenge-145/james-smith/perl/ch-2.pl +++ b/challenge-145/james-smith/perl/ch-2.pl @@ -21,7 +21,7 @@ my @TESTS = ( [ 'llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch', 'l ll a n f i r p w g y gog o e c h d b lll llll t s ili ogo ogogo ogogogo gogog' ], ); -is( eertree($_->[0]), $_->[1] ) for @TESTS; +is( stringify( eertree($_->[0]) ), $_->[1] ) for @TESTS; done_testing(); sub eertree { @@ -37,13 +37,7 @@ sub eertree { add_str( $tree, $str, -1, $_, $_ ), add_str( $tree, $str, q(), $_, $_+1 ) for 0.. @{$str}-1; - ## grab nodes and sort into start/length order and join.... - return join q( ), - sort { $tree->{$a}{'start'} <=> $tree->{$b}{'start'} || - length $a <=> length $b - } - grep { defined $tree->{$_}{'start'} } - keys %{$tree}; + $tree; } sub add_str { @@ -69,3 +63,12 @@ sub add_str { } } +sub stringify { + my $tr = shift; + join q( ), + sort { $tr->{$a}{'start'} <=> $tr->{$b}{'start'} || + length $a <=> length $b + } + grep { defined $tr->{$_}{'start'} } + keys %{$tr}; +} -- cgit