From 9f7299e3a008b6774b9c5573feee1198e5792cc1 Mon Sep 17 00:00:00 2001 From: mathusanm6 Date: Sun, 31 Aug 2025 09:37:08 +0200 Subject: [PATCH 1/4] feat: add problem Contains Duplicate with unit testing --- README.md | 5 +- problems/contains_duplicate/config.yml | 17 ++++ .../contains_duplicate/contains_duplicate.cc | 16 ++++ .../contains_duplicate/contains_duplicate.h | 3 + .../contains_duplicate/contains_duplicate.py | 10 +++ .../contains_duplicate_test.cc | 77 +++++++++++++++++++ .../contains_duplicate_test.py | 50 ++++++++++++ 7 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 problems/contains_duplicate/config.yml create mode 100644 problems/contains_duplicate/contains_duplicate.cc create mode 100644 problems/contains_duplicate/contains_duplicate.h create mode 100644 problems/contains_duplicate/contains_duplicate.py create mode 100644 problems/contains_duplicate/contains_duplicate_test.cc create mode 100644 problems/contains_duplicate/contains_duplicate_test.py diff --git a/README.md b/README.md index c7a7e66..c0ce41a 100644 --- a/README.md +++ b/README.md @@ -15,8 +15,8 @@ ### 📊 Repository Stats [![Last Commit](https://img.shields.io/github/last-commit/mathusanm6/LeetCode?style=for-the-badge&logo=git&logoColor=white)](https://github.com/mathusanm6/LeetCode/commits/main) -[![C++ Solutions](https://img.shields.io/badge/C%2B%2B%20Solutions-2-blue?style=for-the-badge&logo=cplusplus&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems) -[![Python Solutions](https://img.shields.io/badge/Python%20Solutions-2-green?style=for-the-badge&logo=python&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems) +[![C++ Solutions](https://img.shields.io/badge/C%2B%2B%20Solutions-3-blue?style=for-the-badge&logo=cplusplus&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems) +[![Python Solutions](https://img.shields.io/badge/Python%20Solutions-3-green?style=for-the-badge&logo=python&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems) @@ -104,6 +104,7 @@ make lint-python # Lint Python files with ruff | # | Title | Solution | Time | Space | Difficulty | Tag | Note | |---|-------|----------|------|-------|------------|-----|------| | 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](./problems/two_sum/two_sum.py), [C++](./problems/two_sum/two_sum.cc) | _O(n)_ | _O(n)_ | Easy | | | +| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Python](./problems/contains_duplicate/contains_duplicate.py), [C++](./problems/contains_duplicate/contains_duplicate.cc) | _O(n)_ | _O(n)_ | Easy | | | ## Two Pointers diff --git a/problems/contains_duplicate/config.yml b/problems/contains_duplicate/config.yml new file mode 100644 index 0000000..82812f8 --- /dev/null +++ b/problems/contains_duplicate/config.yml @@ -0,0 +1,17 @@ +problem: + number: 217 + title: "Contains Duplicate" + leetcode_url: "https://leetcode.com/problems/contains-duplicate/description/" + difficulty: "easy" + tags: ["Arrays & Hashing"] + +solutions: + python: "problems/contains_duplicate/contains_duplicate.py" + cpp: "problems/contains_duplicate/contains_duplicate.cc" + +complexity: + time: "O(n)" + space: "O(n)" + +notes: "" +readme_link: "" diff --git a/problems/contains_duplicate/contains_duplicate.cc b/problems/contains_duplicate/contains_duplicate.cc new file mode 100644 index 0000000..055b524 --- /dev/null +++ b/problems/contains_duplicate/contains_duplicate.cc @@ -0,0 +1,16 @@ +#include "contains_duplicate.h" + +#include +#include + +bool containsDuplicate(std::vector& nums) { + std::unordered_set seen; + seen.reserve(nums.size()); + for (int& num : nums) { + if (auto iter = seen.find(num); iter != seen.end()) { + return true; + } + seen.insert(num); + } + return false; +} diff --git a/problems/contains_duplicate/contains_duplicate.h b/problems/contains_duplicate/contains_duplicate.h new file mode 100644 index 0000000..96a4070 --- /dev/null +++ b/problems/contains_duplicate/contains_duplicate.h @@ -0,0 +1,3 @@ +#include + +bool containsDuplicate(std::vector& nums); \ No newline at end of file diff --git a/problems/contains_duplicate/contains_duplicate.py b/problems/contains_duplicate/contains_duplicate.py new file mode 100644 index 0000000..d2db556 --- /dev/null +++ b/problems/contains_duplicate/contains_duplicate.py @@ -0,0 +1,10 @@ +from typing import List + + +def containsDuplicate(nums: List[int]) -> bool: + seen = set() + for num in nums: + if num in seen: + return True + seen.add(num) + return False diff --git a/problems/contains_duplicate/contains_duplicate_test.cc b/problems/contains_duplicate/contains_duplicate_test.cc new file mode 100644 index 0000000..b0acedf --- /dev/null +++ b/problems/contains_duplicate/contains_duplicate_test.cc @@ -0,0 +1,77 @@ +#include "contains_duplicate.h" + +#include +#include +#include + +struct ContainsDuplicateCase { + std::string test_name; + std::vector nums; + bool expected; +}; + +using ContainsDuplicateTest = ::testing::TestWithParam; + +TEST_P(ContainsDuplicateTest, TestCases) { + const ContainsDuplicateCase &testCase = GetParam(); + std::vector nums = testCase.nums; // Copy since function takes non-const reference + const bool result = containsDuplicate(nums); + EXPECT_EQ(result, testCase.expected); +} + +INSTANTIATE_TEST_SUITE_P( + ContainsDuplicateTestCases, ContainsDuplicateTest, + ::testing::Values( + // Boundary conditions + ContainsDuplicateCase{.test_name = "EmptyArray", .nums = {}, .expected = false}, + ContainsDuplicateCase{.test_name = "SingleElement", .nums = {42}, .expected = false}, + + // Basic cases + ContainsDuplicateCase{.test_name = "TwoUnique", .nums = {1, 2}, .expected = false}, + ContainsDuplicateCase{.test_name = "TwoDuplicate", .nums = {1, 1}, .expected = true}, + ContainsDuplicateCase{.test_name = "ThreeUnique", .nums = {1, 2, 3}, .expected = false}, + ContainsDuplicateCase{ + .test_name = "ThreeWithDuplicate", .nums = {1, 2, 1}, .expected = true}, + + // Edge cases with special values + ContainsDuplicateCase{.test_name = "ZeroDuplicate", .nums = {0, 1, 0}, .expected = true}, + ContainsDuplicateCase{ + .test_name = "NegativeDuplicate", .nums = {-1, -2, -1}, .expected = true}, + ContainsDuplicateCase{ + .test_name = "MinMaxValues", .nums = {INT_MIN, INT_MAX, INT_MIN}, .expected = true}, + + // Position sensitivity + ContainsDuplicateCase{ + .test_name = "DuplicateAtEnd", .nums = {1, 2, 3, 4, 5, 1}, .expected = true}, + ContainsDuplicateCase{ + .test_name = "DuplicateAtBoundaries", .nums = {5, 1, 2, 3, 4, 5}, .expected = true}, + + // Performance edge cases + ContainsDuplicateCase{.test_name = "LargeUniqueArray", + .nums = + []() { + std::vector v; + for (int i = 0; i < 100; ++i) v.push_back(i); + return v; + }(), + .expected = false}, + ContainsDuplicateCase{.test_name = "ManyDuplicates", + .nums = + []() { + std::vector v(50, 1); + v.push_back(2); + return v; + }(), + .expected = true}, + ContainsDuplicateCase{.test_name = "LargeArrayOneDuplicate", + .nums = + []() { + std::vector v; + for (int i = 0; i < 5000; ++i) v.push_back(i); + v.push_back(2500); // Introduce a single duplicate + return v; + }(), + .expected = true}), + [](const ::testing::TestParamInfo &info) { + return info.param.test_name; + }); \ No newline at end of file diff --git a/problems/contains_duplicate/contains_duplicate_test.py b/problems/contains_duplicate/contains_duplicate_test.py new file mode 100644 index 0000000..783c7bb --- /dev/null +++ b/problems/contains_duplicate/contains_duplicate_test.py @@ -0,0 +1,50 @@ +"""Test cases for the contains_duplicate function.""" + +import pytest + +from contains_duplicate import containsDuplicate + + +@pytest.mark.parametrize( + "nums, expected", + [ + # Boundary conditions + ([], False), + ([42], False), + # Basic cases + ([1, 2], False), + ([1, 1], True), + ([1, 2, 3], False), + ([1, 2, 1], True), + # Edge cases with special values + ([0, 1, 0], True), + ([-1, -2, -1], True), + ([0, -0], True), # 0 and -0 are equal + # Position sensitivity + ([1, 2, 3, 4, 5, 1], True), # duplicate at end + ([5, 1, 2, 3, 4, 5], True), # duplicate at start/end + # Performance edge cases + (list(range(100)), False), # no duplicates, larger array + ([1] * 50 + [2], True), # many duplicates + (list(range(5000)) + [2500], True), # large array with one duplicate + ], + ids=[ + "empty_array", + "single_element", + "two_unique", + "two_duplicate", + "three_unique", + "three_with_duplicate", + "zero_duplicate", + "negative_duplicate", + "zero_negative_zero", + "duplicate_at_end", + "duplicate_at_boundaries", + "large_unique_array", + "many_duplicates", + "large_array_one_duplicate", + ], +) +def test_contains_duplicate(nums, expected): + """Test containsDuplicate with various input scenarios.""" + assert containsDuplicate(nums) == expected From f036dcd2708ead57718b17f186534c0b92274198 Mon Sep 17 00:00:00 2001 From: Mathusan Selvakumar Date: Sun, 31 Aug 2025 09:39:39 +0200 Subject: [PATCH 2/4] Update problems/contains_duplicate/contains_duplicate.cc Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- problems/contains_duplicate/contains_duplicate.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/contains_duplicate/contains_duplicate.cc b/problems/contains_duplicate/contains_duplicate.cc index 055b524..f4c038f 100644 --- a/problems/contains_duplicate/contains_duplicate.cc +++ b/problems/contains_duplicate/contains_duplicate.cc @@ -6,7 +6,7 @@ bool containsDuplicate(std::vector& nums) { std::unordered_set seen; seen.reserve(nums.size()); - for (int& num : nums) { + for (const int& num : nums) { if (auto iter = seen.find(num); iter != seen.end()) { return true; } From b0029287144336edf1a29a8756b523cd3913f730 Mon Sep 17 00:00:00 2001 From: mathusanm6 Date: Sun, 31 Aug 2025 10:00:52 +0200 Subject: [PATCH 3/4] fix: lint warning for cpp --- .../contains_duplicate/contains_duplicate_test.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/problems/contains_duplicate/contains_duplicate_test.cc b/problems/contains_duplicate/contains_duplicate_test.cc index b0acedf..2a407d6 100644 --- a/problems/contains_duplicate/contains_duplicate_test.cc +++ b/problems/contains_duplicate/contains_duplicate_test.cc @@ -1,6 +1,7 @@ #include "contains_duplicate.h" #include +#include #include #include @@ -51,7 +52,10 @@ INSTANTIATE_TEST_SUITE_P( .nums = []() { std::vector v; - for (int i = 0; i < 100; ++i) v.push_back(i); + v.reserve(100); + for (int i = 0; i < 100; ++i) { + v.push_back(i); + } return v; }(), .expected = false}, @@ -67,7 +71,10 @@ INSTANTIATE_TEST_SUITE_P( .nums = []() { std::vector v; - for (int i = 0; i < 5000; ++i) v.push_back(i); + v.reserve(5001); + for (int i = 0; i < 5000; ++i) { + v.push_back(i); + } v.push_back(2500); // Introduce a single duplicate return v; }(), From b3505e60e0dc93878292be772e5ba64086a5a9d1 Mon Sep 17 00:00:00 2001 From: mathusanm6 Date: Sun, 31 Aug 2025 10:03:28 +0200 Subject: [PATCH 4/4] cleanup: remove archives/ directory --- archives/python/helpers/list.py | 9 ---- .../easy/best_time_to_buy_and_sell_stock.py | 13 ------ .../problems/easy/contains_duplicate.py | 11 ----- .../python/problems/easy/linked_list_cycle.py | 20 -------- .../easy/maximum_depth_of_binary_tree.py | 24 ---------- .../problems/easy/merge_sorted_array.py | 21 --------- .../minimum_absolute_difference_in_bst.py | 30 ------------ archives/python/problems/easy/ransom_note.py | 20 -------- .../python/problems/easy/summary_ranges.py | 31 ------------- .../python/problems/easy/valid_anagram.py | 14 ------ .../python/problems/easy/valid_parentheses.py | 24 ---------- archives/python/problems/hard/candy.py | 31 ------------- .../problems/hard/minimum_window_substring.py | 34 -------------- .../problems/hard/sliding_window_maximum.py | 25 ---------- .../problems/hard/trapping_rain_water_o_1.py | 30 ------------ .../problems/hard/trapping_rain_water_o_n.py | 20 -------- .../medium/binary_tree_right_side_view.py | 32 ------------- .../medium/container_with_most_water.py | 15 ------ .../medium/encode_and_decode_strings.py | 37 --------------- .../evaluate_reverse_polish_notation.py | 22 --------- .../problems/medium/generate_parentheses.py | 17 ------- .../python/problems/medium/group_anagrams.py | 13 ------ .../medium/longest_consecutive_sequence.py | 20 -------- ...longest_repeating_character_replacement.py | 15 ------ ..._substring_without_repeating_characters.py | 17 ------- archives/python/problems/medium/min_stack.py | 19 -------- .../medium/minimum_size_subarray_sum.py | 27 ----------- .../problems/medium/number_of_islands.py | 23 ---------- .../problems/medium/permutation_in_string.py | 23 ---------- .../medium/product_of_array_except_self.py | 18 -------- .../problems/medium/snakes_and_ladders.py | 45 ------------------ archives/python/problems/medium/three_sum.py | 24 ---------- .../medium/top_k_frequent_elements.py | 14 ------ .../two_sum_ii_input_array_is_sorted.py | 15 ------ .../python/problems/medium/valid_sudoku.py | 36 --------------- archives/python/tests/__init__.py | 0 .../test_best_time_to_buy_and_sell_stock.py | 13 ------ .../tests/test_binary_tree_right_side_view.py | 37 --------------- archives/python/tests/test_candy.py | 37 --------------- .../tests/test_container_with_most_water.py | 13 ------ .../python/tests/test_contains_duplicate.py | 18 -------- .../tests/test_encode_and_decode_strings.py | 18 -------- .../test_evaluate_reverse_polish_notation.py | 18 -------- .../python/tests/test_generate_parentheses.py | 15 ------ archives/python/tests/test_group_anagrams.py | 29 ------------ .../python/tests/test_linked_list_cycle.py | 27 ----------- .../test_longest_consecutive_sequence.py | 23 ---------- ...longest_repeating_character_replacement.py | 15 ------ ..._substring_without_repeating_characters.py | 18 -------- .../test_maximum_depth_of_binary_tree.py | 34 -------------- .../python/tests/test_merge_sorted_array.py | 29 ------------ archives/python/tests/test_min_stack.py | 13 ------ ...test_minimum_absolute_difference_in_bst.py | 46 ------------------- .../tests/test_minimum_size_subarray_sum.py | 17 ------- .../tests/test_minimum_window_substring.py | 21 --------- .../python/tests/test_number_of_islands.py | 23 ---------- .../tests/test_permutation_in_string.py | 21 --------- .../test_product_of_array_except_self.py | 13 ------ archives/python/tests/test_ransom_note.py | 30 ------------ .../tests/test_sliding_window_maximum.py | 15 ------ .../python/tests/test_snakes_and_ladders.py | 20 -------- archives/python/tests/test_summary_ranges.py | 25 ---------- archives/python/tests/test_three_sum.py | 28 ----------- .../tests/test_top_k_frequent_elements.py | 21 --------- .../python/tests/test_trapping_rain_water.py | 24 ---------- .../test_two_sum_ii_input_array_is_sorted.py | 21 --------- archives/python/tests/test_valid_anagram.py | 27 ----------- .../python/tests/test_valid_parentheses.py | 38 --------------- archives/python/tests/test_valid_sudoku.py | 33 ------------- 69 files changed, 1569 deletions(-) delete mode 100644 archives/python/helpers/list.py delete mode 100644 archives/python/problems/easy/best_time_to_buy_and_sell_stock.py delete mode 100644 archives/python/problems/easy/contains_duplicate.py delete mode 100644 archives/python/problems/easy/linked_list_cycle.py delete mode 100644 archives/python/problems/easy/maximum_depth_of_binary_tree.py delete mode 100644 archives/python/problems/easy/merge_sorted_array.py delete mode 100644 archives/python/problems/easy/minimum_absolute_difference_in_bst.py delete mode 100644 archives/python/problems/easy/ransom_note.py delete mode 100644 archives/python/problems/easy/summary_ranges.py delete mode 100644 archives/python/problems/easy/valid_anagram.py delete mode 100644 archives/python/problems/easy/valid_parentheses.py delete mode 100644 archives/python/problems/hard/candy.py delete mode 100644 archives/python/problems/hard/minimum_window_substring.py delete mode 100644 archives/python/problems/hard/sliding_window_maximum.py delete mode 100644 archives/python/problems/hard/trapping_rain_water_o_1.py delete mode 100644 archives/python/problems/hard/trapping_rain_water_o_n.py delete mode 100644 archives/python/problems/medium/binary_tree_right_side_view.py delete mode 100644 archives/python/problems/medium/container_with_most_water.py delete mode 100644 archives/python/problems/medium/encode_and_decode_strings.py delete mode 100644 archives/python/problems/medium/evaluate_reverse_polish_notation.py delete mode 100644 archives/python/problems/medium/generate_parentheses.py delete mode 100644 archives/python/problems/medium/group_anagrams.py delete mode 100644 archives/python/problems/medium/longest_consecutive_sequence.py delete mode 100644 archives/python/problems/medium/longest_repeating_character_replacement.py delete mode 100644 archives/python/problems/medium/longest_substring_without_repeating_characters.py delete mode 100644 archives/python/problems/medium/min_stack.py delete mode 100644 archives/python/problems/medium/minimum_size_subarray_sum.py delete mode 100644 archives/python/problems/medium/number_of_islands.py delete mode 100644 archives/python/problems/medium/permutation_in_string.py delete mode 100644 archives/python/problems/medium/product_of_array_except_self.py delete mode 100644 archives/python/problems/medium/snakes_and_ladders.py delete mode 100644 archives/python/problems/medium/three_sum.py delete mode 100644 archives/python/problems/medium/top_k_frequent_elements.py delete mode 100644 archives/python/problems/medium/two_sum_ii_input_array_is_sorted.py delete mode 100644 archives/python/problems/medium/valid_sudoku.py delete mode 100644 archives/python/tests/__init__.py delete mode 100644 archives/python/tests/test_best_time_to_buy_and_sell_stock.py delete mode 100644 archives/python/tests/test_binary_tree_right_side_view.py delete mode 100644 archives/python/tests/test_candy.py delete mode 100644 archives/python/tests/test_container_with_most_water.py delete mode 100644 archives/python/tests/test_contains_duplicate.py delete mode 100644 archives/python/tests/test_encode_and_decode_strings.py delete mode 100644 archives/python/tests/test_evaluate_reverse_polish_notation.py delete mode 100644 archives/python/tests/test_generate_parentheses.py delete mode 100644 archives/python/tests/test_group_anagrams.py delete mode 100644 archives/python/tests/test_linked_list_cycle.py delete mode 100644 archives/python/tests/test_longest_consecutive_sequence.py delete mode 100644 archives/python/tests/test_longest_repeating_character_replacement.py delete mode 100644 archives/python/tests/test_longest_substring_without_repeating_characters.py delete mode 100644 archives/python/tests/test_maximum_depth_of_binary_tree.py delete mode 100644 archives/python/tests/test_merge_sorted_array.py delete mode 100644 archives/python/tests/test_min_stack.py delete mode 100644 archives/python/tests/test_minimum_absolute_difference_in_bst.py delete mode 100644 archives/python/tests/test_minimum_size_subarray_sum.py delete mode 100644 archives/python/tests/test_minimum_window_substring.py delete mode 100644 archives/python/tests/test_number_of_islands.py delete mode 100644 archives/python/tests/test_permutation_in_string.py delete mode 100644 archives/python/tests/test_product_of_array_except_self.py delete mode 100644 archives/python/tests/test_ransom_note.py delete mode 100644 archives/python/tests/test_sliding_window_maximum.py delete mode 100644 archives/python/tests/test_snakes_and_ladders.py delete mode 100644 archives/python/tests/test_summary_ranges.py delete mode 100644 archives/python/tests/test_three_sum.py delete mode 100644 archives/python/tests/test_top_k_frequent_elements.py delete mode 100644 archives/python/tests/test_trapping_rain_water.py delete mode 100644 archives/python/tests/test_two_sum_ii_input_array_is_sorted.py delete mode 100644 archives/python/tests/test_valid_anagram.py delete mode 100644 archives/python/tests/test_valid_parentheses.py delete mode 100644 archives/python/tests/test_valid_sudoku.py diff --git a/archives/python/helpers/list.py b/archives/python/helpers/list.py deleted file mode 100644 index c1bbbc4..0000000 --- a/archives/python/helpers/list.py +++ /dev/null @@ -1,9 +0,0 @@ -def equal_ignore_order(a, b): - """Use only when elements are neither hashable or sortable!""" - unmatched = list(b) - for element in a: - try: - unmatched.remove(element) - except ValueError: - return False - return not unmatched diff --git a/archives/python/problems/easy/best_time_to_buy_and_sell_stock.py b/archives/python/problems/easy/best_time_to_buy_and_sell_stock.py deleted file mode 100644 index b50af3d..0000000 --- a/archives/python/problems/easy/best_time_to_buy_and_sell_stock.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import List - - -class Solution: - def maxProfit(self, prices: List[int]) -> int: - min_price = float("inf") - max_profit = 0 - for i in range(len(prices)): - if prices[i] < min_price: - min_price = prices[i] - elif prices[i] - min_price > max_profit: - max_profit = prices[i] - min_price - return max_profit diff --git a/archives/python/problems/easy/contains_duplicate.py b/archives/python/problems/easy/contains_duplicate.py deleted file mode 100644 index f10b420..0000000 --- a/archives/python/problems/easy/contains_duplicate.py +++ /dev/null @@ -1,11 +0,0 @@ -from typing import List - - -class Solution: - def containsDuplicate(self, nums: List[int]) -> bool: - seen = set() - for val in nums: - if val in seen: # Duplicate value found - return True - seen.add(val) - return False diff --git a/archives/python/problems/easy/linked_list_cycle.py b/archives/python/problems/easy/linked_list_cycle.py deleted file mode 100644 index 79031c3..0000000 --- a/archives/python/problems/easy/linked_list_cycle.py +++ /dev/null @@ -1,20 +0,0 @@ -# Definition for singly-linked list. -class ListNode(object): - def __init__(self, x): - self.val = x - self.next = None - - -class Solution(object): - def hasCycle(self, head): - """ - :type head: ListNode - :rtype: bool - """ - fast = slow = head - while fast and fast.next: - slow = slow.next - fast = fast.next.next - if fast == slow: - return True - return False diff --git a/archives/python/problems/easy/maximum_depth_of_binary_tree.py b/archives/python/problems/easy/maximum_depth_of_binary_tree.py deleted file mode 100644 index a8bee5e..0000000 --- a/archives/python/problems/easy/maximum_depth_of_binary_tree.py +++ /dev/null @@ -1,24 +0,0 @@ -# Definition for a binary tree node. -class TreeNode(object): - def __init__(self, val=0, left=None, right=None): - self.val = val - self.left = left - self.right = right - - -class Solution(object): - def maxDepth(self, root): - """ - :type root: TreeNode - :rtype: int - """ - if not root: - return 0 - - left = 0 - if root.left: - left += self.maxDepth(self, root.left) - right = 0 - if root.right: - right += self.maxDepth(self, root.right) - return 1 + max(left, right) diff --git a/archives/python/problems/easy/merge_sorted_array.py b/archives/python/problems/easy/merge_sorted_array.py deleted file mode 100644 index a57d38c..0000000 --- a/archives/python/problems/easy/merge_sorted_array.py +++ /dev/null @@ -1,21 +0,0 @@ -class Solution(object): - def merge(self, nums1, m, nums2, n): - """ - :type nums1: List[int] - :type m: int - :type nums2: List[int] - :type n: int - :rtype: None Do not return anything, modify nums1 in-place instead. - """ - i = m - 1 - j = n - 1 - k = m + n - 1 - - while j >= 0: - if i >= 0 and nums1[i] > nums2[j]: - nums1[k] = nums1[i] - i -= 1 - else: - nums1[k] = nums2[j] - j -= 1 - k -= 1 diff --git a/archives/python/problems/easy/minimum_absolute_difference_in_bst.py b/archives/python/problems/easy/minimum_absolute_difference_in_bst.py deleted file mode 100644 index 32c7357..0000000 --- a/archives/python/problems/easy/minimum_absolute_difference_in_bst.py +++ /dev/null @@ -1,30 +0,0 @@ -# Definition for a binary tree node. -class TreeNode(object): - def __init__(self, val=0, left=None, right=None): - self.val = val - self.left = left - self.right = right - - -class Solution(object): - def __init__(self): - self.prev = float("inf") - self.ans = float("inf") - - def inOrder(self, node): - if node.left: - self.inOrder(node.left) - - self.ans = min(self.ans, abs(node.val - self.prev)) - self.prev = node.val - - if node.right: - self.inOrder(node.right) - - def getMinimumDifference(self, root): - """ - :type root: TreeNode - :rtype: int - """ - self.inOrder(root) - return self.ans diff --git a/archives/python/problems/easy/ransom_note.py b/archives/python/problems/easy/ransom_note.py deleted file mode 100644 index d7f7999..0000000 --- a/archives/python/problems/easy/ransom_note.py +++ /dev/null @@ -1,20 +0,0 @@ -class Solution(object): - def canConstruct(self, ransomNote, magazine): - """ - :type ransomNote: str - :type magazine: str - :rtype: bool - """ - - def index(c): - return ord(c) - ord("a") - - count_l = [0] * 26 - for c in magazine: - count_l[index(c)] += 1 - for c in ransomNote: - idx_c = index(c) - count_l[idx_c] -= 1 - if count_l[idx_c] < 0: - return False - return True diff --git a/archives/python/problems/easy/summary_ranges.py b/archives/python/problems/easy/summary_ranges.py deleted file mode 100644 index 7c302de..0000000 --- a/archives/python/problems/easy/summary_ranges.py +++ /dev/null @@ -1,31 +0,0 @@ -class Solution(object): - def summaryRanges(self, nums): - """ - :type nums: List[int] - :rtype: List[str] - """ - if not nums: - return nums - - res = [] - - start = nums[0] - offset = 1 - for i in range(1, len(nums)): - if start + offset == nums[i]: - offset += 1 - continue - else: - if offset > 1: - res.append(str(start) + "->" + str(start + offset - 1)) - - else: - res.append(str(start)) - - start = nums[i] - offset = 1 - if offset > 1: - res.append(str(start) + "->" + str(start + offset - 1)) - else: - res.append(str(start)) - return res diff --git a/archives/python/problems/easy/valid_anagram.py b/archives/python/problems/easy/valid_anagram.py deleted file mode 100644 index e5b83c5..0000000 --- a/archives/python/problems/easy/valid_anagram.py +++ /dev/null @@ -1,14 +0,0 @@ -import collections - - -class Solution: - def isAnagram(self, s: str, t: str) -> bool: - diff_map = collections.defaultdict(int) - for c in s: - diff_map[c] += 1 - for c in t: - diff_map[c] -= 1 - for val in diff_map.values(): - if val != 0: - return False - return True diff --git a/archives/python/problems/easy/valid_parentheses.py b/archives/python/problems/easy/valid_parentheses.py deleted file mode 100644 index 466adde..0000000 --- a/archives/python/problems/easy/valid_parentheses.py +++ /dev/null @@ -1,24 +0,0 @@ -class Solution(object): - def isValid(self, s: str) -> bool: - def isOpenBracket(c): - open_ = "([{" - return c in open_ - - def correspondingBracket(c): - close = ")]}" - open_ = "([{" - for i in range(len(close)): - if close[i] == c: - return open_[i] - return None - - stack = [] - for c in s: - if isOpenBracket(c): - stack.append(c) - else: - if len(stack) > 0 and correspondingBracket(c) == stack[-1]: - stack.pop() - else: - return False - return len(stack) == 0 diff --git a/archives/python/problems/hard/candy.py b/archives/python/problems/hard/candy.py deleted file mode 100644 index 09fea12..0000000 --- a/archives/python/problems/hard/candy.py +++ /dev/null @@ -1,31 +0,0 @@ -from typing import List - - -class Solution: - def candy(self, ratings: List[int]) -> int: - def slope(i): - if i < 0 or i >= n: - return None - - if ratings[i] > ratings[i + 1]: - return -1 - elif ratings[i] < ratings[i + 1]: - return 1 - else: - return 0 - - n = len(ratings) - - steep_arr = [slope(i) for i in range(n - 1)] - - candies = [1] * n - - for left in range(n - 1): - if steep_arr[left] == 1 and candies[left + 1] <= candies[left]: - candies[left + 1] = candies[left] + 1 - - for right in range(n - 1, 0, -1): - if steep_arr[right - 1] == -1 and candies[right] >= candies[right - 1]: - candies[right - 1] = candies[right] + 1 - - return sum(candies) diff --git a/archives/python/problems/hard/minimum_window_substring.py b/archives/python/problems/hard/minimum_window_substring.py deleted file mode 100644 index 3b29aef..0000000 --- a/archives/python/problems/hard/minimum_window_substring.py +++ /dev/null @@ -1,34 +0,0 @@ -import collections - - -class Solution: - def minWindow(self, s: str, t: str) -> str: - if len(s) < len(t): - return "" - - freq_t = collections.Counter(t) - freq_window = collections.defaultdict(int) - left = 0 - have = 0 - need = len(freq_t) - ans = (float("inf"), 0, 0) - - for right in range(len(s)): - c = s[right] - if c in freq_t: - freq_window[c] += 1 - if freq_window[c] == freq_t[c]: - have += 1 - - while have == need: - if right - left + 1 < ans[0]: - ans = (right - left + 1, left, right) - tmp = s[left] - if tmp in freq_t: - freq_window[tmp] -= 1 - if freq_window[tmp] < freq_t[tmp]: - have -= 1 - left += 1 - - left, right = ans[1], ans[2] - return s[left : right + 1] if ans[0] != float("inf") else "" diff --git a/archives/python/problems/hard/sliding_window_maximum.py b/archives/python/problems/hard/sliding_window_maximum.py deleted file mode 100644 index cc35df0..0000000 --- a/archives/python/problems/hard/sliding_window_maximum.py +++ /dev/null @@ -1,25 +0,0 @@ -from collections import deque -from typing import List - - -class Solution: - def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]: - dq = deque() - res = [] - - for i in range(k): - while dq and nums[i] >= nums[dq[-1]]: - dq.pop() - dq.append(i) - - res.append(nums[dq[0]]) - - for i in range(k, len(nums)): - if dq and dq[0] == i - k: - dq.popleft() - while dq and nums[i] >= nums[dq[-1]]: - dq.pop() - dq.append(i) - res.append(nums[dq[0]]) - - return res diff --git a/archives/python/problems/hard/trapping_rain_water_o_1.py b/archives/python/problems/hard/trapping_rain_water_o_1.py deleted file mode 100644 index cfc6a15..0000000 --- a/archives/python/problems/hard/trapping_rain_water_o_1.py +++ /dev/null @@ -1,30 +0,0 @@ -from typing import List - - -class Solution: - def trap(self, height: List[int]) -> int: - n = len(height) - - leftMax = height[0] # O(1) space - rightMax = height[n - 1] - - left = 1 - right = n - 2 - - trapped_water = 0 - - while left <= right: - if leftMax < rightMax: - if height[left] > leftMax: - leftMax = height[left] - else: - trapped_water += leftMax - height[left] - left += 1 - else: - if height[right] > rightMax: - rightMax = height[right] - else: - trapped_water += rightMax - height[right] - right -= 1 - - return trapped_water diff --git a/archives/python/problems/hard/trapping_rain_water_o_n.py b/archives/python/problems/hard/trapping_rain_water_o_n.py deleted file mode 100644 index 1e1e3c5..0000000 --- a/archives/python/problems/hard/trapping_rain_water_o_n.py +++ /dev/null @@ -1,20 +0,0 @@ -from typing import List - - -class Solution: - def trap(self, height: List[int]) -> int: - n = len(height) - - leftmost = [0] * n # O(n) space - rightmost = [0] * n - - for i in range(1, n): - leftmost[i] = max(leftmost[i - 1], height[i - 1]) - rightmost[n - 1 - i] = max(rightmost[n - i], height[n - i]) - - trapped_water = 0 - - for i in range(n): - trapped_water += max(0, min(leftmost[i], rightmost[i]) - height[i]) - - return trapped_water diff --git a/archives/python/problems/medium/binary_tree_right_side_view.py b/archives/python/problems/medium/binary_tree_right_side_view.py deleted file mode 100644 index 33d898c..0000000 --- a/archives/python/problems/medium/binary_tree_right_side_view.py +++ /dev/null @@ -1,32 +0,0 @@ -from collections import deque - - -# Definition for a binary tree node. -class TreeNode(object): - def __init__(self, val=0, left=None, right=None): - self.val = val - self.left = left - self.right = right - - -class Solution(object): - def rightSideView(self, root): - """ - :type root: TreeNode - :rtype: List[int] - """ - res = [] - dq = deque([root]) - while dq: - rightMost = None - n = len(dq) - - for i in range(n): - node = dq.popleft() - if node: - rightMost = node - dq.append(node.left) - dq.append(node.right) - if rightMost: - res.append(rightMost.val) - return res diff --git a/archives/python/problems/medium/container_with_most_water.py b/archives/python/problems/medium/container_with_most_water.py deleted file mode 100644 index 43b86c6..0000000 --- a/archives/python/problems/medium/container_with_most_water.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import List - - -class Solution: - def maxArea(self, height: List[int]) -> int: - n = len(height) - left, right = 0, n - 1 - ans = 0 - while left < right: - ans = max(ans, min(height[right], height[left]) * (right - left)) - if height[right] < height[left]: - right -= 1 - else: - left += 1 - return ans diff --git a/archives/python/problems/medium/encode_and_decode_strings.py b/archives/python/problems/medium/encode_and_decode_strings.py deleted file mode 100644 index af9c8dd..0000000 --- a/archives/python/problems/medium/encode_and_decode_strings.py +++ /dev/null @@ -1,37 +0,0 @@ -from typing import List - - -class Codec: - def encode(self, strs: List[str]) -> str: - """Encodes a list of strings to a single string.""" - encoded_list = [] - for s in strs: - encoded_list.append(str(len(s)) + "#") - encoded_list.append(s) - return "".join(encoded_list) - - def decode(self, s: str) -> List[str]: - """Decodes a single string to a list of strings.""" - res = [] - len_to_read = None - prev = 0 - i = 1 - while i < len(s): - if not len_to_read: - if s[i] == "#": - len_to_read = int(s[prev:i]) - if len_to_read == 0: - res.append("") - prev = i + 1 - i += 1 - else: - res.append(s[i : i + len_to_read]) - prev = i + len_to_read - i = i + len_to_read + 1 - len_to_read = None - return res - - -# Your Codec object will be instantiated and called as such: -# codec = Codec() -# codec.decode(codec.encode(strs)) diff --git a/archives/python/problems/medium/evaluate_reverse_polish_notation.py b/archives/python/problems/medium/evaluate_reverse_polish_notation.py deleted file mode 100644 index e6bb9f7..0000000 --- a/archives/python/problems/medium/evaluate_reverse_polish_notation.py +++ /dev/null @@ -1,22 +0,0 @@ -from typing import List - - -class Solution: - def evalRPN(self, tokens: List[str]) -> int: - stack = [] - for token in tokens: - if token in "+-*/": - b = stack.pop() - a = stack.pop() - print(token, a, b) - if token == "+": - stack.append(a + b) - elif token == "-": - stack.append(a - b) - elif token == "*": - stack.append(a * b) - else: - stack.append(int(a / b)) - else: - stack.append(int(token)) - return stack[-1] diff --git a/archives/python/problems/medium/generate_parentheses.py b/archives/python/problems/medium/generate_parentheses.py deleted file mode 100644 index 39b56f9..0000000 --- a/archives/python/problems/medium/generate_parentheses.py +++ /dev/null @@ -1,17 +0,0 @@ -from typing import List - - -class Solution: - def generateParenthesis(self, n: int) -> List[str]: - def backtrack(s="", left=0, right=0): - if len(s) == 2 * n: - result.append(s) - return - if left < n: - backtrack(s + "(", left + 1, right) - if right < left: - backtrack(s + ")", left, right + 1) - - result = [] - backtrack() - return result diff --git a/archives/python/problems/medium/group_anagrams.py b/archives/python/problems/medium/group_anagrams.py deleted file mode 100644 index ec4f860..0000000 --- a/archives/python/problems/medium/group_anagrams.py +++ /dev/null @@ -1,13 +0,0 @@ -from typing import List -import collections - - -class Solution: - def groupAnagrams(self, strs: List[str]) -> List[List[str]]: - groups = collections.defaultdict(list) - for s in strs: - count = [0] * 26 - for c in s: - count[ord(c) - ord("a")] += 1 - groups[tuple(count)].append(s) - return groups.values() diff --git a/archives/python/problems/medium/longest_consecutive_sequence.py b/archives/python/problems/medium/longest_consecutive_sequence.py deleted file mode 100644 index 96d4a5a..0000000 --- a/archives/python/problems/medium/longest_consecutive_sequence.py +++ /dev/null @@ -1,20 +0,0 @@ -from typing import List - - -class Solution: - def longestConsecutive(self, nums: List[int]) -> int: - longest_streak = 0 - num_set = set(nums) - - for num in num_set: - if num - 1 not in num_set: - current_num = num - current_streak = 1 - - while current_num + 1 in num_set: - current_num += 1 - current_streak += 1 - - longest_streak = max(longest_streak, current_streak) - - return longest_streak diff --git a/archives/python/problems/medium/longest_repeating_character_replacement.py b/archives/python/problems/medium/longest_repeating_character_replacement.py deleted file mode 100644 index 24de9d6..0000000 --- a/archives/python/problems/medium/longest_repeating_character_replacement.py +++ /dev/null @@ -1,15 +0,0 @@ -class Solution: - def characterReplacement(self, s: str, k: int) -> int: - def getIndex(c): - return ord(c) - ord("A") - - count = [0] * 26 - left = 0 - ans = 0 - for right in range(len(s)): - count[getIndex(s[right])] += 1 - while (right - left + 1) - max(count) > k: - count[getIndex(s[left])] -= 1 - left += 1 - ans = max(ans, right - left + 1) - return ans diff --git a/archives/python/problems/medium/longest_substring_without_repeating_characters.py b/archives/python/problems/medium/longest_substring_without_repeating_characters.py deleted file mode 100644 index 4ae8454..0000000 --- a/archives/python/problems/medium/longest_substring_without_repeating_characters.py +++ /dev/null @@ -1,17 +0,0 @@ -import collections - - -class Solution: - def lengthOfLongestSubstring(self, s: str) -> int: - left = 0 - count_map = collections.defaultdict(int) - max_length = 0 - - for r, c in enumerate(s): - count_map[c] += 1 - while count_map[c] > 1: - count_map[s[left]] -= 1 - left += 1 - max_length = max(max_length, r - left + 1) - - return max_length diff --git a/archives/python/problems/medium/min_stack.py b/archives/python/problems/medium/min_stack.py deleted file mode 100644 index 3860e3a..0000000 --- a/archives/python/problems/medium/min_stack.py +++ /dev/null @@ -1,19 +0,0 @@ -class MinStack: - def __init__(self): - self.stack = [] - - def push(self, val: int) -> None: - if self.stack: - _, tmin = self.stack[-1] - self.stack.append((val, min(tmin, val))) - else: - self.stack.append((val, val)) - - def pop(self) -> None: - self.stack.pop()[0] - - def top(self) -> int: - return self.stack[-1][0] - - def getMin(self) -> int: - return self.stack[-1][1] diff --git a/archives/python/problems/medium/minimum_size_subarray_sum.py b/archives/python/problems/medium/minimum_size_subarray_sum.py deleted file mode 100644 index 59c5cf9..0000000 --- a/archives/python/problems/medium/minimum_size_subarray_sum.py +++ /dev/null @@ -1,27 +0,0 @@ -class Solution(object): - def minSubArrayLen(self, target, nums): - """ - :type target: int - :type nums: List[int] - :rtype: int - """ - - if not nums: - return 0 - - n = len(nums) - left, right = 0, 1 - min_len = float("inf") - sub_sum = nums[0] - while right <= n: - if sub_sum < target: - if right < n: - sub_sum += nums[right] - right += 1 - else: - diff = right - left - if diff < min_len: - min_len = min(diff, min_len) - sub_sum -= nums[left] - left += 1 - return 0 if min_len == float("inf") else min_len diff --git a/archives/python/problems/medium/number_of_islands.py b/archives/python/problems/medium/number_of_islands.py deleted file mode 100644 index b6fefb2..0000000 --- a/archives/python/problems/medium/number_of_islands.py +++ /dev/null @@ -1,23 +0,0 @@ -class Solution(object): - def dfs(self, grid, i, j): - if i < 0 or j < 0 or i >= len(grid) or j >= len(grid[0]) or grid[i][j] != "1": - return - grid[i][j] = "#" - self.dfs(grid, i - 1, j) - self.dfs(grid, i + 1, j) - self.dfs(grid, i, j - 1) - self.dfs(grid, i, j + 1) - return - - def numIslands(self, grid): - """ - :type grid: List[List[str]] - :rtype: int - """ - ans = 0 - for i in range(len(grid)): - for j in range(len(grid[0])): - if grid[i][j] == "1": - self.dfs(grid, i, j) - ans += 1 - return ans diff --git a/archives/python/problems/medium/permutation_in_string.py b/archives/python/problems/medium/permutation_in_string.py deleted file mode 100644 index 6916a33..0000000 --- a/archives/python/problems/medium/permutation_in_string.py +++ /dev/null @@ -1,23 +0,0 @@ -import collections - - -class Solution: - def checkInclusion(self, s1: str, s2: str) -> bool: - if len(s2) < len(s1): - return False - - freq_s1 = collections.Counter(s1) - freq_s2 = collections.Counter(s2[: len(s1)]) - - if freq_s1 == freq_s2: - return True - - left = 0 - for right in range(len(s1), len(s2)): - freq_s2[s2[left]] -= 1 - freq_s2[s2[right]] += 1 - if freq_s1 == freq_s2: - return True - left += 1 - - return False diff --git a/archives/python/problems/medium/product_of_array_except_self.py b/archives/python/problems/medium/product_of_array_except_self.py deleted file mode 100644 index 31233e9..0000000 --- a/archives/python/problems/medium/product_of_array_except_self.py +++ /dev/null @@ -1,18 +0,0 @@ -from typing import List - - -class Solution: - def productExceptSelf(self, nums: List[int]) -> List[int]: - n = len(nums) - output = [1] * n - prefix = 1 - for i in range(n): - output[i] *= prefix - prefix *= nums[i] - - suffix = 1 - for i in range(n - 1, -1, -1): - output[i] *= suffix - suffix *= nums[i] - - return output diff --git a/archives/python/problems/medium/snakes_and_ladders.py b/archives/python/problems/medium/snakes_and_ladders.py deleted file mode 100644 index 05fa409..0000000 --- a/archives/python/problems/medium/snakes_and_ladders.py +++ /dev/null @@ -1,45 +0,0 @@ -from collections import deque - - -class Solution: - def snakesAndLadders(self, board): - # board size - row_sz = len(board) - col_sz = len(board[0]) - - q = deque() - visited = set() - parent = [-1] * ((row_sz * col_sz) + 1) - distance = [-1] * ((row_sz * col_sz) + 1) - - q.append(1) - visited.add(1) - distance[1] = 0 - - while q: - node = q.popleft() - - for i in range(1, 7): - n = node + i - if n >= len(parent): - continue - - ft = (n - 1) // col_sz - r = row_sz - 1 - ft - c = (n - 1) % col_sz - if ft % 2 != 0: - c = col_sz - 1 - c - - v = board[r][c] - - if v != -1: - n = v - if n in visited or v == node: - continue - - q.append(n) - visited.add(n) - parent[n] = node - distance[n] = distance[node] + 1 - - return distance[-1] diff --git a/archives/python/problems/medium/three_sum.py b/archives/python/problems/medium/three_sum.py deleted file mode 100644 index 258a13b..0000000 --- a/archives/python/problems/medium/three_sum.py +++ /dev/null @@ -1,24 +0,0 @@ -from typing import List - - -class Solution: - def threeSum(self, nums: List[int]) -> List[List[int]]: - nums.sort() - n = len(nums) - res = [] - for i in range(n - 2): - if i > 0 and nums[i] == nums[i - 1]: - continue - left, right = i + 1, n - 1 - while left < right: - three_sum = nums[i] + nums[left] + nums[right] - if three_sum == 0: - res.append([nums[i], nums[left], nums[right]]) - left += 1 - while nums[left] == nums[left - 1] and left < right: - left += 1 - elif three_sum < 0: - left += 1 - else: - right -= 1 - return res diff --git a/archives/python/problems/medium/top_k_frequent_elements.py b/archives/python/problems/medium/top_k_frequent_elements.py deleted file mode 100644 index e71ee88..0000000 --- a/archives/python/problems/medium/top_k_frequent_elements.py +++ /dev/null @@ -1,14 +0,0 @@ -from typing import List -import collections -import heapq - - -class Solution: - def topKFrequent(self, nums: List[int], k: int) -> List[int]: - freq_map = collections.Counter(nums) - min_heap = [] - for val, count in freq_map.items(): - heapq.heappush(min_heap, (count, val)) - while len(min_heap) > k: - heapq.heappop(min_heap) - return list(map(lambda tup: tup[1], min_heap)) diff --git a/archives/python/problems/medium/two_sum_ii_input_array_is_sorted.py b/archives/python/problems/medium/two_sum_ii_input_array_is_sorted.py deleted file mode 100644 index 8a25e5b..0000000 --- a/archives/python/problems/medium/two_sum_ii_input_array_is_sorted.py +++ /dev/null @@ -1,15 +0,0 @@ -from typing import List - - -class Solution: - def twoSum(self, numbers: List[int], target: int) -> List[int]: - left, right = 0, len(numbers) - 1 - while left < right: - tmp_sum = numbers[left] + numbers[right] - if tmp_sum == target: - return [left + 1, right + 1] - elif tmp_sum < target: - left += 1 - else: - right -= 1 - return [-1, -1] diff --git a/archives/python/problems/medium/valid_sudoku.py b/archives/python/problems/medium/valid_sudoku.py deleted file mode 100644 index 9672bd7..0000000 --- a/archives/python/problems/medium/valid_sudoku.py +++ /dev/null @@ -1,36 +0,0 @@ -class Solution(object): - def isValidRow(self, board): - for row in board: - if not self.isValid(row): - return False - return True - - def isValidCol(self, board): - for col in zip(*board): - if not self.isValid(col): - return False - return True - - def isValidSquare(self, board): - for i in (0, 3, 6): - for j in (0, 3, 6): - square = [board[x][y] for x in range(i, i + 3) for y in range(j, j + 3)] - if not self.isValid(square): - return False - return True - - def isValid(self, cells): - res = [i for i in cells if i != "."] - return len(res) == len(set(res)) - - def isValidSudoku(self, board): - """ - :type board: List[List[str]] - :rtype: bool - """ - - return ( - self.isValidRow(board) - and self.isValidCol(board) - and self.isValidSquare(board) - ) diff --git a/archives/python/tests/__init__.py b/archives/python/tests/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/archives/python/tests/test_best_time_to_buy_and_sell_stock.py b/archives/python/tests/test_best_time_to_buy_and_sell_stock.py deleted file mode 100644 index 8fbdffd..0000000 --- a/archives/python/tests/test_best_time_to_buy_and_sell_stock.py +++ /dev/null @@ -1,13 +0,0 @@ -from problems.easy.best_time_to_buy_and_sell_stock import Solution - -solution = Solution() - - -def test_sample_one(): - prices = [7, 1, 5, 3, 6, 4] - assert solution.maxProfit(prices) == 5 - - -def test_sample_two(): - prices = [7, 6, 4, 3, 1] - assert solution.maxProfit(prices) == 0 diff --git a/archives/python/tests/test_binary_tree_right_side_view.py b/archives/python/tests/test_binary_tree_right_side_view.py deleted file mode 100644 index 1bc886d..0000000 --- a/archives/python/tests/test_binary_tree_right_side_view.py +++ /dev/null @@ -1,37 +0,0 @@ -from problems.medium.binary_tree_right_side_view import Solution, TreeNode - - -def test_right_side_view(): - solution = Solution - - # Testcase 1 (my own test case) - root = TreeNode(3) - root.left = TreeNode(9) - root.left.left = TreeNode(10) - root.left.left.left = TreeNode(11) - root.left.left.left.right = TreeNode(20) - assert solution.rightSideView(solution, root) == [3, 9, 10, 11, 20] - - # Testcase 2 - root = TreeNode(1) - root.left = TreeNode(2) - root.left.right = TreeNode(5) - root.right = TreeNode(3) - root.right.right = TreeNode(4) - assert solution.rightSideView(solution, root) == [1, 3, 4] - - # Testcase 3 - root = TreeNode(1) - root.left = TreeNode(2) - root.left.right = TreeNode(5) - root.right = TreeNode(3) - assert solution.rightSideView(solution, root) == [1, 3, 5] - - # Testcase 4 - root = TreeNode(1) - root.left = TreeNode(2) - root.left.right = TreeNode(5) - root.right = TreeNode(3) - root.right.right = TreeNode(4) - root.right.right.right = TreeNode(6) - assert solution.rightSideView(solution, root) == [1, 3, 4, 6] diff --git a/archives/python/tests/test_candy.py b/archives/python/tests/test_candy.py deleted file mode 100644 index d182dff..0000000 --- a/archives/python/tests/test_candy.py +++ /dev/null @@ -1,37 +0,0 @@ -from problems.hard.candy import Solution - - -def test_candy(): - solution = Solution() - - # Testcase 1 - ratings = [1, 0, 2] - assert solution.candy(ratings) == 5 - - # Testcase 2 - ratings = [1, 2, 2] - assert solution.candy(ratings) == 4 - - # Testcase 3 - ratings = [1, 2, 3] - assert solution.candy(ratings) == 6 - - # Testcase 4 - ratings = [3, 2, 1] - assert solution.candy(ratings) == 6 - - # Testcase 5 - ratings = [1, 5, 4, 2] - assert solution.candy(ratings) == 7 - - # Testcase 6 - ratings = [1, 5, 6, 4, 2] - assert solution.candy(ratings) == 9 - - # Testcase 7 - ratings = [6, 5, 1, 4, 2] - assert solution.candy(ratings) == 9 - - # Testcase 8 - ratings = [6, 5, 6, 4, 2] - assert solution.candy(ratings) == 9 diff --git a/archives/python/tests/test_container_with_most_water.py b/archives/python/tests/test_container_with_most_water.py deleted file mode 100644 index 6f9e82f..0000000 --- a/archives/python/tests/test_container_with_most_water.py +++ /dev/null @@ -1,13 +0,0 @@ -from problems.medium.container_with_most_water import Solution - -solution = Solution() - - -def test_sample_one(): - height = [1, 8, 6, 2, 5, 4, 8, 3, 7] - assert solution.maxArea(height) == 49 - - -def test_sample_two(): - height = [1, 1] - assert solution.maxArea(height) == 1 diff --git a/archives/python/tests/test_contains_duplicate.py b/archives/python/tests/test_contains_duplicate.py deleted file mode 100644 index 77f6dd5..0000000 --- a/archives/python/tests/test_contains_duplicate.py +++ /dev/null @@ -1,18 +0,0 @@ -from problems.easy.contains_duplicate import Solution - -solution = Solution() - - -def test_one_duplicate(): - sample = [1, 2, 3, 1] - assert solution.containsDuplicate(sample) - - -def test_no_duplicates(): - sample = [1, 2, 3, 4] - assert not solution.containsDuplicate(sample) - - -def test_empty_array(): - sample = [] - assert not solution.containsDuplicate(sample) diff --git a/archives/python/tests/test_encode_and_decode_strings.py b/archives/python/tests/test_encode_and_decode_strings.py deleted file mode 100644 index 9d2e144..0000000 --- a/archives/python/tests/test_encode_and_decode_strings.py +++ /dev/null @@ -1,18 +0,0 @@ -from problems.medium.encode_and_decode_strings import Codec - -codec = Codec() - - -def test_sample_simple(): - dummy_input = ["Hello", "World"] - assert codec.decode(codec.encode(dummy_input)) == dummy_input - - -def test_sample_one_empty_string(): - dummy_input = [""] - assert codec.decode(codec.encode(dummy_input)) == dummy_input - - -def test_sample_two_empty_string(): - dummy_input = ["", ""] - assert codec.decode(codec.encode(dummy_input)) == dummy_input diff --git a/archives/python/tests/test_evaluate_reverse_polish_notation.py b/archives/python/tests/test_evaluate_reverse_polish_notation.py deleted file mode 100644 index c761386..0000000 --- a/archives/python/tests/test_evaluate_reverse_polish_notation.py +++ /dev/null @@ -1,18 +0,0 @@ -from problems.medium.evaluate_reverse_polish_notation import Solution - -solution = Solution() - - -def test_sample_one(): - tokens = ["2", "1", "+", "3", "*"] - assert solution.evalRPN(tokens) == 9 - - -def test_sample_two(): - tokens = ["4", "13", "5", "/", "+"] - assert solution.evalRPN(tokens) == 6 - - -def test_sample_three(): - tokens = ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"] - assert solution.evalRPN(tokens) == 22 diff --git a/archives/python/tests/test_generate_parentheses.py b/archives/python/tests/test_generate_parentheses.py deleted file mode 100644 index a98b742..0000000 --- a/archives/python/tests/test_generate_parentheses.py +++ /dev/null @@ -1,15 +0,0 @@ -from problems.medium.generate_parentheses import Solution - -solution = Solution() - - -def test_sample_one(): - n = 3 - expected = ["((()))", "(()())", "(())()", "()(())", "()()()"] - assert solution.generateParenthesis(n) == expected - - -def test_sample_two(): - n = 1 - expected = ["()"] - assert solution.generateParenthesis(n) == expected diff --git a/archives/python/tests/test_group_anagrams.py b/archives/python/tests/test_group_anagrams.py deleted file mode 100644 index 1630738..0000000 --- a/archives/python/tests/test_group_anagrams.py +++ /dev/null @@ -1,29 +0,0 @@ -from helpers.list import equal_ignore_order -from problems.medium.group_anagrams import Solution - -solution = Solution() - - -def test_sample_one(): - strs = ["eat", "tea", "tan", "ate", "nat", "bat"] - expected = [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]] - - # To avoid unexpected order issue... - unordered_ans = solution.groupAnagrams(strs) - ordered_ans = [] - for li in unordered_ans: - ordered_ans.append(sorted(li)) - - assert equal_ignore_order(ordered_ans, expected) - - -def test_sample_two(): - strs = [""] - expected = [[""]] - assert equal_ignore_order(solution.groupAnagrams(strs), expected) - - -def test_sample_three(): - strs = ["a"] - expected = [["a"]] - assert equal_ignore_order(solution.groupAnagrams(strs), expected) diff --git a/archives/python/tests/test_linked_list_cycle.py b/archives/python/tests/test_linked_list_cycle.py deleted file mode 100644 index 1ff71b9..0000000 --- a/archives/python/tests/test_linked_list_cycle.py +++ /dev/null @@ -1,27 +0,0 @@ -from problems.easy.linked_list_cycle import Solution, ListNode - - -def test_linked_list_cycle(): - solution = Solution - - # Testcase 1 - head = ListNode(3) - head.next = ListNode(2) - head.next.next = ListNode(0) - head.next.next.next = ListNode(-4) - head.next.next.next.next = head.next - assert solution.hasCycle(solution, head) - - # Testcase 2 - head = ListNode(1) - head.next = ListNode(2) - head.next.next = head - assert solution.hasCycle(solution, head) - - # Testcase 3 - head = ListNode(1) - assert not solution.hasCycle(solution, head) - - # Testcase 4 - head = None - assert not solution.hasCycle(solution, head) diff --git a/archives/python/tests/test_longest_consecutive_sequence.py b/archives/python/tests/test_longest_consecutive_sequence.py deleted file mode 100644 index 8a117d0..0000000 --- a/archives/python/tests/test_longest_consecutive_sequence.py +++ /dev/null @@ -1,23 +0,0 @@ -from problems.medium.longest_consecutive_sequence import Solution - -solution = Solution() - - -def test_sample_one(): - nums = [100, 4, 200, 1, 3, 2] - assert solution.longestConsecutive(nums) == 4 - - -def test_sample_two(): - nums = [0, 3, 7, 2, 5, 8, 4, 6, 0, 1] - assert solution.longestConsecutive(nums) == 9 - - -def test_sample_three(): - nums = [0] - assert solution.longestConsecutive(nums) == 1 - - -def test_sample_four(): - nums = [] - assert solution.longestConsecutive(nums) == 0 diff --git a/archives/python/tests/test_longest_repeating_character_replacement.py b/archives/python/tests/test_longest_repeating_character_replacement.py deleted file mode 100644 index 8cc4353..0000000 --- a/archives/python/tests/test_longest_repeating_character_replacement.py +++ /dev/null @@ -1,15 +0,0 @@ -from problems.medium.longest_repeating_character_replacement import Solution - -solution = Solution() - - -def test_sample_one(): - s = "ABAB" - k = 2 - assert solution.characterReplacement(s, k) == 4 - - -def test_sample_two(): - s = "AABABBA" - k = 1 - assert solution.characterReplacement(s, k) == 4 diff --git a/archives/python/tests/test_longest_substring_without_repeating_characters.py b/archives/python/tests/test_longest_substring_without_repeating_characters.py deleted file mode 100644 index ea1cfc5..0000000 --- a/archives/python/tests/test_longest_substring_without_repeating_characters.py +++ /dev/null @@ -1,18 +0,0 @@ -from problems.medium.longest_substring_without_repeating_characters import Solution - -solution = Solution() - - -def test_sample_one(): - s = "abcabcbb" - assert solution.lengthOfLongestSubstring(s) == 3 - - -def test_sample_two(): - s = "bbbbb" - assert solution.lengthOfLongestSubstring(s) == 1 - - -def test_sample_three(): - s = "pwwkew" - assert solution.lengthOfLongestSubstring(s) == 3 diff --git a/archives/python/tests/test_maximum_depth_of_binary_tree.py b/archives/python/tests/test_maximum_depth_of_binary_tree.py deleted file mode 100644 index 5bfc1fb..0000000 --- a/archives/python/tests/test_maximum_depth_of_binary_tree.py +++ /dev/null @@ -1,34 +0,0 @@ -from problems.easy.maximum_depth_of_binary_tree import Solution, TreeNode - - -def test_maximum_depth_of_binary_tree(): - solution = Solution - - # Testcase 1 - root = TreeNode(3) - root.left = TreeNode(9) - root.right = TreeNode(20) - root.right.left = TreeNode(15) - root.right.right = TreeNode(7) - assert solution.maxDepth(solution, root) == 3 - - # Testcase 2 - root = TreeNode(1) - root.right = TreeNode(2) - assert solution.maxDepth(solution, root) == 2 - - # Testcase 3 - root = None - assert solution.maxDepth(solution, root) == 0 - - # Testcase 4 - root = TreeNode(0) - assert solution.maxDepth(solution, root) == 1 - - # Testcase 5 - root = TreeNode(1) - root.left = TreeNode(2) - root.left.left = TreeNode(3) - root.left.left.left = TreeNode(4) - root.left.left.left.left = TreeNode(5) - assert solution.maxDepth(solution, root) == 5 diff --git a/archives/python/tests/test_merge_sorted_array.py b/archives/python/tests/test_merge_sorted_array.py deleted file mode 100644 index f874f91..0000000 --- a/archives/python/tests/test_merge_sorted_array.py +++ /dev/null @@ -1,29 +0,0 @@ -from problems.easy.merge_sorted_array import Solution - - -def test_merge_sorted_array(): - solution = Solution() - - # Test Case 1 - nums1 = [1, 2, 3, 0, 0, 0] - solution.merge(nums1, 3, [2, 5, 6], 3) - - assert nums1 == [1, 2, 2, 3, 5, 6] - - # Test Case 2 - nums1 = [1] - solution.merge(nums1, 1, [], 0) - - assert nums1 == [1] - - # Test Case 3 - nums1 = [0] - solution.merge(nums1, 0, [1], 1) - - assert nums1 == [1] - - # Test Case 4 - nums1 = [1, 0] - solution.merge(nums1, 1, [2], 1) - - assert nums1 == [1, 2] diff --git a/archives/python/tests/test_min_stack.py b/archives/python/tests/test_min_stack.py deleted file mode 100644 index 20b89a6..0000000 --- a/archives/python/tests/test_min_stack.py +++ /dev/null @@ -1,13 +0,0 @@ -from problems.medium.min_stack import MinStack - - -def test_sample(): - min_stack = MinStack() - min_stack.push(-2) - min_stack.push(0) - min_stack.push(-3) - min1 = min_stack.getMin() - min_stack.pop() - top1 = min_stack.top() - min2 = min_stack.getMin() - assert min1 == -3 and top1 == 0 and min2 == -2 diff --git a/archives/python/tests/test_minimum_absolute_difference_in_bst.py b/archives/python/tests/test_minimum_absolute_difference_in_bst.py deleted file mode 100644 index c85e9b1..0000000 --- a/archives/python/tests/test_minimum_absolute_difference_in_bst.py +++ /dev/null @@ -1,46 +0,0 @@ -from problems.easy.minimum_absolute_difference_in_bst import Solution, TreeNode - - -def test_get_minimum_difference(): - # Test case 1 - solution = Solution() - root = TreeNode(4) - root.left = TreeNode(2) - root.right = TreeNode(6) - root.left.left = TreeNode(1) - root.left.right = TreeNode(3) - - assert solution.getMinimumDifference(root) == 1 - - # Test case 2 - solution = Solution() - root = TreeNode(1) - root.right = TreeNode(3) - root.right.left = TreeNode(2) - - assert solution.getMinimumDifference(root) == 1 - - # Test case 3 - solution = Solution() - root = TreeNode(1) - root.right = TreeNode(5) - root.right.left = TreeNode(3) - - assert solution.getMinimumDifference(root) == 2 - - # Test case 4 - solution = Solution() - root = TreeNode(543) - root.left = TreeNode(384) - root.right = TreeNode(652) - root.left.right = TreeNode(445) - root.right.right = TreeNode(699) - - assert solution.getMinimumDifference(root) == 47 - - # Test case 5 - solution = Solution() - root = TreeNode(1) - root.right = TreeNode(2) - - assert solution.getMinimumDifference(root) == 1 diff --git a/archives/python/tests/test_minimum_size_subarray_sum.py b/archives/python/tests/test_minimum_size_subarray_sum.py deleted file mode 100644 index 9969a97..0000000 --- a/archives/python/tests/test_minimum_size_subarray_sum.py +++ /dev/null @@ -1,17 +0,0 @@ -from problems.medium.minimum_size_subarray_sum import Solution - - -def test_minimum_size_subarray_sum(): - solution = Solution() - - # Test Case 1 - assert solution.minSubArrayLen(7, [2, 3, 1, 2, 4, 3]) == 2 - - # Test Case 2 - assert solution.minSubArrayLen(4, [1, 4, 4]) == 1 - - # Test Case 3 - assert solution.minSubArrayLen(11, [1, 1, 1, 1, 1, 1, 1, 1]) == 0 - - # Test Case 4 - assert solution.minSubArrayLen(11, [1, 2, 3, 4, 5]) == 3 diff --git a/archives/python/tests/test_minimum_window_substring.py b/archives/python/tests/test_minimum_window_substring.py deleted file mode 100644 index 9df958a..0000000 --- a/archives/python/tests/test_minimum_window_substring.py +++ /dev/null @@ -1,21 +0,0 @@ -from problems.hard.minimum_window_substring import Solution - -solution = Solution() - - -def test_sample_one(): - s = "ADOBECODEBANC" - t = "ABC" - assert solution.minWindow(s, t) == "BANC" - - -def test_sample_two(): - s = "a" - t = "a" - assert solution.minWindow(s, t) == "a" - - -def test_sample_three(): - s = "a" - t = "aa" - assert solution.minWindow(s, t) == "" diff --git a/archives/python/tests/test_number_of_islands.py b/archives/python/tests/test_number_of_islands.py deleted file mode 100644 index e81f677..0000000 --- a/archives/python/tests/test_number_of_islands.py +++ /dev/null @@ -1,23 +0,0 @@ -from problems.medium.number_of_islands import Solution - - -def test_num_islands(): - solution = Solution() - - # Testcase 1 - grid = [ - ["1", "1", "1", "1", "0"], - ["1", "1", "0", "1", "0"], - ["1", "1", "0", "0", "0"], - ["0", "0", "0", "0", "0"], - ] - assert solution.numIslands(grid) == 1 - - # Testcase 2 - grid = [ - ["1", "1", "0", "0", "0"], - ["1", "1", "0", "0", "0"], - ["0", "0", "1", "0", "0"], - ["0", "0", "0", "1", "1"], - ] - assert solution.numIslands(grid) == 3 diff --git a/archives/python/tests/test_permutation_in_string.py b/archives/python/tests/test_permutation_in_string.py deleted file mode 100644 index 04fd94f..0000000 --- a/archives/python/tests/test_permutation_in_string.py +++ /dev/null @@ -1,21 +0,0 @@ -from problems.medium.permutation_in_string import Solution - -solution = Solution() - - -def test_sample_one(): - s1 = "ab" - s2 = "eidbaooo" - assert solution.checkInclusion(s1, s2) - - -def test_sample_two(): - s1 = "ab" - s2 = "eidboaoo" - assert not solution.checkInclusion(s1, s2) - - -def test_sample_three(): - s1 = "a" - s2 = "ab" - assert solution.checkInclusion(s1, s2) diff --git a/archives/python/tests/test_product_of_array_except_self.py b/archives/python/tests/test_product_of_array_except_self.py deleted file mode 100644 index ba13275..0000000 --- a/archives/python/tests/test_product_of_array_except_self.py +++ /dev/null @@ -1,13 +0,0 @@ -from problems.medium.product_of_array_except_self import Solution - -solution = Solution() - - -def test_sample_one(): - nums = [1, 2, 3, 4] - assert solution.productExceptSelf(nums) == [24, 12, 8, 6] - - -def test_sample_two(): - nums = [-1, 1, 0, -3, 3] - assert solution.productExceptSelf(nums) == [0, 0, 9, 0, 0] diff --git a/archives/python/tests/test_ransom_note.py b/archives/python/tests/test_ransom_note.py deleted file mode 100644 index 220f691..0000000 --- a/archives/python/tests/test_ransom_note.py +++ /dev/null @@ -1,30 +0,0 @@ -from problems.easy.ransom_note import Solution - - -def test_ransom_note(): - solution = Solution - - # Test Case 1 - ransomNote = "a" - magazine = "b" - assert solution.canConstruct(solution, ransomNote, magazine) is False - - # Test Case 2 - ransomNote = "aa" - magazine = "ab" - assert solution.canConstruct(solution, ransomNote, magazine) is False - - # Test Case 3 - ransomNote = "aa" - magazine = "aab" - assert solution.canConstruct(solution, ransomNote, magazine) is True - - # Test Case 4 - ransomNote = "fffbfg" - magazine = "effjfggbffjdgbjjhhdegh" - assert solution.canConstruct(solution, ransomNote, magazine) is True - - # Test Case 5 - ransomNote = "" - magazine = "" - assert solution.canConstruct(solution, ransomNote, magazine) is True diff --git a/archives/python/tests/test_sliding_window_maximum.py b/archives/python/tests/test_sliding_window_maximum.py deleted file mode 100644 index 836eed9..0000000 --- a/archives/python/tests/test_sliding_window_maximum.py +++ /dev/null @@ -1,15 +0,0 @@ -from problems.hard.sliding_window_maximum import Solution - -solution = Solution() - - -def test_sample_one(): - nums = [1, 3, -1, -3, 5, 3, 6, 7] - k = 3 - assert solution.maxSlidingWindow(nums, k) == [3, 3, 5, 5, 6, 7] - - -def test_sample_two(): - nums = [1] - k = 1 - assert solution.maxSlidingWindow(nums, k) == [1] diff --git a/archives/python/tests/test_snakes_and_ladders.py b/archives/python/tests/test_snakes_and_ladders.py deleted file mode 100644 index 951aeda..0000000 --- a/archives/python/tests/test_snakes_and_ladders.py +++ /dev/null @@ -1,20 +0,0 @@ -from problems.medium.snakes_and_ladders import Solution - - -def test_snakes_and_ladders(): - solution = Solution - - # Test Case 1 - board = [ - [-1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1], - [-1, -1, -1, -1, -1, -1], - [-1, 35, -1, -1, 13, -1], - [-1, -1, -1, -1, -1, -1], - [-1, 15, -1, -1, -1, -1], - ] - assert solution.snakesAndLadders(solution, board) == 4 - - # Test Case 2 - board = [[-1, -1], [-1, 3]] - assert solution.snakesAndLadders(solution, board) == 1 diff --git a/archives/python/tests/test_summary_ranges.py b/archives/python/tests/test_summary_ranges.py deleted file mode 100644 index 3a78b18..0000000 --- a/archives/python/tests/test_summary_ranges.py +++ /dev/null @@ -1,25 +0,0 @@ -from problems.easy.summary_ranges import Solution - - -def test_summary_ranges(): - solution = Solution - - # Test Case 1 - nums = [0, 1, 2, 4, 5, 7] - assert solution.summaryRanges(solution, nums) == ["0->2", "4->5", "7"] - - # Test Case 2 - nums = [0, 2, 3, 4, 6, 8, 9] - assert solution.summaryRanges(solution, nums) == ["0", "2->4", "6", "8->9"] - - # Test Case 3 - nums = [] - assert solution.summaryRanges(solution, nums) == [] - - # Test Case 4 - nums = [-1] - assert solution.summaryRanges(solution, nums) == ["-1"] - - # Test Case 5 - nums = [-100, 0, 1, 2, 100] - assert solution.summaryRanges(solution, nums) == ["-100", "0->2", "100"] diff --git a/archives/python/tests/test_three_sum.py b/archives/python/tests/test_three_sum.py deleted file mode 100644 index f6cd535..0000000 --- a/archives/python/tests/test_three_sum.py +++ /dev/null @@ -1,28 +0,0 @@ -from problems.medium.three_sum import Solution - - -solution = Solution() - - -def test_sample_one(): - nums = [-1, 0, 1, 2, -1, -4] - expected = [[-1, -1, 2], [-1, 0, 1]] - assert solution.threeSum(nums) == expected - - -def test_sample_two(): - nums = [0, 1, 1] - expected = [] - assert solution.threeSum(nums) == expected - - -def test_sample_three(): - nums = [0, 0, 0] - expected = [[0, 0, 0]] - assert solution.threeSum(nums) == expected - - -def test_sample_four(): - nums = [0, 0, 0, 0] - expected = [[0, 0, 0]] - assert solution.threeSum(nums) == expected diff --git a/archives/python/tests/test_top_k_frequent_elements.py b/archives/python/tests/test_top_k_frequent_elements.py deleted file mode 100644 index f0d098f..0000000 --- a/archives/python/tests/test_top_k_frequent_elements.py +++ /dev/null @@ -1,21 +0,0 @@ -from problems.medium.top_k_frequent_elements import Solution - -solution = Solution() - - -def test_sample_big(): - nums = [1, 1, 1, 2, 2, 3] - k = 2 - assert sorted(solution.topKFrequent(nums, k)) == [1, 2] - - -def test_sample_small(): - nums = [1] - k = 1 - assert solution.topKFrequent(nums, k) == [1] - - -def test_sample_unsorted_big(): - nums = [1, 0, -12, 13, -12, 0, 3, 1] - k = 3 - assert sorted(solution.topKFrequent(nums, k)) == [-12, 0, 1] diff --git a/archives/python/tests/test_trapping_rain_water.py b/archives/python/tests/test_trapping_rain_water.py deleted file mode 100644 index 40c4a29..0000000 --- a/archives/python/tests/test_trapping_rain_water.py +++ /dev/null @@ -1,24 +0,0 @@ -from problems.hard.trapping_rain_water_o_n import Solution as trap_o_n -from problems.hard.trapping_rain_water_o_1 import Solution as trap_o_1 - - -def test_trapping_rain_water(): - # Test Case 1 - height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1] - assert trap_o_n().trap(height) == 6 - assert trap_o_1().trap(height) == 6 - - # Test Case 2 - height = [4, 2, 0, 3, 2, 5] - assert trap_o_n().trap(height) == 9 - assert trap_o_1().trap(height) == 9 - - # Test Case 3 - height = [4, 2, 3] - assert trap_o_n().trap(height) == 1 - assert trap_o_1().trap(height) == 1 - - # Test Case 4 - height = [4, 2, 3, 1] - assert trap_o_n().trap(height) == 1 - assert trap_o_1().trap(height) == 1 diff --git a/archives/python/tests/test_two_sum_ii_input_array_is_sorted.py b/archives/python/tests/test_two_sum_ii_input_array_is_sorted.py deleted file mode 100644 index 32dc3c2..0000000 --- a/archives/python/tests/test_two_sum_ii_input_array_is_sorted.py +++ /dev/null @@ -1,21 +0,0 @@ -from problems.medium.two_sum_ii_input_array_is_sorted import Solution - -solution = Solution() - - -def test_sample_one(): - numbers = [2, 7, 11, 15] - target = 9 - assert solution.twoSum(numbers, target) == [1, 2] - - -def test_sample_two(): - numbers = [2, 3, 4] - target = 6 - assert solution.twoSum(numbers, target) == [1, 3] - - -def test_sample_three(): - numbers = [-1, 0] - target = -1 - assert solution.twoSum(numbers, target) == [1, 2] diff --git a/archives/python/tests/test_valid_anagram.py b/archives/python/tests/test_valid_anagram.py deleted file mode 100644 index 2dbcce5..0000000 --- a/archives/python/tests/test_valid_anagram.py +++ /dev/null @@ -1,27 +0,0 @@ -from problems.easy.valid_anagram import Solution - -solution = Solution() - - -def test_anagram(): - s = "anagram" - t = "nagaram" - assert solution.isAnagram(s, t) - - -def test_not_anagram(): - s = "rat" - t = "car" - assert not solution.isAnagram(s, t) - - -def test_empty_strings(): - s = "" - t = "" - assert solution.isAnagram(s, t) - - -def test_one_empty_string(): - s = "" - t = "a" - assert not solution.isAnagram(s, t) diff --git a/archives/python/tests/test_valid_parentheses.py b/archives/python/tests/test_valid_parentheses.py deleted file mode 100644 index 0e9dc10..0000000 --- a/archives/python/tests/test_valid_parentheses.py +++ /dev/null @@ -1,38 +0,0 @@ -from problems.easy.valid_parentheses import Solution - -solution = Solution() - - -def test_sample_one(): - s = "()" - assert solution.isValid(s) - - -def test_sample_two(): - s = "()[]{}" - assert solution.isValid(s) - - -def test_sample_three(): - s = "(]" - assert not solution.isValid(s) - - -def test_sample_four(): - s = "([)]" - assert not solution.isValid(s) - - -def test_sample_five(): - s = "{[]}" - assert solution.isValid(s) - - -def test_sample_six(): - s = "" - assert solution.isValid(s) - - -def test_sample_seven(): - s = "]([])" - assert not solution.isValid(s) diff --git a/archives/python/tests/test_valid_sudoku.py b/archives/python/tests/test_valid_sudoku.py deleted file mode 100644 index f4f74e8..0000000 --- a/archives/python/tests/test_valid_sudoku.py +++ /dev/null @@ -1,33 +0,0 @@ -from problems.medium.valid_sudoku import Solution - -solution = Solution() - - -def test_valid_sudoku(): - board = [ - ["5", "3", ".", ".", "7", ".", ".", ".", "."], - ["6", ".", ".", "1", "9", "5", ".", ".", "."], - [".", "9", "8", ".", ".", ".", ".", "6", "."], - ["8", ".", ".", ".", "6", ".", ".", ".", "3"], - ["4", ".", ".", "8", ".", "3", ".", ".", "1"], - ["7", ".", ".", ".", "2", ".", ".", ".", "6"], - [".", "6", ".", ".", ".", ".", "2", "8", "."], - [".", ".", ".", "4", "1", "9", ".", ".", "5"], - [".", ".", ".", ".", "8", ".", ".", "7", "9"], - ] - assert solution.isValidSudoku(board) - - -def test_not_valid_sudoku(): - board = [ - ["8", "3", ".", ".", "7", ".", ".", ".", "."], - ["6", ".", ".", "1", "9", "5", ".", ".", "."], - [".", "9", "8", ".", ".", ".", ".", "6", "."], - ["8", ".", ".", ".", "6", ".", ".", ".", "3"], - ["4", ".", ".", "8", ".", "3", ".", ".", "1"], - ["7", ".", ".", ".", "2", ".", ".", ".", "6"], - [".", "6", ".", ".", ".", ".", "2", "8", "."], - [".", ".", ".", "4", "1", "9", ".", ".", "5"], - [".", ".", ".", ".", "8", ".", ".", "7", "9"], - ] - assert not solution.isValidSudoku(board)