Skip to content

clean of unnecessary checks, imports, calls #7993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backtracking/rat_in_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ def run_maze(maze: list[list[int]], i: int, j: int, solutions: list[list[int]])
solutions[i][j] = 1
return True

lower_flag = (not (i < 0)) and (not (j < 0)) # Check lower bounds
lower_flag = (not i < 0) and (not j < 0) # Check lower bounds
upper_flag = (i < size) and (j < size) # Check upper bounds

if lower_flag and upper_flag:
# check for already visited and block points.
block_flag = (not (solutions[i][j])) and (not (maze[i][j]))
block_flag = (not solutions[i][j]) and (not maze[i][j])
if block_flag:
# check visited
solutions[i][j] = 1
Expand Down
2 changes: 1 addition & 1 deletion boolean_algebra/not_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def test_not_gate() -> None:

if __name__ == "__main__":
print(not_gate(0))
print(not_gate(1))
print(not_gate(1))
3 changes: 1 addition & 2 deletions cellular_automata/nagel_schrekenberg.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def construct_highway(

highway = [[-1] * number_of_cells] # Create a highway without any car
i = 0
if initial_speed < 0:
initial_speed = 0
initial_speed = max(initial_speed, 0)
while i < number_of_cells:
highway[0][i] = (
randint(0, max_speed) if random_speed else initial_speed
Expand Down
4 changes: 2 additions & 2 deletions ciphers/mixed_keyword_cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
s = []
for _ in range(len_temp):
s.append(temp[k])
if not (k < 25):
if k >= 25:
break
k += 1
modalpha.append(s)
Expand All @@ -52,7 +52,7 @@ def mixed_keyword(key: str = "college", pt: str = "UNIVERSITY") -> str:
k = 0
for j in range(len_temp):
for m in modalpha:
if not (len(m) - 1 >= j):
if not len(m) - 1 >= j:
break
d[alpha[k]] = m[j]
if not k < 25:
Expand Down
2 changes: 1 addition & 1 deletion compression/huffman.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def traverse_tree(root: Letter | TreeNode, bitstring: str) -> list[Letter]:
Recursively traverse the Huffman Tree to set each
Letter's bitstring dictionary, and return the list of Letters
"""
if type(root) is Letter:
if isinstance(root, Letter):
root.bitstring[root.letter] = bitstring
return [root]
treenode: TreeNode = root # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion data_structures/heap/min_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def insert(self, node):
self.sift_up(len(self.heap) - 1)

def is_empty(self):
return True if len(self.heap) == 0 else False
return len(self.heap) == 0

def decrease_key(self, node, new_value):
assert (
Expand Down
2 changes: 1 addition & 1 deletion digital_image_processing/test_digital_image_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from digital_image_processing import convert_to_negative as cn
from digital_image_processing import sepia as sp
from digital_image_processing.dithering import burkes as bs
from digital_image_processing.edge_detection import canny as canny
from digital_image_processing.edge_detection import canny
from digital_image_processing.filters import convolve as conv
from digital_image_processing.filters import gaussian_filter as gg
from digital_image_processing.filters import local_binary_pattern as lbp
Expand Down
5 changes: 2 additions & 3 deletions dynamic_programming/fizz_buzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ def fizz_buzz(number: int, iterations: int) -> str:
...
ValueError: iterations must be defined as integers
"""

if not type(iterations) == int:
if not isinstance(iterations, int):
raise ValueError("iterations must be defined as integers")
if not type(number) == int or not number >= 1:
if not isinstance(number, int) or not number >= 1:
raise ValueError(
"""starting number must be
and integer and be more than 0"""
Expand Down
3 changes: 1 addition & 2 deletions dynamic_programming/max_sub_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ def max_sub_array(nums: list[int]) -> int:
current = 0
for i in nums:
current += i
if current < 0:
current = 0
current = max(current, 0)
best = max(best, current)
return best

Expand Down
8 changes: 4 additions & 4 deletions graphs/directed_and_undirected_(weighted)_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def cycle_nodes(self):
and not on_the_way_back
):
len_stack = len(stack) - 1
while True and len_stack >= 0:
while len_stack >= 0:
if stack[len_stack] == node[1]:
anticipating_nodes.add(node[1])
break
Expand Down Expand Up @@ -220,7 +220,7 @@ def has_cycle(self):
and not on_the_way_back
):
len_stack_minus_one = len(stack) - 1
while True and len_stack_minus_one >= 0:
while len_stack_minus_one >= 0:
if stack[len_stack_minus_one] == node[1]:
anticipating_nodes.add(node[1])
break
Expand Down Expand Up @@ -392,7 +392,7 @@ def cycle_nodes(self):
and not on_the_way_back
):
len_stack = len(stack) - 1
while True and len_stack >= 0:
while len_stack >= 0:
if stack[len_stack] == node[1]:
anticipating_nodes.add(node[1])
break
Expand Down Expand Up @@ -445,7 +445,7 @@ def has_cycle(self):
and not on_the_way_back
):
len_stack_minus_one = len(stack) - 1
while True and len_stack_minus_one >= 0:
while len_stack_minus_one >= 0:
if stack[len_stack_minus_one] == node[1]:
anticipating_nodes.add(node[1])
break
Expand Down
3 changes: 2 additions & 1 deletion graphs/multi_heuristic_astar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import heapq
import sys

