Skip to content

Commit e905b82

Browse files
authored
Improved tasks 155-452
1 parent fc963c1 commit e905b82

File tree

20 files changed

+127
-129
lines changed

20 files changed

+127
-129
lines changed

src/main/python/g0101_0200/s0155_min_stack/MinStack_test.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@
33

44
class MinStackTest(unittest.TestCase):
55
def test_minStack(self):
6-
minStack = MinStack()
7-
minStack.push(-2)
8-
minStack.push(0)
9-
minStack.push(-3)
10-
self.assertEqual(minStack.getMin(), -3)
11-
minStack.pop()
12-
self.assertEqual(minStack.top(), 0)
13-
self.assertEqual(minStack.getMin(), -2)
6+
min_stack = MinStack()
7+
min_stack.push(-2)
8+
min_stack.push(0)
9+
min_stack.push(-3)
10+
self.assertEqual(min_stack.getMin(), -3)
11+
min_stack.pop()
12+
self.assertEqual(min_stack.top(), 0)
13+
self.assertEqual(min_stack.getMin(), -2)
1414

1515
def test_minStack2(self):
16-
minStack = MinStack()
17-
minStack.push(0)
18-
minStack.push(1)
19-
minStack.push(0)
20-
self.assertEqual(minStack.getMin(), 0)
21-
minStack.pop()
22-
self.assertEqual(minStack.getMin(), 0)
16+
min_stack = MinStack()
17+
min_stack.push(0)
18+
min_stack.push(1)
19+
min_stack.push(0)
20+
self.assertEqual(min_stack.getMin(), 0)
21+
min_stack.pop()
22+
self.assertEqual(min_stack.getMin(), 0)
2323

2424
def test_minStack3(self):
25-
minStack = MinStack()
26-
minStack.push(1)
27-
minStack.push(2)
28-
self.assertEqual(minStack.top(), 2)
29-
self.assertEqual(minStack.getMin(), 1)
30-
minStack.pop()
31-
self.assertEqual(minStack.getMin(), 1)
32-
self.assertEqual(minStack.top(), 1)
25+
min_stack = MinStack()
26+
min_stack.push(1)
27+
min_stack.push(2)
28+
self.assertEqual(min_stack.top(), 2)
29+
self.assertEqual(min_stack.getMin(), 1)
30+
min_stack.pop()
31+
self.assertEqual(min_stack.getMin(), 1)
32+
self.assertEqual(min_stack.top(), 1)

src/main/python/g0101_0200/s0160_intersection_of_two_linked_lists/Solution0160.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def __init__(self, x):
1515
# self.val = x
1616
# self.next = None
1717
class Solution:
18-
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
19-
node1, node2 = headA, headB
18+
def getIntersectionNode(self, head_a: ListNode, head_b: ListNode) -> Optional[ListNode]:
19+
node1, node2 = head_a, head_b
2020
while node1 != node2:
21-
node1 = headB if node1 is None else node1.next
22-
node2 = headA if node2 is None else node2.next
21+
node1 = head_b if node1 is None else node1.next
22+
node2 = head_a if node2 is None else node2.next
2323
return node1
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
import unittest
22
from Solution0160 import Solution, ListNode
33

4-
def build_intersecting_lists(listA, listB, skipA, skipB):
5-
if not listA or not listB:
4+
def build_intersecting_lists(list_a, list_b, skip_a, skip_b):
5+
if not list_a or not list_b:
66
return None, None
77

8-
nodesA = [ListNode(val) for val in listA]
9-
nodesB = [ListNode(val) for val in listB]
8+
nodes_a = [ListNode(val) for val in list_a]
9+
nodes_b = [ListNode(val) for val in list_b]
1010

1111
# Connect listA
12-
for i in range(len(nodesA) - 1):
13-
nodesA[i].next = nodesA[i + 1]
12+
for i in range(len(nodes_a) - 1):
13+
nodes_a[i].next = nodes_a[i + 1]
1414

1515
# Connect listB
16-
for i in range(len(nodesB) - 1):
17-
nodesB[i].next = nodesB[i + 1]
16+
for i in range(len(nodes_b) - 1):
17+
nodes_b[i].next = nodes_b[i + 1]
1818

1919
# Create intersection
20-
if skipA < len(nodesA) and skipB < len(nodesB):
21-
nodesB[-1].next = nodesA[skipA]
20+
if skip_a < len(nodes_a) and skip_b < len(nodes_b):
21+
nodes_b[-1].next = nodes_a[skip_a]
2222

23-
return nodesA[0], nodesB[0]
23+
return nodes_a[0], nodes_b[0]
2424

2525
class SolutionTest(unittest.TestCase):
2626
def test_getIntersectionNode(self):
2727
# listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
28-
headA, headB = build_intersecting_lists([4,1,8,4,5], [5,6,1,8,4,5], 2, 3)
29-
result = Solution().getIntersectionNode(headA, headB)
28+
head_a, head_b = build_intersecting_lists([4,1,8,4,5], [5,6,1,8,4,5], 2, 3)
29+
result = Solution().getIntersectionNode(head_a, head_b)
3030
self.assertEqual(result.val, 8)
3131

3232
def test_getIntersectionNode2(self):
3333
# listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
34-
headA, headB = build_intersecting_lists([1,9,1,2,4], [3,2,4], 3, 1)
35-
result = Solution().getIntersectionNode(headA, headB)
34+
head_a, head_b = build_intersecting_lists([1,9,1,2,4], [3,2,4], 3, 1)
35+
result = Solution().getIntersectionNode(head_a, head_b)
3636
self.assertEqual(result.val, 2)
3737

