Skip to content

Commit

Permalink
Add unitests to TwoSum problem & create Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
joffilyfe committed Dec 12, 2023
1 parent 6f79929 commit f4dd04e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
32 changes: 30 additions & 2 deletions 1.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,40 @@ class Solution:

# this is O(n) because we iterate only once over the list
def twoSum(self, nums: List[int], target: int) -> List[int]:
table = {}
table: dict[int, int] = {}
result: List[int] = []

for i in range(0, len(nums)):
complement = target - nums[i]

if table.get(complement) is not None:
return [table.get(complement), i]
result = [table[complement], i]
break

table[nums[i]] = i

return result


import unittest


class Test(unittest.TestCase):
def setUp(self):
self.solution = Solution()

def test_first(self):
self.assertEqual(
self.solution.twoSum(nums=[2, 7, 11, 15], target=9),
[0, 1],
)

def test_second(self):
self.assertEqual(
self.solution.twoSum(nums=[3, 2, 4], target=6),
[1, 2],
)


if __name__ == "__main__":
unittest.main()
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
| Problem Number | Tag | URL |
| -------------- | ---------- | -------------------------------------- |
| 1.py | Hash table | https://leetcode.com/problems/two-sum/ |
| 1051.py | | |
| 1207.py | | |
| 1356.py | | |
| 1365.py | | |
| 1370.py | | |
| 1436.py | | |
| 1464.py | | |
| 1475.py | | |
| 1512.py | | |
| 1636.py | | |
| 1656.py | | |
| 1684.py | | |
| 1710.py | | |
| 1742.py | | |
| 1748.py | | |
| 1832.py | | |
| 1859.py | | |
| 1876.py | | |
| 1920.py | | |
| 1935.py | | |
| 1941.py | | |
| 20.py | | |
| 2006.py | | |
| 2032.py | | |
| 2037.py | | |
| 2053.py | | |
| 2060.py | | |
| 2103.py | | |
| 2154.py | | |
| 2206.py | | |
| 2215.py | | |
| 2283.py | | |
| 2325.py | | |
| 2341.py | | |
| 2363.py | | |
| 2367-2.py | | |
| 2367.py | | |
| 2418.py | | |
| 2475.py | | |
| 2500.py | | |
| 2545.py | | |
| 2570.py | | |
| 2670.py | | |
| 2716.py | | |
| 2733.py | | |
| 2744.py | | |
| 2848.py | | |
| 2913.py | | |
| 2932.py | | |
| 414.py | | |
| 496.py | | |
| 56.py | | |
| 561.py | | |
| 771.py | | |
| 804.py | | |
| 905.py | | |
| 961.py | | |

0 comments on commit f4dd04e

Please sign in to comment.