interactive-coding-challenges
interactive-coding-challenges copied to clipboard
Two sum unit test too strict
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.
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.