Skip to content

Commit 9249b4a

Browse files
committed
add tests and new problem
1 parent df23d34 commit 9249b4a

File tree

7 files changed

+131
-15
lines changed

7 files changed

+131
-15
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.0.1"
44
authors = [{ name = "tarolling", email = "benitojoens@gmail.com" }]
55
description = "Implementations of LeetCode problem solutions in Python"
66
readme = "README.md"
7-
requires-python = ">=3.8"
7+
requires-python = ">=3.12"
88
classifiers = [
99
"Programming Language :: Python :: 3",
1010
"License :: OSI Approved :: MIT License",

src/leetcodepy/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ListNode:
2+
def __init__(self, val=0, next=None):
3+
self.val = val
4+
self.next = next

src/leetcodepy/easy.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ def two_sum(nums: list[int], target: int) -> list[int]:
2727
2828
Constraints:
2929
30-
2 <= nums.length <= 104
31-
-109 <= nums[i] <= 109
32-
-109 <= target <= 109
30+
2 <= nums.length <= 10^4
31+
-10^9 <= nums[i] <= 10^9
32+
-10^9 <= target <= 10^9
3333
Only one valid answer exists.
3434
3535
"""
3636
# Constraints
37-
assert 2 <= len(nums) <= 104
38-
assert all(map(lambda x: -109 <= x <= 109, nums))
39-
assert -109 <= target <= 109
37+
assert 2 <= len(nums) <= 10**4
38+
assert all(map(lambda x: -(10**9) <= x <= 10**9, nums))
39+
assert -(10**9) <= target <= 10**9
4040

4141
# Implementation
4242
hashmap = {}

src/leetcodepy/medium.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
class ListNode:
2-
def __init__(self, x):
3-
self.val = x
4-
self.next = None
1+
import re
2+
3+
from . import ListNode
54

65

76
def add_two_numbers(l1: ListNode | None, l2: ListNode | None) -> ListNode | None:
@@ -49,3 +48,53 @@ def add_two_numbers(l1: ListNode | None, l2: ListNode | None) -> ListNode | None
4948
l1 = l1.next if l1 else None
5049
l2 = l2.next if l2 else None
5150
return dummy_head.next
51+
52+
53+
def longest_substring_without_repeating_characters(s: str) -> int:
54+
"""
55+
Given a string s, find the length of the longest substring
56+
without repeating characters.
57+
58+
59+
Example 1:
60+
61+
Input: s = "abcabcbb"
62+
Output: 3
63+
Explanation: The answer is "abc", with the length of 3.
64+
65+
Example 2:
66+
67+
Input: s = "bbbbb"
68+
Output: 1
69+
Explanation: The answer is "b", with the length of 1.
70+
71+
Example 3:
72+
73+
Input: s = "pwwkew"
74+
Output: 3
75+
Explanation: The answer is "wke", with the length of 3.
76+
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.
77+
78+
79+
80+
Constraints:
81+
82+
0 <= s.length <= 5 * 10^4
83+
s consists of English letters, digits, symbols and spaces.
84+
"""
85+
pattern = r'^[A-Za-z0-9\s!@#$%^&*()_+\-=\[\]{};:\'",.<>/?\\|`~]+$'
86+
assert re.match(pattern, s)
87+
assert 0 <= len(s) <= 5 * 10**4
88+
89+
max_length = left = 0
90+
count = {}
91+
92+
for right, c in enumerate(s):
93+
count[c] = 1 + count.get(c, 0)
94+
while count[c] > 1:
95+
count[s[left]] -= 1
96+
left += 1
97+
98+
max_length = max(max_length, right - left + 1)
99+
100+
return max_length

tests/__init__.py

Whitespace-only changes.

tests/test_easy.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1-
from leetcodepy.easy import two_sum
1+
from src.leetcodepy.easy import *
2+
import pytest
23

34

45
def test_two_sum():
5-
assert two_sum([2, 7, 11, 15], 9) == [0, 1]
6-
assert two_sum([3, 2, 4], 6) == [1, 2]
7-
assert two_sum([3, 3], 6) == [0, 1]
6+
# Invalid inputs
7+
with pytest.raises(AssertionError):
8+
two_sum([1], 1)
9+
with pytest.raises(AssertionError):
10+
two_sum([1 for _ in range(10**5 + 1)], 1)
11+
with pytest.raises(AssertionError):
12+
two_sum([-(10**9) - 1, 2, 3, 4], 1)
13+
with pytest.raises(AssertionError):
14+
two_sum([2, 3, 4, 10**9 + 1], 2)
15+
with pytest.raises(AssertionError):
16+
two_sum([1, 2, 4, 5, 6], -(10**9) - 1)
17+
with pytest.raises(AssertionError):
18+
two_sum([1, 2, 4, 5, 6], 10**9 + 1)
19+
with pytest.raises(AssertionError):
20+
two_sum([1, 2, 13289, 10**9 + 9, 12312], 10**9 + 2)
21+
22+
# Valid inputs
23+
assert set(two_sum([2, 7, 11, 15], 9)) == set([0, 1])
24+
assert set(two_sum([3, 2, 4], 6)) == set([1, 2])
25+
assert set(two_sum([3, 3], 6)) == set([0, 1])

tests/test_medium.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from src.leetcodepy.medium import *
2+
from src.leetcodepy import ListNode
3+
4+
5+
def test_add_two_numbers():
6+
def list_node_to_list(l: ListNode) -> list:
7+
result = []
8+
current_node = l
9+
while current_node.next is not None:
10+
result.append(current_node.val)
11+
current_node = current_node.next
12+
result.append(current_node.val)
13+
return result
14+
15+
assert list_node_to_list(
16+
add_two_numbers(
17+
ListNode(2, ListNode(4, ListNode(3))), ListNode(5, ListNode(6, ListNode(4)))
18+
)
19+
) == [7, 0, 8]
20+
assert list_node_to_list(add_two_numbers(ListNode(0), ListNode(0))) == [0]
21+
assert list_node_to_list(
22+
add_two_numbers(
23+
ListNode(
24+
9,
25+
ListNode(
26+
9,
27+
ListNode(
28+
9,
29+
ListNode(
30+
9,
31+
ListNode(9, ListNode(9, ListNode(9))),
32+
),
33+
),
34+
),
35+
),
36+
ListNode(9, ListNode(9, ListNode(9, ListNode(9)))),
37+
)
38+
) == [8, 9, 9, 9, 0, 0, 0, 1]
39+
40+
41+
def test_longest_substring_without_repeating_characters():
42+
# Valid inputs
43+
assert longest_substring_without_repeating_characters("abcabcbb") == 3
44+
assert longest_substring_without_repeating_characters("bbbbb") == 1
45+
assert longest_substring_without_repeating_characters("pwwkew") == 3

0 commit comments

Comments
 (0)