mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
clean of unnecessary checks, imports, calls (#7993)
This commit is contained in:
parent
a25c53e8b0
commit
f32d611689
|
@ -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
|
||||
|
|
|
@ -34,4 +34,4 @@ def test_not_gate() -> None:
|
|||
|
||||
if __name__ == "__main__":
|
||||
print(not_gate(0))
|
||||
print(not_gate(1))
|
||||
print(not_gate(1))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
@ -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:
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -121,7 +121,7 @@ class MinHeap:
|
|||
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 (
|
||||
|
|
|
@ -10,7 +10,7 @@ from digital_image_processing import change_contrast as cc
|
|||
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
|
||||
|
|
|
@ -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"""
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -167,7 +167,7 @@ class DirectedGraph:
|
|||
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
|
||||
|
@ -220,7 +220,7 @@ class DirectedGraph:
|
|||
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
|
||||
|
@ -392,7 +392,7 @@ class Graph:
|
|||
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
|
||||
|
@ -445,7 +445,7 @@ class Graph:
|
|||
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
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import heapq
|
||||
import sys
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
@ -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):
|
||||
|
|
|
@ -129,7 +129,7 @@ class Vector:
|
|||
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")
|
||||
|
|
|
@ -388,16 +388,10 @@ class SmoSVM:
|
|||
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):
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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():
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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.")
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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.")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -84,7 +84,7 @@ class Automaton:
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user