3838
def test_getIntersectionNode3(self):
3939
# listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
40-
headA, headB = build_intersecting_lists([2,6,4], [1,5], 3, 2)
41-
result = Solution().getIntersectionNode(headA, headB)
40+
head_a, head_b = build_intersecting_lists([2,6,4], [1,5], 3, 2)
41+
result = Solution().getIntersectionNode(head_a, head_b)
4242
self.assertIsNone(result)

src/main/python/g0101_0200/s0190_reverse_bits/Solution0190.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Solution:
66
def reverseBits(self, n: int) -> int:
77
ret = 0
88
# because there are 32 bits in total
9-
for i in range(32):
9+
for _ in range(32):
1010
ret = ret << 1
1111
# If the bit is 1 we OR it with 1, ie add 1
1212
if (n & 1) > 0:

src/main/python/g0201_0300/s0207_course_schedule/Solution0207.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class Solution:
99
GRAY = 1
1010
BLACK = 2
1111

12-
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
13-
adj = [[] for _ in range(numCourses)]
12+
def canFinish(self, num_courses: int, prerequisites: List[List[int]]) -> bool:
13+
adj = [[] for _ in range(num_courses)]
1414
for pre in prerequisites:
1515
adj[pre[1]].append(pre[0])
1616

17-
colors = [self.WHITE] * numCourses
17+
colors = [self.WHITE] * num_courses
1818

19-
for i in range(numCourses):
19+
for i in range(num_courses):
2020
if colors[i] == self.WHITE and adj[i] and self.hasCycle(adj, i, colors):
2121
return False
2222
return True

src/main/python/g0201_0300/s0208_implement_trie_prefix_tree/Trie.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
class TrieNode:
77
def __init__(self):
8-
self.children = dict()
9-
self.isWordEnd = False
8+
self.children = {}
9+
self.is_word_end = False
1010

1111
class Trie:
1212
def __init__(self):
@@ -18,15 +18,15 @@ def insert(self, word: str) -> None:
1818
if c not in curr.children:
1919
curr.children[c] = TrieNode()
2020
curr = curr.children[c]
21-
curr.isWordEnd = True
21+
curr.is_word_end = True
2222

2323
def search(self, word: str) -> bool:
2424
curr = self.root
2525
for c in word:
2626
if c not in curr.children:
2727
return False
2828
curr = curr.children[c]
29-
return curr.isWordEnd
29+
return curr.is_word_end
3030

3131
def startsWith(self, prefix: str) -> bool:
3232
curr = self.root

src/main/python/g0201_0300/s0210_course_schedule_ii/Solution0210.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from typing import List, Dict
66

77
class Solution:
8-
def findOrder(self, numCourses: int, prerequisites: List[List[int]]) -> List[int]:
8+
def findOrder(self, num_courses: int, prerequisites: List[List[int]]) -> List[int]:
99
graph = {}
10-
for i in range(numCourses):
10+
for i in range(num_courses):
1111
graph[i] = []
1212
for classes in prerequisites:
1313
graph[classes[0]].append(classes[1])

src/main/python/g0201_0300/s0210_course_schedule_ii/Solution0210_test.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
class SolutionTest(unittest.TestCase):
55
def test_courseScheduleII(self):
66
prerequisites = [[1, 0]]
7-
numCourses = 2
8-
self.assertEqual(Solution().findOrder(numCourses, prerequisites), [0, 1])
7+
num_courses = 2
8+
self.assertEqual(Solution().findOrder(num_courses, prerequisites), [0, 1])
99

1010
def test_courseScheduleII2(self):
1111
prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2]]
12-
numCourses = 4
13-
self.assertEqual(Solution().findOrder(numCourses, prerequisites), [0, 1, 2, 3])
12+
num_courses = 4
13+
self.assertEqual(Solution().findOrder(num_courses, prerequisites), [0, 1, 2, 3])
1414

1515
def test_courseScheduleII3(self):
1616
prerequisites = []
17-
numCourses = 1
18-
self.assertEqual(Solution().findOrder(numCourses, prerequisites), [0])
17+
num_courses = 1
18+
self.assertEqual(Solution().findOrder(num_courses, prerequisites), [0])

src/main/python/g0201_0300/s0212_word_search_ii/Solution0212.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def remove_word(word, root = trie):
2727

2828
return None
2929

30-
def getAns(m, n, root, word):
30+
def get_ans(m, n, root, word):
3131

3232
if root["end"]:
3333
ans.append(word)
@@ -38,14 +38,12 @@ def getAns(m, n, root, word):
3838
for i,j in [[1,0],[-1,0],[0,1],[0,-1]]:
3939
a, b = m+i, n+j
4040
if 0 <= a < y and 0 <= b < x and visited[a][b] == False and root[board[a][b]]:
41-
getAns(a, b, root[board[a][b]], word + board[a][b])
41+
get_ans(a, b, root[board[a][b]], word + board[a][b])
4242
visited[m][n] = False
4343

44-
return
45-
4644
for i in range(y):
4745
for j in range(x):
4846
if trie[board[i][j]]:
49-
getAns(i, j, trie[board[i][j]], board[i][j])
47+
get_ans(i, j, trie[board[i][j]], board[i][j])
5048

5149
return ans

src/main/python/g0201_0300/s0226_invert_binary_tree/Solution0226_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ def test_invertTree2(self):
4848
def test_invertTree3(self):
4949
root = build_tree([])
5050
result = Solution().invertTree(root)
51-
self.assertEqual(result, None)
51+
self.assertIsNone(result)

0 commit comments

Comments
 (0)