Skip to content

Commit f32d611

Browse files
authored
clean of unnecessary checks, imports, calls (TheAlgorithms#7993)
1 parent a25c53e commit f32d611

File tree

27 files changed

+44
-57
lines changed

27 files changed

+44
-57
lines changed

backtracking/rat_in_maze.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
8888
solutions[i][j] = 1
8989
return True
9090

91-
lower_flag = (not (i < 0)) and (not (j < 0)) # Check lower bounds
91+
lower_flag = (not i < 0) and (not j < 0) # Check lower bounds
9292
upper_flag = (i < size) and (j < size) # Check upper bounds
9393

9494
if lower_flag and upper_flag:
9595
# check for already visited and block points.
96-
block_flag = (not (solutions[i][j])) and (not (maze[i][j]))
96+
block_flag = (not solutions[i][j]) and (not maze[i][j])
9797
if block_flag:
9898
# check visited
9999
solutions[i][j] = 1

boolean_algebra/not_gate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ def test_not_gate() -> None:
3434

3535
if __name__ == "__main__":
3636
print(not_gate(0))
37-
print(not_gate(1))
37+
print(not_gate(1))

cellular_automata/nagel_schrekenberg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ def construct_highway(
4545

4646
highway = [[-1] * number_of_cells] # Create a highway without any car
4747
i = 0
48-
if initial_speed < 0:
49-
initial_speed = 0
48+
initial_speed = max(initial_speed, 0)
5049
while i < number_of_cells:
5150
highway[0][i] = (
5251
randint(0, max_speed) if random_speed else initial_speed

ciphers/mixed_keyword_cypher.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
4242
s = []
4343
for _ in range(len_temp):
4444
s.append(temp[k])
45-
if not (k < 25):
45+
if k >= 25:
4646
break
4747
k += 1
4848
modalpha.append(s)
@@ -52,7 +52,7 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
5252
k = 0
5353
for j in range(len_temp):
5454
for m in modalpha:
55-
if not (len(m) - 1 >= j):
55+
if not len(m) - 1 >= j:
5656
break
5757
d[alpha[k]] = m[j]
5858
if not k < 25:

compression/huffman.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
5656
Recursively traverse the Huffman Tree to set each
5757
Letter's bitstring dictionary, and return the list of Letters
5858
"""
59-
if type(root) is Letter:
59+
if isinstance(root, Letter):
6060
root.bitstring[root.letter] = bitstring
6161
return [root]
6262
treenode: TreeNode = root # type: ignore

data_structures/heap/min_heap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def insert(self, node):
121121
self.sift_up(len(self.heap) - 1)
122122

123123
def is_empty(self):
124-
return True if len(self.heap) == 0 else False
124+
return len(self.heap) == 0
125125

126126
def decrease_key(self, node, new_value):
127127
assert (

digital_image_processing/test_digital_image_processing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from digital_image_processing import convert_to_negative as cn
1111
from digital_image_processing import sepia as sp
1212
from digital_image_processing.dithering import burkes as bs
13-
from digital_image_processing.edge_detection import canny as canny
13+
from digital_image_processing.edge_detection import canny
1414
from digital_image_processing.filters import convolve as conv
1515
from digital_image_processing.filters import gaussian_filter as gg
1616
from digital_image_processing.filters import local_binary_pattern as lbp

dynamic_programming/fizz_buzz.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ def fizz_buzz(number: int, iterations: int) -> str:
3333
...
3434
ValueError: iterations must be defined as integers
3535
"""
36-
37-
if not type(iterations) == int:
36+
if not isinstance(iterations, int):
3837
raise ValueError("iterations must be defined as integers")
39-
if not type(number) == int or not number >= 1:
38+
if not isinstance(number, int) or not number >= 1:
4039
raise ValueError(
4140
"""starting number must be
4241
and integer and be more than 0"""

dynamic_programming/max_sub_array.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def max_sub_array(nums: list[int]) -> int:
6262
current = 0
6363
for i in nums:
6464
current += i
65-
if current < 0:
66-
current = 0
65+
current = max(current, 0)
6766
best = max(best, current)
6867
return best
6968

graphs/directed_and_undirected_(weighted)_graph.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def cycle_nodes(self):
167167
and not on_the_way_back
168168
):
169169
len_stack = len(stack) - 1
170-
while True and len_stack >= 0:
170+
while len_stack >= 0:
171171
if stack[len_stack] == node[1]:
172172
anticipating_nodes.add(node[1])
173173
break
@@ -220,7 +220,7 @@ def has_cycle(self):
220220
and not on_the_way_back
221221
):
222222
len_stack_minus_one = len(stack) - 1
223-
while True and len_stack_minus_one >= 0:
223+
while len_stack_minus_one >= 0:
224224
if stack[len_stack_minus_one] == node[1]:
225225
anticipating_nodes.add(node[1])
226226
break
@@ -392,7 +392,7 @@ def cycle_nodes(self):
392392
and not on_the_way_back
393393
):
394394
len_stack = len(stack) - 1
395-
while True and len_stack >= 0:
395+
while len_stack >= 0:
396396
if stack[len_stack] == node[1]:
397397
anticipating_nodes.add(node[1])
398398
break
@@ -445,7 +445,7 @@ def has_cycle(self):
445445
and not on_the_way_back
446446
):
447447
len_stack_minus_one = len(stack) - 1
448-
while True and len_stack_minus_one >= 0:
448+
while len_stack_minus_one >= 0:
449449
if stack[len_stack_minus_one] == node[1]:
450450
anticipating_nodes.add(node[1])
451451
break

0 commit comments

Comments
 (0)