in reply to Re: LeetCode Problem #1 - Two Sum - Improve and/or discuss.
in thread LeetCode Problem #1 - Two Sum - Improve and/or discuss.
That's a fair enough comment: any order was specified in the problem spec. I added to @tests:
my @tests = ( [[2,7,11,15], 9, [0,1]], [[2,7,11,15], 9, [1,0]], [[3,2,4], 6, [1,2]], [[3,2,4], 6, [2,1]], [[3,3], 6, [0,1]], [[3,3], 6, [1,0]], );
Modified is_deeply:
for my $test (@tests) { is_deeply sort_arrayref(two_sum($test->[INPUT], $test->[TARGET])), sort_arrayref($test->[EXPECTED]); }
Appended a new subroutine:
sub sort_arrayref { my ($aref) = @_; return [ sort { $a <=> $b } @$aref ]; }
Output now:
1..6 ok 1 ok 2 ok 3 ok 4 ok 5 ok 6
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: LeetCode Problem #1 - Two Sum - Improve and/or discuss.
by LanX (Saint) on Jan 24, 2022 at 02:25 UTC | |
by kcott (Archbishop) on Jan 24, 2022 at 03:35 UTC | |
by NetWallah (Canon) on Jan 24, 2022 at 04:00 UTC | |
by LanX (Saint) on Jan 24, 2022 at 08:49 UTC | |
by kcott (Archbishop) on Jan 24, 2022 at 21:36 UTC | |
by LanX (Saint) on Jan 24, 2022 at 22:13 UTC | |
|