This notebook was prepared by Donne Martin. Source and license info is on GitHub.
Challenge Notebook¶
Problem: Given an array, find the two indices that sum to a specific value.¶
Constraints¶
- Is there exactly one solution?
- Yes
- Is there always a solution?
- Yes
- Is the array an array of ints?
- Yes
- Is the array sorted? No
- Are negative values possible?
- Yes
- Can we assume the inputs are valid?
- No
- Can we assume this fits memory?
- Yes
Test Cases¶
- None input -> TypeError
- [] -> ValueError
- [1, 3, 2, -7, 5], 7 -> [2, 4]
Algorithm¶
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
Code¶
In [ ]:
class Solution(object):
def two_sum(self, nums, val):
# TODO: Implement me
pass
Unit Test¶
The following unit test is expected to fail until you solve the challenge.
In [ ]:
# %load test_two_sum.py
import unittest
class TestTwoSum(unittest.TestCase):
def test_two_sum(self):
solution = Solution()
self.assertRaises(TypeError, solution.two_sum, None, None)
self.assertRaises(ValueError, solution.two_sum, [], 0)
target = 7
nums = [1, 3, 2, -7, 5]
expected = [2, 4]
self.assertEqual(solution.two_sum(nums, target), expected)
print('Success: test_two_sum')
def main():
test = TestTwoSum()
test.test_two_sum()
if __name__ == '__main__':
main()
Solution Notebook¶
Review the Solution Notebook for a discussion on algorithms and code solutions.