import numpy as np

Expand Down Expand Up @@ -116,7 +117,7 @@ def do_something(back_pointer, goal, start):
print(x, end=" ")
x = back_pointer[x]
print(x)
quit()
sys.exit()


def valid(p: TPos):
Expand Down
2 changes: 1 addition & 1 deletion linear_algebra/src/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def component(self, i: int) -> float:
input: index (0-indexed)
output: the i-th component of the vector.
"""
if type(i) is int and -len(self.__components) <= i < len(self.__components):
if isinstance(i, int) and -len(self.__components) <= i < len(self.__components):
return self.__components[i]
else:
raise Exception("index out of range")
Expand Down
10 changes: 2 additions & 8 deletions machine_learning/sequential_minimum_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,16 +388,10 @@ def _norm(self, data):
return (data - self._min) / (self._max - self._min)

def _is_unbound(self, index):
if 0.0 < self.alphas[index] < self._c:
return True
else:
return False
return bool(0.0 < self.alphas[index] < self._c)

def _is_support(self, index):
if self.alphas[index] > 0:
return True
else:
return False
return bool(self.alphas[index] > 0)

@property
def unbound(self):
Expand Down
3 changes: 1 addition & 2 deletions maths/find_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ def find_min(nums: list[int | float]) -> int | float:
raise ValueError("find_min() arg is an empty sequence")
min_num = nums[0]
for num in nums:
if min_num > num:
min_num = num
min_num = min(min_num, num)
return min_num


Expand Down
6 changes: 2 additions & 4 deletions maths/kadanes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ def kadanes(arr: list) -> int:

for i in arr:
max_till_element += i
if max_sum <= max_till_element:
max_sum = max_till_element
if max_till_element < 0:
max_till_element = 0
max_sum = max(max_sum, max_till_element)
max_till_element = max(max_till_element, 0)
return max_sum


Expand Down
6 changes: 2 additions & 4 deletions maths/largest_subarray_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ def max_sub_array_sum(a: list, size: int = 0):
max_ending_here = 0
for i in range(0, size):
max_ending_here = max_ending_here + a[i]
if max_so_far < max_ending_here:
max_so_far = max_ending_here
if max_ending_here < 0:
max_ending_here = 0
max_so_far = max(max_so_far, max_ending_here)
max_ending_here = max(max_ending_here, 0)
return max_so_far


Expand Down
2 changes: 1 addition & 1 deletion maths/series/geometric_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def geometric_series(
power = 1
multiple = common_ratio_r
for _ in range(int(nth_term)):
if series == []:
if not series:
series.append(start_term_a)
else:
power += 1
Expand Down
2 changes: 1 addition & 1 deletion networking_flow/ford_fulkerson.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def bfs(graph, s, t, parent):
visited[ind] = True
parent[ind] = u

return True if visited[t] else False
return visited[t]


def ford_fulkerson(graph, source, sink):
Expand Down
2 changes: 1 addition & 1 deletion networking_flow/minimum_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def bfs(graph, s, t, parent):
visited[ind] = True
parent[ind] = u

return True if visited[t] else False
return visited[t]


def mincut(graph, source, sink):
Expand Down
10 changes: 3 additions & 7 deletions other/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,9 @@ def is_strong_password(password: str, min_length: int = 8) -> bool:
num = any(char in digits for char in password)
spec_char = any(char in punctuation for char in password)

if upper and lower and num and spec_char:
return True

else:
# Passwords should contain UPPERCASE, lowerase
# numbers, and special characters
return False
return upper and lower and num and spec_char
# Passwords should contain UPPERCASE, lowerase
# numbers, and special characters


def main():
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_025/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def fibonacci(n: int) -> int:
144

"""
if n == 1 or type(n) is not int:
if n == 1 or not isinstance(n, int):
return 0
elif n == 2:
return 1
Expand Down
2 changes: 1 addition & 1 deletion project_euler/problem_036/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def is_palindrome(n: int | str) -> bool:
False
"""
n = str(n)
return True if n == n[::-1] else False
return n == n[::-1]


def solution(n: int = 1000000):
Expand Down
4 changes: 2 additions & 2 deletions quantum/q_fourier_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def quantum_fourier_transform(number_of_qubits: int = 3) -> qiskit.result.counts
...
ValueError: number of qubits must be exact integer.
"""
if type(number_of_qubits) == str:
if isinstance(number_of_qubits, str):
raise TypeError("number of qubits must be a integer.")
if not number_of_qubits > 0:
if number_of_qubits <= 0:
raise ValueError("number of qubits must be > 0.")
if math.floor(number_of_qubits) != number_of_qubits:
raise ValueError("number of qubits must be exact integer.")
Expand Down
6 changes: 5 additions & 1 deletion quantum/q_full_adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ def quantum_full_adder(
...
ValueError: inputs must be less or equal to 2.
"""
if (type(input_1) == str) or (type(input_2) == str) or (type(carry_in) == str):
if (
isinstance(input_1, str)
or isinstance(input_2, str)
or isinstance(carry_in, str)
):
raise TypeError("inputs must be integers.")

if (input_1 < 0) or (input_2 < 0) or (carry_in < 0):
Expand Down
2 changes: 1 addition & 1 deletion quantum/superdense_coding.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def superdense_coding(bit_1: int = 1, bit_2: int = 1) -> qiskit.result.counts.Co
...
ValueError: inputs must be less or equal to 1.
"""
if (type(bit_1) == str) or (type(bit_2) == str):
if isinstance(bit_1, str) or isinstance(bit_2, str):
raise TypeError("inputs must be integers.")
if (bit_1 < 0) or (bit_2 < 0):
raise ValueError("inputs must be positive.")
Expand Down
2 changes: 1 addition & 1 deletion sorts/msd_radix_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def _msd_radix_sort_inplace(
j = end_index - 1
while i <= j:
changed = False
if not ((list_of_ints[i] >> bit_position) & 1):
if not (list_of_ints[i] >> bit_position) & 1:
# found zero at the beginning
i += 1
changed = True
Expand Down
2 changes: 1 addition & 1 deletion strings/aho_corasick.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def search_in(self, string: str) -> dict[str, list[int]]:
else:
current_state = next_state
for key in self.adlist[current_state]["output"]:
if not (key in result):
if key not in result:
result[key] = []
result[key].append(i - len(key) + 1)
return result
Expand Down