donnemartin / interactive-coding-challenges

120+ interactive Python coding interview challenges (algorithms and data structures). Includes Anki flashcards.
Other
29.44k stars 4.45k forks source link

Two sum unit test too strict #230

Open liavkoren opened 6 years ago

liavkoren commented 6 years ago
    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.

havanagrawal commented 5 years ago

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.