Skip to content

Commit 4d6b1f4

Browse files
ThanhNITjavadev
authored andcommitted
Added tests 141-189
1 parent e7f8e94 commit 4d6b1f4

File tree

18 files changed

+300
-0
lines changed

18 files changed

+300
-0
lines changed

src/main/python/g0101_0200/s0141_linked_list_cycle/Solution0141.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# #Data_Structure_I_Day_7_Linked_List #Udemy_Linked_List #Top_Interview_150_Linked_List
33
# #Big_O_Time_O(N)_Space_O(1) #2025_07_25_Time_44_ms_(80.06%)_Space_19.98_MB_(29.66%)
44

5+
from typing import Optional
6+
class ListNode:
7+
def __init__(self, x):
8+
self.val = x
9+
self.next = None
10+
511
# Definition for singly-linked list.
612
# class ListNode:
713
# def __init__(self, x):
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import unittest
2+
from Solution0141 import Solution, ListNode
3+
4+
def build_list_with_cycle(values, cycle_pos):
5+
if not values:
6+
return None
7+
8+
nodes = [ListNode(val) for val in values]
9+
for i in range(len(nodes) - 1):
10+
nodes[i].next = nodes[i + 1]
11+
12+
if cycle_pos >= 0 and cycle_pos < len(nodes):
13+
nodes[-1].next = nodes[cycle_pos]
14+
15+
return nodes[0]
16+
17+
class SolutionTest(unittest.TestCase):
18+
def test_hasCycle(self):
19+
head = build_list_with_cycle([3, 2, 0, -4], 1)
20+
self.assertTrue(Solution().hasCycle(head))
21+
22+
def test_hasCycle2(self):
23+
head = build_list_with_cycle([1, 2], 0)
24+
self.assertTrue(Solution().hasCycle(head))
25+
26+
def test_hasCycle3(self):
27+
head = build_list_with_cycle([1], -1)
28+
self.assertFalse(Solution().hasCycle(head))

src/main/python/g0101_0200/s0142_linked_list_cycle_ii/Solution0142.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
# #Data_Structure_II_Day_10_Linked_List #Level_1_Day_4_Linked_List #Udemy_Linked_List
33
# #Big_O_Time_O(N)_Space_O(1) #2025_07_25_Time_38_ms_(94.15%)_Space_19.60_MB_(79.86%)
44

5+
from typing import Optional
6+
class ListNode:
7+
def __init__(self, x):
8+
self.val = x
9+
self.next = None
10+
511
# Definition for singly-linked list.
612
# class ListNode:
713
# def __init__(self, x):
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import unittest
2+
from Solution0142 import Solution, ListNode
3+
4+
def build_list_with_cycle(values, cycle_pos):
5+
if not values:
6+
return None
7+
8+
nodes = [ListNode(val) for val in values]
9+
for i in range(len(nodes) - 1):
10+
nodes[i].next = nodes[i + 1]
11+
12+
if cycle_pos >= 0 and cycle_pos < len(nodes):
13+
nodes[-1].next = nodes[cycle_pos]
14+
15+
return nodes[0]
16+
17+
class SolutionTest(unittest.TestCase):
18+
def test_detectCycle(self):
19+
head = build_list_with_cycle([3, 2, 0, -4], 1)
20+
result = Solution().detectCycle(head)
21+
self.assertEqual(result.val, 2)
22+
23+
def test_detectCycle2(self):
24+
head = build_list_with_cycle([1, 2], 0)
25+
result = Solution().detectCycle(head)
26+
self.assertEqual(result.val, 1)
27+
28+
def test_detectCycle3(self):
29+
head = build_list_with_cycle([1], -1)
30+
result = Solution().detectCycle(head)
31+
self.assertIsNone(result)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
from LRUCache import LRUCache
3+
4+
class LRUCacheTest(unittest.TestCase):
5+
def test_lruCache(self):
6+
lru = LRUCache(2)
7+
lru.put(1, 1)
8+
lru.put(2, 2)
9+
self.assertEqual(lru.get(1), 1)
10+
lru.put(3, 3)
11+
self.assertEqual(lru.get(2), -1)
12+
lru.put(4, 4)
13+
self.assertEqual(lru.get(1), -1)
14+
self.assertEqual(lru.get(3), 3)
15+
self.assertEqual(lru.get(4), 4)
16+
17+
def test_lruCache2(self):
18+
lru = LRUCache(1)
19+
lru.put(2, 1)
20+
self.assertEqual(lru.get(2), 1)
21+
22+
def test_lruCache3(self):
23+
lru = LRUCache(2)
24+
lru.put(1, 0)
25+
lru.put(2, 2)
26+
self.assertEqual(lru.get(1), 0)
27+
lru.put(3, 3)
28+
self.assertEqual(lru.get(2), -1)
29+
lru.put(4, 4)
30+
self.assertEqual(lru.get(1), -1)
31+
self.assertEqual(lru.get(3), 3)
32+
self.assertEqual(lru.get(4), 4)

