interactive-coding-challenges icon indicating copy to clipboard operation
interactive-coding-challenges copied to clipboard

Two sum unit test too strict

Open liavkoren opened this issue 6 years ago • 1 comments

    def test_two_sum(self):
        solution = Solution()
        assert_raises(TypeError, solution.two_sum, None, None)
        assert_raises(ValueError, solution.two_sum, [], 0)
        target = 7
        nums = [1, 3, 2, -7, 5]
        expected = [2, 4]
        assert_equal(solution.two_sum(nums, target), expected)
        print('Success: test_two_sum')

[4, 2] should also be a valid answer. Unittest has an assertItemsEqual method, I'm not sure if nose has that too. I'm happy to write a PR for this, it's trivial to fix.

liavkoren avatar Mar 01 '18 19:03 liavkoren

The constraints in the problem clearly state that there is exactly one solution. Instead of modifying the solution, the problem should probably state:

...find the two indices i, j s.t. i < j...

though this is an implicit assumption for the two sum problem in general.

havanagrawal avatar Apr 18 '19 00:04 havanagrawal