in reply to Re^3: LeetCode Problem #1 - Two Sum - Improve and/or discuss.
in thread LeetCode Problem #1 - Two Sum - Improve and/or discuss.

I'm not following your prosaic description here. Please post whatever parts of the code you'd write differently.

— Ken

  • Comment on Re^4: LeetCode Problem #1 - Two Sum - Improve and/or discuss.

Replies are listed 'Best First'.
Re^5: LeetCode Problem #1 - Two Sum - Improve and/or discuss.
by NetWallah (Canon) on Jan 24, 2022 at 04:00 UTC
    What LanX is saying is : since you are now sorting the output of twosum as a part of your "is_deeply" compare, you no longer need to duplicate tests , and you can revert to your original set of tests that have the results in increasing-index order.

                    "If you had better tools, you could more effectively demonstrate your total incompetence."

Re^5: LeetCode Problem #1 - Two Sum - Improve and/or discuss.
by LanX (Saint) on Jan 24, 2022 at 08:49 UTC
    Hi Ken

    > Please post whatever parts of the code you'd write differently.

    my @tests = ( [[2,7,11,15], 9, [0,1]], [[3,2,4], 6, [1,2]], [[3,3], 6, [0,1]], ); for my $test (@tests) { is_deeply sort_arrayref(two_sum($test->[INPUT], $test->[TARGET])), $test->[EXPECTED]; } sub sort_arrayref { my ($aref) = @_; return [ sort { $a <=> $b } @$aref ]; }

    hope it's clearer now :)

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

        > By the way, I don't see any difference between my &sort_arrayref and yours. I've looked closely at both; do let me know if I've missed something.

        They are the same.

        > You've modified is_deeply such that any additional tests added to @tests must have the results in ascending order for the is_deeply test to be successful.

        OK, if you prefer to order them during the tests, so be it.

        > There were no duplicates. I added (different) tests such that the result could be in "any order".

        hmm ... those seemingly different "any order" arrays are sorted to the same expect-part in the tests.

        Hence half of your tests are redundant and violate the DRY principle.

        I really don't know how to explain it any better.

        FWIW: one actually needs more tests if the input isn't unique, since those cases are different but legit solutions.

        [[2,7,7,15], 9, [0,1]], [[2,7,7,15], 9, [0,2]],

        another case for multiple solutions would be

        [[2,7,4,5], 9, [0,1]], [[2,7,4,5], 9, [2,3]],

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery