Skip to content

Commit dfdf5fd

Browse files
committed
Linting, formatting, move tests to tests/unit
1 parent b01f0f2 commit dfdf5fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+569
-340
lines changed

problems/___init__.py

Whitespace-only changes.

solutions/0001.py renamed to problems/problem_0001.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88

9-
class Solution(object):
9+
class Solution:
1010
def twoSum(self, nums, target):
1111
"""
1212
:type nums: List[int]
@@ -24,6 +24,3 @@ def twoSum(self, nums, target):
2424
for i, num in enumerate(nums):
2525
if target - num in val2id and i != val2id[target - num]:
2626
return i, val2id[target - num]
27-
28-
29-
assert Solution().twoSum([2, 7, 11, 15], 9) == (0, 1)

problems/problem_0002.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""You are given two non-empty linked lists representing
2+
two non-negative integers.
3+
4+
The digits are stored in reverse order,
5+
and each of their nodes contains a single digit.
6+
Add the two numbers and return the sum as a linked list.
7+
8+
You may assume the two numbers do not contain any leading zero,
9+
except the number 0 itself.
10+
11+
"""
12+
13+
14+
# Definition of singly-linked list.
15+
class ListNode:
16+
def __init__(self, val=0, next_val=None):
17+
self.val = val
18+
self.next_val = next_val
19+
20+
21+
class Solution:
22+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
23+
24+
remainder = 0
25+
26+
dummy = ListNode()
27+
last = dummy
28+
29+
while True:
30+
31+
s = l1.val + l2.val + remainder
32+
s, remainder = s % 10, s // 10
33+
34+
new_last = ListNode(val=s)
35+
36+
last.next_val = new_last
37+
last = new_last
38+
39+
if l1.next_val is None and l2.next_val is None and remainder == 0:
40+
break
41+
42+
if l1.next_val is not None:
43+
l1 = l1.next_val
44+
else:
45+
l1 = ListNode()
46+
47+
if l2.next_val is not None:
48+
l2 = l2.next_val
49+
else:
50+
l2 = ListNode()
51+
52+
return dummy.next_val
53+
54+
55+
def listnode2array(l):
56+
result_array = []
57+
while True:
58+
result_array.append(l.val)
59+
if l.next_val is None:
60+
break
61+
l = l.next_val
62+
return result_array

solutions/0007.py renamed to problems/problem_0007.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@ def reverse(self, x: int) -> int:
1616

1717
x = int("".join(x))
1818
x *= sign
19-
x *= -(2 ** 31) <= x <= 2 ** 31 - 1
19+
x *= -(2**31) <= x <= 2**31 - 1
2020

2121
return x
22-
23-
24-
sol = Solution()
25-
26-
assert sol.reverse(-123) == -321
27-
assert sol.reverse(120) == 21
28-
assert sol.reverse(0) == 0
29-
assert sol.reverse(1534236469) == 0
30-
assert sol.reverse(1) == 1

solutions/0009.py renamed to problems/problem_0009.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
class Solution:
77
def get_digit(self, num, i):
8-
return (num % 10 ** (i + 1) - num % 10 ** i) / 10 ** i
8+
return (num % 10 ** (i + 1) - num % 10**i) / 10**i
99

1010
def isPalindrome(self, x):
1111

1212
if x < 0:
1313
return False
1414

1515
for i in range(20):
16-
if x < 10 ** i:
16+
if x < 10**i:
1717
num_digits = i
1818
break
1919

@@ -27,9 +27,3 @@ def isPalindrome(self, x):
2727
return False
2828

2929
return True
30-
31-
32-
assert not Solution().isPalindrome(9223372036854775807)
33-
assert not Solution().isPalindrome(12822)
34-
assert not Solution().isPalindrome(-2147447412)
35-
assert Solution().isPalindrome(121)

solutions/0011.py renamed to problems/problem_0011.py

-9
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,3 @@ def maxArea(self, height: List[int]) -> int:
2929
idx1 -= 1
3030

3131
return max_area
32-
33-
34-
sol = Solution()
35-
36-
assert sol.maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7]) == 49
37-
assert sol.maxArea([3, 9, 3, 4, 7, 2, 12, 6]) == 45
38-
assert sol.maxArea([1, 2]) == 1
39-
assert sol.maxArea([1, 3, 2, 5, 25, 24, 5]) == 24
40-
assert sol.maxArea([1, 1]) == 1

solutions/0013.py renamed to problems/problem_0013.py

-7
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,3 @@ def romanToInt(self, s: str) -> int:
3737
res_sum += roman_mapper[s[i]]
3838

3939
return res_sum
40-
41-
42-
sol = Solution()
43-
44-
assert sol.romanToInt("IX") == 9
45-
assert sol.romanToInt("LVIII") == 58
46-
assert sol.romanToInt("MCMXCIV") == 1994

solutions/0014.py renamed to problems/problem_0014.py

-10
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,3 @@ def longestCommonPrefix(self, strs) -> str:
1717
return "".join(prefix)
1818
prefix.append(chars[0])
1919
return "".join(prefix)
20-
21-
22-
sol = Solution()
23-
24-
assert sol.longestCommonPrefix(["flower", "flow", "flight"]) == "fl"
25-
assert sol.longestCommonPrefix(["dog", "racecar", "car"]) == ""
26-
assert sol.longestCommonPrefix(["aca", "cba"]) == ""
27-
assert sol.longestCommonPrefix(["ab", "ab"]) == "ab"
28-
assert sol.longestCommonPrefix([]) == ""
29-
assert sol.longestCommonPrefix(["", "b"]) == ""

solutions/0015.py renamed to problems/problem_0015.py

-15
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,3 @@ def threeSum(self, nums: List[int]) -> List[List[int]]:
3434
idx1 -= 1
3535

3636
return list(set(res))
37-
38-
39-
sol = Solution()
40-
assert sol.threeSum([-1, 0, 1, 2, -1, -4]) == [(-1, 0, 1), (-1, -1, 2)]
41-
assert sol.threeSum([-1, 0, 1, 2, -1, -4, -2, -3, 3, 0, 4]) == [
42-
(-3, -1, 4),
43-
(-1, 0, 1),
44-
(-4, 1, 3),
45-
(-2, 0, 2),
46-
(-1, -1, 2),
47-
(-3, 0, 3),
48-
(-4, 0, 4),
49-
(-2, -1, 3),
50-
(-3, 1, 2),
51-
]

solutions/0020.py renamed to problems/problem_0020.py

-9
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,3 @@ def isValid(self, s: str) -> bool:
2525
return False
2626

2727
return not stack
28-
29-
30-
assert Solution().isValid("()")
31-
assert Solution().isValid("()[]{}")
32-
assert not Solution().isValid("(]")
33-
assert not Solution().isValid("([)]")
34-
assert Solution().isValid("{[]}")
35-
assert Solution().isValid("(((((())))))")
36-
assert Solution().isValid("{}{}()[]")

solutions/0026.py renamed to problems/problem_0026.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99

1010
class Solution:
11+
def __init__(self):
12+
self.nums = None
13+
1114
def removeDuplicates(self, nums, debug_mode=False) -> int:
1215

1316
len_nums = len(nums)
@@ -28,14 +31,3 @@ def removeDuplicates(self, nums, debug_mode=False) -> int:
2831
self.nums = nums[: last_correct_idx + 1]
2932

3033
return last_correct_idx + 1
31-
32-
33-
sol = Solution()
34-
35-
assert sol.removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4], debug_mode=True) == 5
36-
assert sol.nums == [0, 1, 2, 3, 4]
37-
38-
assert sol.removeDuplicates([1, 1, 2], debug_mode=True) == 2
39-
assert sol.nums == [1, 2]
40-
41-
assert sol.removeDuplicates([]) == 0

solutions/0027.py renamed to problems/problem_0027.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,22 @@
99

1010

1111
class Solution:
12-
def __init__(self, debug_mode=False):
13-
self.debug_mode = debug_mode
12+
def __init__(self):
13+
self.nums = None
1414

1515
def removeElement(self, nums, val, debug_mode=False) -> int:
1616

1717
last_correct_idx = 0
1818

19-
for i in range(len(nums)):
19+
for nums_val in nums:
2020

21-
if nums[i] == val:
21+
if nums_val == val:
2222
continue
2323

24-
nums[last_correct_idx] = nums[i]
24+
nums[last_correct_idx] = nums_val
2525
last_correct_idx += 1
2626

27-
if self.debug_mode:
27+
if debug_mode: # Sanity check
2828
self.nums = nums[:last_correct_idx]
2929

3030
return last_correct_idx
31-
32-
33-
sol = Solution(debug_mode=True)
34-
35-
assert sol.removeElement([3, 2, 2, 3], 3) == 2
36-
assert sol.nums == [2, 2]
37-
38-
assert sol.removeElement([0, 1, 2, 2, 3, 0, 4, 2], 2) == 5
39-
assert sol.nums == [0, 1, 3, 0, 4]

solutions/0028.py renamed to problems/problem_0028.py

-15
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,3 @@ def strStr(self, haystack: str, needle: str) -> int:
3838
return i
3939

