diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..86c6b68 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,31 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.x + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run tests + run: | + python -m unittest discover -s leetcode -p '*test.py' diff --git a/commit b/commit old mode 100755 new mode 100644 index 4c4ba8f..3446a8d --- a/commit +++ b/commit @@ -10,6 +10,9 @@ def main(): # Format with Deno subprocess.run(["deno", "fmt", "."], check=True) + # Run tests + subprocess.run(["python3", "-m", "unittest", "discover"], check=True) + # Git add all changes subprocess.run(["git", "add", "."], check=True) @@ -25,4 +28,4 @@ def main(): subprocess.run(["git", "push"], check=True) if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/leetcode/01-two-sum/two_sum_test.py b/leetcode/01-two-sum/two_sum_test.py new file mode 100644 index 0000000..cc66d68 --- /dev/null +++ b/leetcode/01-two-sum/two_sum_test.py @@ -0,0 +1,24 @@ +import unittest +from two_sum import Solution + +class TestTwoSum(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.twoSum([2, 7, 11, 15], 9), [1, 0]) + + def test_no_solution(self): + self.assertIsNone(self.solution.twoSum([1, 2, 3], 7)) + + def test_multiple_solutions(self): + self.assertEqual(self.solution.twoSum([1, 2, 3, 4, 5], 6), [3, 1]) + + def test_negative_numbers(self): + self.assertEqual(self.solution.twoSum([-1, -2, -3, -4, -5], -8), [4, 2]) + + def test_mixed_numbers(self): + self.assertEqual(self.solution.twoSum([-1, 2, 3, -4, 5], 1), [4, 0]) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/21-merge-two-sorted-lists/implementation_test.py b/leetcode/21-merge-two-sorted-lists/implementation_test.py new file mode 100644 index 0000000..25f5a81 --- /dev/null +++ b/leetcode/21-merge-two-sorted-lists/implementation_test.py @@ -0,0 +1,44 @@ +import unittest +from implementation import Solution, ListNode + +class TestDeleteDuplicates(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def list_to_linkedlist(self, elements): + head = ListNode(elements[0]) + current = head + for element in elements[1:]: + current.next = ListNode(element) + current = current.next + return head + + def linkedlist_to_list(self, head): + elements = [] + while head: + elements.append(head.val) + head = head.next + return elements + + def test_example_case(self): + head = self.list_to_linkedlist([1, 1, 2]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1, 2]) + + def test_no_duplicates(self): + head = self.list_to_linkedlist([1, 2, 3]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3]) + + def test_all_duplicates(self): + head = self.list_to_linkedlist([1, 1, 1]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1]) + + def test_mixed_duplicates(self): + head = self.list_to_linkedlist([1, 1, 2, 3, 3]) + result = self.solution.deleteDuplicates(head) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3]) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/21-merge-two-sorted-lists/slow_test.py b/leetcode/21-merge-two-sorted-lists/slow_test.py new file mode 100644 index 0000000..01c96f7 --- /dev/null +++ b/leetcode/21-merge-two-sorted-lists/slow_test.py @@ -0,0 +1,54 @@ +import unittest +from slow import Solution, ListNode + +class TestMergeTwoLists(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def list_to_linkedlist(self, elements): + head = ListNode(elements[0]) + current = head + for element in elements[1:]: + current.next = ListNode(element) + current = current.next + return head + + def linkedlist_to_list(self, head): + elements = [] + while head: + elements.append(head.val) + head = head.next + return elements + + def test_example_case(self): + list1 = self.list_to_linkedlist([1, 2, 4]) + list2 = self.list_to_linkedlist([1, 3, 4]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 1, 2, 3, 4, 4]) + + def test_empty_lists(self): + list1 = self.list_to_linkedlist([]) + list2 = self.list_to_linkedlist([]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), []) + + def test_one_empty_list(self): + list1 = self.list_to_linkedlist([1, 2, 3]) + list2 = self.list_to_linkedlist([]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3]) + + def test_no_overlap(self): + list1 = self.list_to_linkedlist([1, 2, 3]) + list2 = self.list_to_linkedlist([4, 5, 6]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 2, 3, 4, 5, 6]) + + def test_all_overlap(self): + list1 = self.list_to_linkedlist([1, 1, 1]) + list2 = self.list_to_linkedlist([1, 1, 1]) + result = self.solution.mergeTwoLists(list1, list2) + self.assertEqual(self.linkedlist_to_list(result), [1, 1, 1, 1, 1, 1]) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/35-search-insert-position/binary_search_test.py b/leetcode/35-search-insert-position/binary_search_test.py new file mode 100644 index 0000000..16b324d --- /dev/null +++ b/leetcode/35-search-insert-position/binary_search_test.py @@ -0,0 +1,24 @@ +import unittest +from binary_search import Solution + +class TestSearchInsert(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 5), 2) + + def test_insert_position(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 2), 1) + + def test_insert_at_end(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 7), 4) + + def test_insert_at_start(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 0), 0) + + def test_empty_list(self): + self.assertEqual(self.solution.searchInsert([], 5), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/35-search-insert-position/simple_test.py b/leetcode/35-search-insert-position/simple_test.py new file mode 100644 index 0000000..f6ac1db --- /dev/null +++ b/leetcode/35-search-insert-position/simple_test.py @@ -0,0 +1,24 @@ +import unittest +from simple import Solution + +class TestSearchInsert(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 5), 2) + + def test_insert_position(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 2), 1) + + def test_insert_at_end(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 7), 4) + + def test_insert_at_start(self): + self.assertEqual(self.solution.searchInsert([1, 3, 5, 6], 0), 0) + + def test_empty_list(self): + self.assertEqual(self.solution.searchInsert([], 5), 0) + +if __name__ == '__main__': + unittest.main() diff --git a/leetcode/704-binary-search/normal_python_test.py b/leetcode/704-binary-search/normal_python_test.py new file mode 100644 index 0000000..49a4e4e --- /dev/null +++ b/leetcode/704-binary-search/normal_python_test.py @@ -0,0 +1,27 @@ +import unittest +from normal_python import Solution + +class TestBinarySearch(unittest.TestCase): + def setUp(self): + self.solution = Solution() + + def test_example_case(self): + self.assertEqual(self.solution.binary_search([1, 2, 3, 4, 5], 3), 2) + + def test_target_not_found(self): + self.assertEqual(self.solution.binary_search([1, 2, 3, 4, 5], 6), -1) + + def test_empty_list(self): + self.assertEqual(self.solution.binary_search([], 3), -1) + + def test_single_element_found(self): + self.assertEqual(self.solution.binary_search([3], 3), 0) + + def test_single_element_not_found(self): + self.assertEqual(self.solution.binary_search([1], 3), -1) + + def test_multiple_occurrences(self): + self.assertEqual(self.solution.binary_search([1, 2, 3, 3, 3, 4, 5], 3), 2) + +if __name__ == '__main__': + unittest.main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29