src/main/python/g0101_0200/s0148_sort_list/Solution0148.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
# #Divide_and_Conquer #Merge_Sort #Level_2_Day_4_Linked_List #Top_Interview_150_Divide_and_Conquer
33
# #Big_O_Time_O(log(N))_Space_O(log(N)) #2025_07_25_Time_186_ms_(43.67%)_Space_32.96_MB_(41.09%)
44

5+
from typing import Optional
6+
7+
class ListNode:
8+
def __init__(self, val=0, next=None):
9+
self.val = val
10+
self.next = next
11+
512
# Definition for singly-linked list.
613
# class ListNode:
714
# def __init__(self, val=0, next=None):
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import unittest
2+
from Solution0148 import Solution, ListNode
3+
4+
def from_list(values):
5+
dummy = ListNode(0)
6+
curr = dummy
7+
for v in values:
8+
curr.next = ListNode(v)
9+
curr = curr.next
10+
return dummy.next
11+
12+
def to_list(head):
13+
result = []
14+
curr = head
15+
while curr is not None:
16+
result.append(curr.val)
17+
curr = curr.next
18+
return result
19+
20+
class SolutionTest(unittest.TestCase):
21+
def test_sortList(self):
22+
head = from_list([4, 2, 1, 3])
23+
result = Solution().sortList(head)
24+
self.assertEqual(to_list(result), [1, 2, 3, 4])
25+
26+
def test_sortList2(self):
27+
head = from_list([-1, 5, 3, 4, 0])
28+
result = Solution().sortList(head)
29+
self.assertEqual(to_list(result), [-1, 0, 3, 4, 5])
30+
31+
def test_sortList3(self):
32+
head = from_list([])
33+
result = Solution().sortList(head)
34+
self.assertEqual(to_list(result), [])

src/main/python/g0101_0200/s0152_maximum_product_subarray/Solution0152.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# #Dynamic_Programming_I_Day_6 #Level_2_Day_13_Dynamic_Programming #Udemy_Dynamic_Programming
33
# #Big_O_Time_O(N)_Space_O(1) #2025_07_25_Time_3_ms_(98.51%)_Space_18.56_MB_(15.01%)
44

5+
from typing import List
6+
57
class Solution:
68
def maxProduct(self, nums: List[int]) -> int:
79
ans = float('-inf')
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import unittest
2+
from Solution0152 import Solution
3+
4+
class SolutionTest(unittest.TestCase):
5+
def test_maxProduct(self):
6+
self.assertEqual(Solution().maxProduct([2,3,-2,4]), 6)
7+
8+
def test_maxProduct2(self):
9+
self.assertEqual(Solution().maxProduct([-2,0,-1]), 0)
10+
11+
def test_maxProduct3(self):
12+
self.assertEqual(Solution().maxProduct([-2]), -2)
13+
14+
def test_maxProduct4(self):
15+
self.assertEqual(Solution().maxProduct([-2,3,-4]), 24)

src/main/python/g0101_0200/s0153_find_minimum_in_rotated_sorted_array/Solution0153.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# #Binary_Search_I_Day_12 #Udemy_Binary_Search #Top_Interview_150_Binary_Search
33
# #Big_O_Time_O(log_N)_Space_O(log_N) #2025_07_25_Time_0_ms_(100.00%)_Space_18.07_MB_(50.37%)
44

5+
from typing import List
6+
57
class Solution:
68
def findMinUtil(self, nums: List[int], l: int, r: int) -> int:
79
if l == r:

0 commit comments

Comments
 (0)