4040
return -1
41-
42-
43-
if __name__ == "__main__":
44-
45-
sol = Solution()
46-
47-
assert sol.strStr("aaa", "aaaa") == -1
48-
assert sol.strStr("mississippi", "issip") == 4
49-
assert sol.strStr("hello", "ll") == 2
50-
assert sol.strStr("aaa", "aaa") == 0
51-
assert sol.strStr("a", "a") == 0
52-
assert sol.strStr("aaa", "aaa") == 0
53-
assert sol.strStr("ba", "a") == 1
54-
assert sol.strStr("aaaaa", "bba") == -1
55-
assert sol.strStr("", "") == 0

solutions/0075.py renamed to problems/problem_0075.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""
1212

1313

14-
class Solution(object):
14+
class Solution:
1515
def merge(self, left_part, right_part, a):
1616
i = 0
1717
j = 0
@@ -50,8 +50,3 @@ def merge_sort(self, a):
5050

5151
def sortColors(self, nums):
5252
self.merge_sort(nums)
53-
54-
55-
colors = [2, 0, 2, 1, 1, 0]
56-
Solution().sortColors(colors)
57-
assert colors == [0, 0, 1, 1, 2, 2]

solutions/0125.py renamed to problems/problem_0125.py

-6
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,3 @@ def isPalindrome(self, s):
3232
j -= 1
3333

3434
return True
35-
36-
37-
assert Solution().isPalindrome("a,ba")
38-
assert Solution().isPalindrome("A man, a plan, a canal: Panama")
39-
assert not (Solution().isPalindrome("race a car"))
40-
assert Solution().isPalindrome("")

solutions/0189.py renamed to problems/problem_0189.py

-9
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,3 @@ def rotate(self, nums, k):
3939
tmp = nums[i]
4040
nums[i] = nums[i + 1]
4141
nums[i + 1] = tmp
42-
43-
44-
nums = []
45-
Solution().rotate(nums, 1)
46-
assert nums == []
47-
48-
nums = [1, 2, 3, 4, 5, 6, 7]
49-
Solution().rotate(nums, 3)
50-
assert nums == [5, 6, 7, 1, 2, 3, 4]

solutions/0198.py renamed to problems/problem_0198.py

-8
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,3 @@ def rob(self, nums: List[int]) -> int:
2828
rob1 = current
2929

3030
return max(rob0, rob1)
31-
32-
33-
sol = Solution()
34-
35-
assert sol.rob([2, 1, 1, 2]) == 4
36-
assert sol.rob([]) == 0
37-
assert sol.rob([2, 7, 9, 3, 1]) == 12
38-
assert sol.rob([1, 2, 3, 1]) == 4

solutions/0204.py renamed to problems/problem_0204.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ def countPrimes(self, n):
2020
sieve = [True] * n
2121
sieve[0], sieve[1] = False, False
2222

23-
for i in range(2, int(n ** 0.5) + 1):
23+
for i in range(2, int(n**0.5) + 1):
2424

2525
if not sieve[i]:
2626
continue
2727

2828
sieve[i * i : n : i] = [False] * len(sieve[i * i : n : i])
2929

3030
return sum(sieve)
31-
32-
33-
assert Solution().countPrimes(999983) == 78497

solutions/0344.py renamed to problems/problem_0344.py

-13
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,3 @@ def reverseString(self, s: List[str]) -> None:
1919
s[i], s[j] = s[j], s[i]
2020
i += 1
2121
j -= 1
22-
23-
24-
if __name__ == "__main__":
25-
26-
sol = Solution()
27-
28-
s = ["h", "e", "l", "l", "o"]
29-
sol.reverseString(s)
30-
assert s == ["o", "l", "l", "e", "h"]
31-
32-
s = ["H", "a", "n", "n", "a", "h"]
33-
sol.reverseString(s)
34-
assert s == ["h", "a", "n", "n", "a", "H"]

solutions/0442.py renamed to problems/problem_0442.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""
1616

1717

18-
class Solution(object):
18+
class Solution:
1919
def findDuplicates(self, nums):
2020
res = []
2121
for num in nums:
@@ -24,6 +24,3 @@ def findDuplicates(self, nums):
2424
else:
2525
nums[abs(num) - 1] *= -1
2626
return res
27-
28-
29-
assert Solution().findDuplicates([4, 3, 2, 7, 8, 2, 3, 1]) == [2, 3]

solutions/0454.py renamed to problems/problem_0454.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""
2424

2525

26-
class Solution(object):
26+
class Solution:
2727
def fourSumCount(self, A, B, C, D):
2828
res = 0
2929
cnt = {}
@@ -42,6 +42,3 @@ def fourSumCount(self, A, B, C, D):
4242
res += cnt[current_dif]
4343

4444
return res
45-
46-
47-
assert Solution().fourSumCount([1, 2], [-2, -1], [-1, 2], [0, 2]) == 2

0 commit comments

Comments
 (0)