mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +00:00
[pre-commit.ci] pre-commit autoupdate (#9013)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.0.285 → v0.0.286](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.285...v0.0.286) - [github.com/tox-dev/pyproject-fmt: 0.13.1 → 1.1.0](https://github.com/tox-dev/pyproject-fmt/compare/0.13.1...1.1.0) * updating DIRECTORY.md * Fis ruff rules PIE808,PLR1714 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
parent
0a9438071e
commit
421ace81ed
|
@ -16,7 +16,7 @@ repos:
|
||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.0.285
|
rev: v0.0.286
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ repos:
|
||||||
- tomli
|
- tomli
|
||||||
|
|
||||||
- repo: https://github.com/tox-dev/pyproject-fmt
|
- repo: https://github.com/tox-dev/pyproject-fmt
|
||||||
rev: "0.13.1"
|
rev: "1.1.0"
|
||||||
hooks:
|
hooks:
|
||||||
- id: pyproject-fmt
|
- id: pyproject-fmt
|
||||||
|
|
||||||
|
|
|
@ -245,7 +245,6 @@
|
||||||
* Stacks
|
* Stacks
|
||||||
* [Balanced Parentheses](data_structures/stacks/balanced_parentheses.py)
|
* [Balanced Parentheses](data_structures/stacks/balanced_parentheses.py)
|
||||||
* [Dijkstras Two Stack Algorithm](data_structures/stacks/dijkstras_two_stack_algorithm.py)
|
* [Dijkstras Two Stack Algorithm](data_structures/stacks/dijkstras_two_stack_algorithm.py)
|
||||||
* [Evaluate Postfix Notations](data_structures/stacks/evaluate_postfix_notations.py)
|
|
||||||
* [Infix To Postfix Conversion](data_structures/stacks/infix_to_postfix_conversion.py)
|
* [Infix To Postfix Conversion](data_structures/stacks/infix_to_postfix_conversion.py)
|
||||||
* [Infix To Prefix Conversion](data_structures/stacks/infix_to_prefix_conversion.py)
|
* [Infix To Prefix Conversion](data_structures/stacks/infix_to_prefix_conversion.py)
|
||||||
* [Next Greater Element](data_structures/stacks/next_greater_element.py)
|
* [Next Greater Element](data_structures/stacks/next_greater_element.py)
|
||||||
|
|
|
@ -152,9 +152,9 @@ def strictly_diagonally_dominant(table: NDArray[float64]) -> bool:
|
||||||
|
|
||||||
is_diagonally_dominant = True
|
is_diagonally_dominant = True
|
||||||
|
|
||||||
for i in range(0, rows):
|
for i in range(rows):
|
||||||
total = 0
|
total = 0
|
||||||
for j in range(0, cols - 1):
|
for j in range(cols - 1):
|
||||||
if i == j:
|
if i == j:
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -20,7 +20,7 @@ def secant_method(lower_bound: float, upper_bound: float, repeats: int) -> float
|
||||||
"""
|
"""
|
||||||
x0 = lower_bound
|
x0 = lower_bound
|
||||||
x1 = upper_bound
|
x1 = upper_bound
|
||||||
for _ in range(0, repeats):
|
for _ in range(repeats):
|
||||||
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
|
x0, x1 = x1, x1 - (f(x1) * (x1 - x0)) / (f(x1) - f(x0))
|
||||||
return x1
|
return x1
|
||||||
|
|
||||||
|
|
|
@ -95,7 +95,7 @@ def util_hamilton_cycle(graph: list[list[int]], path: list[int], curr_ind: int)
|
||||||
return graph[path[curr_ind - 1]][path[0]] == 1
|
return graph[path[curr_ind - 1]][path[0]] == 1
|
||||||
|
|
||||||
# Recursive Step
|
# Recursive Step
|
||||||
for next_ver in range(0, len(graph)):
|
for next_ver in range(len(graph)):
|
||||||
if valid_connection(graph, next_ver, curr_ind, path):
|
if valid_connection(graph, next_ver, curr_ind, path):
|
||||||
# Insert current vertex into path as next transition
|
# Insert current vertex into path as next transition
|
||||||
path[curr_ind] = next_ver
|
path[curr_ind] = next_ver
|
||||||
|
|
|
@ -48,7 +48,7 @@ def is_safe(grid: Matrix, row: int, column: int, n: int) -> bool:
|
||||||
is found) else returns True if it is 'safe'
|
is found) else returns True if it is 'safe'
|
||||||
"""
|
"""
|
||||||
for i in range(9):
|
for i in range(9):
|
||||||
if grid[row][i] == n or grid[i][column] == n:
|
if n in {grid[row][i], grid[i][column]}:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
|
|
@ -20,7 +20,7 @@ def get_reverse_bit_string(number: int) -> str:
|
||||||
)
|
)
|
||||||
raise TypeError(msg)
|
raise TypeError(msg)
|
||||||
bit_string = ""
|
bit_string = ""
|
||||||
for _ in range(0, 32):
|
for _ in range(32):
|
||||||
bit_string += str(number % 2)
|
bit_string += str(number % 2)
|
||||||
number = number >> 1
|
number = number >> 1
|
||||||
return bit_string
|
return bit_string
|
||||||
|
|
|
@ -119,7 +119,7 @@ def decrypt_message(
|
||||||
for i in range(0, len(message) + 1, period):
|
for i in range(0, len(message) + 1, period):
|
||||||
a, b, c = __decrypt_part(message[i : i + period], character_to_number)
|
a, b, c = __decrypt_part(message[i : i + period], character_to_number)
|
||||||
|
|
||||||
for j in range(0, len(a)):
|
for j in range(len(a)):
|
||||||
decrypted_numeric.append(a[j] + b[j] + c[j])
|
decrypted_numeric.append(a[j] + b[j] + c[j])
|
||||||
|
|
||||||
for each in decrypted_numeric:
|
for each in decrypted_numeric:
|
||||||
|
|
|
@ -7,10 +7,10 @@ class SegmentTree:
|
||||||
def __init__(self, size: int) -> None:
|
def __init__(self, size: int) -> None:
|
||||||
self.size = size
|
self.size = size
|
||||||
# approximate the overall size of segment tree with given value
|
# approximate the overall size of segment tree with given value
|
||||||
self.segment_tree = [0 for i in range(0, 4 * size)]
|
self.segment_tree = [0 for i in range(4 * size)]
|
||||||
# create array to store lazy update
|
# create array to store lazy update
|
||||||
self.lazy = [0 for i in range(0, 4 * size)]
|
self.lazy = [0 for i in range(4 * size)]
|
||||||
self.flag = [0 for i in range(0, 4 * size)] # flag for lazy update
|
self.flag = [0 for i in range(4 * size)] # flag for lazy update
|
||||||
|
|
||||||
def left(self, idx: int) -> int:
|
def left(self, idx: int) -> int:
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -125,7 +125,7 @@ def test_circular_linked_list() -> None:
|
||||||
circular_linked_list.insert_tail(6)
|
circular_linked_list.insert_tail(6)
|
||||||
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7))
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(1, 7))
|
||||||
circular_linked_list.insert_head(0)
|
circular_linked_list.insert_head(0)
|
||||||
assert str(circular_linked_list) == "->".join(str(i) for i in range(0, 7))
|
assert str(circular_linked_list) == "->".join(str(i) for i in range(7))
|
||||||
|
|
||||||
assert circular_linked_list.delete_front() == 0
|
assert circular_linked_list.delete_front() == 0
|
||||||
assert circular_linked_list.delete_tail() == 6
|
assert circular_linked_list.delete_tail() == 6
|
||||||
|
|
|
@ -98,7 +98,7 @@ class DoublyLinkedList:
|
||||||
self.tail = new_node
|
self.tail = new_node
|
||||||
else:
|
else:
|
||||||
temp = self.head
|
temp = self.head
|
||||||
for _ in range(0, index):
|
for _ in range(index):
|
||||||
temp = temp.next
|
temp = temp.next
|
||||||
temp.previous.next = new_node
|
temp.previous.next = new_node
|
||||||
new_node.previous = temp.previous
|
new_node.previous = temp.previous
|
||||||
|
@ -149,7 +149,7 @@ class DoublyLinkedList:
|
||||||
self.tail.next = None
|
self.tail.next = None
|
||||||
else:
|
else:
|
||||||
temp = self.head
|
temp = self.head
|
||||||
for _ in range(0, index):
|
for _ in range(index):
|
||||||
temp = temp.next
|
temp = temp.next
|
||||||
delete_node = temp
|
delete_node = temp
|
||||||
temp.next.previous = temp.previous
|
temp.next.previous = temp.previous
|
||||||
|
@ -215,7 +215,7 @@ def test_doubly_linked_list() -> None:
|
||||||
|
|
||||||
linked_list.insert_at_head(0)
|
linked_list.insert_at_head(0)
|
||||||
linked_list.insert_at_tail(11)
|
linked_list.insert_at_tail(11)
|
||||||
assert str(linked_list) == "->".join(str(i) for i in range(0, 12))
|
assert str(linked_list) == "->".join(str(i) for i in range(12))
|
||||||
|
|
||||||
assert linked_list.delete_head() == 0
|
assert linked_list.delete_head() == 0
|
||||||
assert linked_list.delete_at_nth(9) == 10
|
assert linked_list.delete_at_nth(9) == 10
|
||||||
|
|
|
@ -68,7 +68,7 @@ def is_palindrome_dict(head):
|
||||||
middle += 1
|
middle += 1
|
||||||
else:
|
else:
|
||||||
step = 0
|
step = 0
|
||||||
for i in range(0, len(v)):
|
for i in range(len(v)):
|
||||||
if v[i] + v[len(v) - 1 - step] != checksum:
|
if v[i] + v[len(v) - 1 - step] != checksum:
|
||||||
return False
|
return False
|
||||||
step += 1
|
step += 1
|
||||||
|
|
|
@ -370,7 +370,7 @@ def test_singly_linked_list() -> None:
|
||||||
|
|
||||||
linked_list.insert_head(0)
|
linked_list.insert_head(0)
|
||||||
linked_list.insert_tail(11)
|
linked_list.insert_tail(11)
|
||||||
assert str(linked_list) == "->".join(str(i) for i in range(0, 12))
|
assert str(linked_list) == "->".join(str(i) for i in range(12))
|
||||||
|
|
||||||
assert linked_list.delete_head() == 0
|
assert linked_list.delete_head() == 0
|
||||||
assert linked_list.delete_nth(9) == 10
|
assert linked_list.delete_nth(9) == 10
|
||||||
|
@ -378,11 +378,11 @@ def test_singly_linked_list() -> None:
|
||||||
assert len(linked_list) == 9
|
assert len(linked_list) == 9
|
||||||
assert str(linked_list) == "->".join(str(i) for i in range(1, 10))
|
assert str(linked_list) == "->".join(str(i) for i in range(1, 10))
|
||||||
|
|
||||||
assert all(linked_list[i] == i + 1 for i in range(0, 9)) is True
|
assert all(linked_list[i] == i + 1 for i in range(9)) is True
|
||||||
|
|
||||||
for i in range(0, 9):
|
for i in range(9):
|
||||||
linked_list[i] = -i
|
linked_list[i] = -i
|
||||||
assert all(linked_list[i] == -i for i in range(0, 9)) is True
|
assert all(linked_list[i] == -i for i in range(9)) is True
|
||||||
|
|
||||||
linked_list.reverse()
|
linked_list.reverse()
|
||||||
assert str(linked_list) == "->".join(str(i) for i in range(-8, 1))
|
assert str(linked_list) == "->".join(str(i) for i in range(-8, 1))
|
||||||
|
|
|
@ -36,7 +36,7 @@ def calculation_span(price, s):
|
||||||
|
|
||||||
# A utility function to print elements of array
|
# A utility function to print elements of array
|
||||||
def print_array(arr, n):
|
def print_array(arr, n):
|
||||||
for i in range(0, n):
|
for i in range(n):
|
||||||
print(arr[i], end=" ")
|
print(arr[i], end=" ")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -31,8 +31,8 @@ def get_slice(img: np.ndarray, x: int, y: int, kernel_size: int) -> np.ndarray:
|
||||||
def get_gauss_kernel(kernel_size: int, spatial_variance: float) -> np.ndarray:
|
def get_gauss_kernel(kernel_size: int, spatial_variance: float) -> np.ndarray:
|
||||||
# Creates a gaussian kernel of given dimension.
|
# Creates a gaussian kernel of given dimension.
|
||||||
arr = np.zeros((kernel_size, kernel_size))
|
arr = np.zeros((kernel_size, kernel_size))
|
||||||
for i in range(0, kernel_size):
|
for i in range(kernel_size):
|
||||||
for j in range(0, kernel_size):
|
for j in range(kernel_size):
|
||||||
arr[i, j] = math.sqrt(
|
arr[i, j] = math.sqrt(
|
||||||
abs(i - kernel_size // 2) ** 2 + abs(j - kernel_size // 2) ** 2
|
abs(i - kernel_size // 2) ** 2 + abs(j - kernel_size // 2) ** 2
|
||||||
)
|
)
|
||||||
|
|
|
@ -11,8 +11,8 @@ def im2col(image, block_size):
|
||||||
dst_width = rows - block_size[0] + 1
|
dst_width = rows - block_size[0] + 1
|
||||||
image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0]))
|
image_array = zeros((dst_height * dst_width, block_size[1] * block_size[0]))
|
||||||
row = 0
|
row = 0
|
||||||
for i in range(0, dst_height):
|
for i in range(dst_height):
|
||||||
for j in range(0, dst_width):
|
for j in range(dst_width):
|
||||||
window = ravel(image[i : i + block_size[0], j : j + block_size[1]])
|
window = ravel(image[i : i + block_size[0], j : j + block_size[1]])
|
||||||
image_array[row, :] = window
|
image_array[row, :] = window
|
||||||
row += 1
|
row += 1
|
||||||
|
|
|
@ -71,8 +71,8 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
# Iterating through the image and calculating the
|
# Iterating through the image and calculating the
|
||||||
# local binary pattern value for each pixel.
|
# local binary pattern value for each pixel.
|
||||||
for i in range(0, image.shape[0]):
|
for i in range(image.shape[0]):
|
||||||
for j in range(0, image.shape[1]):
|
for j in range(image.shape[1]):
|
||||||
lbp_image[i][j] = local_binary_value(image, i, j)
|
lbp_image[i][j] = local_binary_value(image, i, j)
|
||||||
|
|
||||||
cv2.imshow("local binary pattern", lbp_image)
|
cv2.imshow("local binary pattern", lbp_image)
|
||||||
|
|
|
@ -118,8 +118,8 @@ def test_local_binary_pattern():
|
||||||
|
|
||||||
# Iterating through the image and calculating the local binary pattern value
|
# Iterating through the image and calculating the local binary pattern value
|
||||||
# for each pixel.
|
# for each pixel.
|
||||||
for i in range(0, image.shape[0]):
|
for i in range(image.shape[0]):
|
||||||
for j in range(0, image.shape[1]):
|
for j in range(image.shape[1]):
|
||||||
lbp_image[i][j] = lbp.local_binary_value(image, i, j)
|
lbp_image[i][j] = lbp.local_binary_value(image, i, j)
|
||||||
|
|
||||||
assert lbp_image.any()
|
assert lbp_image.any()
|
||||||
|
|
|
@ -131,7 +131,7 @@ def strassen(matrix1: list, matrix2: list) -> list:
|
||||||
|
|
||||||
# Adding zeros to the matrices so that the arrays dimensions are the same and also
|
# Adding zeros to the matrices so that the arrays dimensions are the same and also
|
||||||
# power of 2
|
# power of 2
|
||||||
for i in range(0, maxim):
|
for i in range(maxim):
|
||||||
if i < dimension1[0]:
|
if i < dimension1[0]:
|
||||||
for _ in range(dimension1[1], maxim):
|
for _ in range(dimension1[1], maxim):
|
||||||
new_matrix1[i].append(0)
|
new_matrix1[i].append(0)
|
||||||
|
@ -146,7 +146,7 @@ def strassen(matrix1: list, matrix2: list) -> list:
|
||||||
final_matrix = actual_strassen(new_matrix1, new_matrix2)
|
final_matrix = actual_strassen(new_matrix1, new_matrix2)
|
||||||
|
|
||||||
# Removing the additional zeros
|
# Removing the additional zeros
|
||||||
for i in range(0, maxim):
|
for i in range(maxim):
|
||||||
if i < dimension1[0]:
|
if i < dimension1[0]:
|
||||||
for _ in range(dimension2[1], maxim):
|
for _ in range(dimension2[1], maxim):
|
||||||
final_matrix[i].pop()
|
final_matrix[i].pop()
|
||||||
|
|
|
@ -5,19 +5,19 @@ class Graph:
|
||||||
def __init__(self, n=0): # a graph with Node 0,1,...,N-1
|
def __init__(self, n=0): # a graph with Node 0,1,...,N-1
|
||||||
self.n = n
|
self.n = n
|
||||||
self.w = [
|
self.w = [
|
||||||
[math.inf for j in range(0, n)] for i in range(0, n)
|
[math.inf for j in range(n)] for i in range(n)
|
||||||
] # adjacency matrix for weight
|
] # adjacency matrix for weight
|
||||||
self.dp = [
|
self.dp = [
|
||||||
[math.inf for j in range(0, n)] for i in range(0, n)
|
[math.inf for j in range(n)] for i in range(n)
|
||||||
] # dp[i][j] stores minimum distance from i to j
|
] # dp[i][j] stores minimum distance from i to j
|
||||||
|
|
||||||
def add_edge(self, u, v, w):
|
def add_edge(self, u, v, w):
|
||||||
self.dp[u][v] = w
|
self.dp[u][v] = w
|
||||||
|
|
||||||
def floyd_warshall(self):
|
def floyd_warshall(self):
|
||||||
for k in range(0, self.n):
|
for k in range(self.n):
|
||||||
for i in range(0, self.n):
|
for i in range(self.n):
|
||||||
for j in range(0, self.n):
|
for j in range(self.n):
|
||||||
self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j])
|
self.dp[i][j] = min(self.dp[i][j], self.dp[i][k] + self.dp[k][j])
|
||||||
|
|
||||||
def show_min(self, u, v):
|
def show_min(self, u, v):
|
||||||
|
|
|
@ -53,7 +53,7 @@ def pull():
|
||||||
key = machine_time % m
|
key = machine_time % m
|
||||||
|
|
||||||
# Evolution (Time Length)
|
# Evolution (Time Length)
|
||||||
for _ in range(0, t):
|
for _ in range(t):
|
||||||
# Variables (Position + Parameters)
|
# Variables (Position + Parameters)
|
||||||
r = params_space[key]
|
r = params_space[key]
|
||||||
value = buffer_space[key]
|
value = buffer_space[key]
|
||||||
|
|
|
@ -135,7 +135,7 @@ def emitter_converter(size_par, data):
|
||||||
|
|
||||||
# Mount the message
|
# Mount the message
|
||||||
cont_bp = 0 # parity bit counter
|
cont_bp = 0 # parity bit counter
|
||||||
for x in range(0, size_par + len(data)):
|
for x in range(size_par + len(data)):
|
||||||
if data_ord[x] is None:
|
if data_ord[x] is None:
|
||||||
data_out.append(str(parity[cont_bp]))
|
data_out.append(str(parity[cont_bp]))
|
||||||
cont_bp += 1
|
cont_bp += 1
|
||||||
|
@ -228,7 +228,7 @@ def receptor_converter(size_par, data):
|
||||||
|
|
||||||
# Mount the message
|
# Mount the message
|
||||||
cont_bp = 0 # Parity bit counter
|
cont_bp = 0 # Parity bit counter
|
||||||
for x in range(0, size_par + len(data_output)):
|
for x in range(size_par + len(data_output)):
|
||||||
if data_ord[x] is None:
|
if data_ord[x] is None:
|
||||||
data_out.append(str(parity[cont_bp]))
|
data_out.append(str(parity[cont_bp]))
|
||||||
cont_bp += 1
|
cont_bp += 1
|
||||||
|
|
|
@ -97,7 +97,7 @@ class SHA1Hash:
|
||||||
for block in self.blocks:
|
for block in self.blocks:
|
||||||
expanded_block = self.expand_block(block)
|
expanded_block = self.expand_block(block)
|
||||||
a, b, c, d, e = self.h
|
a, b, c, d, e = self.h
|
||||||
for i in range(0, 80):
|
for i in range(80):
|
||||||
if 0 <= i < 20:
|
if 0 <= i < 20:
|
||||||
f = (b & c) | ((~b) & d)
|
f = (b & c) | ((~b) & d)
|
||||||
k = 0x5A827999
|
k = 0x5A827999
|
||||||
|
|
|
@ -138,7 +138,7 @@ class SHA256:
|
||||||
|
|
||||||
a, b, c, d, e, f, g, h = self.hashes
|
a, b, c, d, e, f, g, h = self.hashes
|
||||||
|
|
||||||
for index in range(0, 64):
|
for index in range(64):
|
||||||
if index > 15:
|
if index > 15:
|
||||||
# modify the zero-ed indexes at the end of the array
|
# modify the zero-ed indexes at the end of the array
|
||||||
s0 = (
|
s0 = (
|
||||||
|
|
|
@ -110,7 +110,7 @@ def run_gradient_descent():
|
||||||
while True:
|
while True:
|
||||||
j += 1
|
j += 1
|
||||||
temp_parameter_vector = [0, 0, 0, 0]
|
temp_parameter_vector = [0, 0, 0, 0]
|
||||||
for i in range(0, len(parameter_vector)):
|
for i in range(len(parameter_vector)):
|
||||||
cost_derivative = get_cost_derivative(i - 1)
|
cost_derivative = get_cost_derivative(i - 1)
|
||||||
temp_parameter_vector[i] = (
|
temp_parameter_vector[i] = (
|
||||||
parameter_vector[i] - LEARNING_RATE * cost_derivative
|
parameter_vector[i] - LEARNING_RATE * cost_derivative
|
||||||
|
|
|
@ -78,7 +78,7 @@ def run_linear_regression(data_x, data_y):
|
||||||
|
|
||||||
theta = np.zeros((1, no_features))
|
theta = np.zeros((1, no_features))
|
||||||
|
|
||||||
for i in range(0, iterations):
|
for i in range(iterations):
|
||||||
theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
|
theta = run_steep_gradient_descent(data_x, data_y, len_data, alpha, theta)
|
||||||
error = sum_of_square_error(data_x, data_y, len_data, theta)
|
error = sum_of_square_error(data_x, data_y, len_data, theta)
|
||||||
print(f"At Iteration {i + 1} - Error is {error:.5f}")
|
print(f"At Iteration {i + 1} - Error is {error:.5f}")
|
||||||
|
@ -107,7 +107,7 @@ def main():
|
||||||
theta = run_linear_regression(data_x, data_y)
|
theta = run_linear_regression(data_x, data_y)
|
||||||
len_result = theta.shape[1]
|
len_result = theta.shape[1]
|
||||||
print("Resultant Feature vector : ")
|
print("Resultant Feature vector : ")
|
||||||
for i in range(0, len_result):
|
for i in range(len_result):
|
||||||
print(f"{theta[0, i]:.5f}")
|
print(f"{theta[0, i]:.5f}")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -32,10 +32,10 @@ if __name__ == "__main__":
|
||||||
train_x, train_y = [], []
|
train_x, train_y = [], []
|
||||||
test_x, test_y = [], []
|
test_x, test_y = [], []
|
||||||
|
|
||||||
for i in range(0, len(train_data) - forward_days - look_back + 1):
|
for i in range(len(train_data) - forward_days - look_back + 1):
|
||||||
train_x.append(train_data[i : i + look_back])
|
train_x.append(train_data[i : i + look_back])
|
||||||
train_y.append(train_data[i + look_back : i + look_back + forward_days])
|
train_y.append(train_data[i + look_back : i + look_back + forward_days])
|
||||||
for i in range(0, len(test_data) - forward_days - look_back + 1):
|
for i in range(len(test_data) - forward_days - look_back + 1):
|
||||||
test_x.append(test_data[i : i + look_back])
|
test_x.append(test_data[i : i + look_back])
|
||||||
test_y.append(test_data[i + look_back : i + look_back + forward_days])
|
test_y.append(test_data[i + look_back : i + look_back + forward_days])
|
||||||
x_train = np.array(train_x)
|
x_train = np.array(train_x)
|
||||||
|
|
|
@ -101,7 +101,7 @@ def analyze_text(text: str) -> tuple[dict, dict]:
|
||||||
|
|
||||||
# first case when we have space at start.
|
# first case when we have space at start.
|
||||||
two_char_strings[" " + text[0]] += 1
|
two_char_strings[" " + text[0]] += 1
|
||||||
for i in range(0, len(text) - 1):
|
for i in range(len(text) - 1):
|
||||||
single_char_strings[text[i]] += 1
|
single_char_strings[text[i]] += 1
|
||||||
two_char_strings[text[i : i + 2]] += 1
|
two_char_strings[text[i : i + 2]] += 1
|
||||||
return single_char_strings, two_char_strings
|
return single_char_strings, two_char_strings
|
||||||
|
|
|
@ -21,7 +21,7 @@ def totient(n: int) -> list:
|
||||||
for i in range(2, n + 1):
|
for i in range(2, n + 1):
|
||||||
if is_prime[i]:
|
if is_prime[i]:
|
||||||
primes.append(i)
|
primes.append(i)
|
||||||
for j in range(0, len(primes)):
|
for j in range(len(primes)):
|
||||||
if i * primes[j] >= n:
|
if i * primes[j] >= n:
|
||||||
break
|
break
|
||||||
is_prime[i * primes[j]] = False
|
is_prime[i * primes[j]] = False
|
||||||
|
|
|
@ -81,7 +81,7 @@ if __name__ == "__main__":
|
||||||
):
|
):
|
||||||
n = int(input("Enter the number of denominations you want to add: ").strip())
|
n = int(input("Enter the number of denominations you want to add: ").strip())
|
||||||
|
|
||||||
for i in range(0, n):
|
for i in range(n):
|
||||||
denominations.append(int(input(f"Denomination {i}: ").strip()))
|
denominations.append(int(input(f"Denomination {i}: ").strip()))
|
||||||
value = input("Enter the change you want to make in Indian Currency: ").strip()
|
value = input("Enter the change you want to make in Indian Currency: ").strip()
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -28,7 +28,7 @@ def multiplicative_persistence(num: int) -> int:
|
||||||
numbers = [int(i) for i in num_string]
|
numbers = [int(i) for i in num_string]
|
||||||
|
|
||||||
total = 1
|
total = 1
|
||||||
for i in range(0, len(numbers)):
|
for i in range(len(numbers)):
|
||||||
total *= numbers[i]
|
total *= numbers[i]
|
||||||
|
|
||||||
num_string = str(total)
|
num_string = str(total)
|
||||||
|
@ -67,7 +67,7 @@ def additive_persistence(num: int) -> int:
|
||||||
numbers = [int(i) for i in num_string]
|
numbers = [int(i) for i in num_string]
|
||||||
|
|
||||||
total = 0
|
total = 0
|
||||||
for i in range(0, len(numbers)):
|
for i in range(len(numbers)):
|
||||||
total += numbers[i]
|
total += numbers[i]
|
||||||
|
|
||||||
num_string = str(total)
|
num_string = str(total)
|
||||||
|
|
|
@ -45,7 +45,7 @@ def is_harmonic_series(series: list) -> bool:
|
||||||
return True
|
return True
|
||||||
rec_series = []
|
rec_series = []
|
||||||
series_len = len(series)
|
series_len = len(series)
|
||||||
for i in range(0, series_len):
|
for i in range(series_len):
|
||||||
if series[i] == 0:
|
if series[i] == 0:
|
||||||
raise ValueError("Input series cannot have 0 as an element")
|
raise ValueError("Input series cannot have 0 as an element")
|
||||||
rec_series.append(1 / series[i])
|
rec_series.append(1 / series[i])
|
||||||
|
|
|
@ -54,7 +54,7 @@ def spiral_print_clockwise(a: list[list[int]]) -> None:
|
||||||
return
|
return
|
||||||
|
|
||||||
# horizotal printing increasing
|
# horizotal printing increasing
|
||||||
for i in range(0, mat_col):
|
for i in range(mat_col):
|
||||||
print(a[0][i])
|
print(a[0][i])
|
||||||
# vertical printing down
|
# vertical printing down
|
||||||
for i in range(1, mat_row):
|
for i in range(1, mat_row):
|
||||||
|
|
|
@ -7,10 +7,10 @@ def floyd(n):
|
||||||
Parameters:
|
Parameters:
|
||||||
n : size of pattern
|
n : size of pattern
|
||||||
"""
|
"""
|
||||||
for i in range(0, n):
|
for i in range(n):
|
||||||
for _ in range(0, n - i - 1): # printing spaces
|
for _ in range(n - i - 1): # printing spaces
|
||||||
print(" ", end="")
|
print(" ", end="")
|
||||||
for _ in range(0, i + 1): # printing stars
|
for _ in range(i + 1): # printing stars
|
||||||
print("* ", end="")
|
print("* ", end="")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ def get_totients(max_one: int) -> list[int]:
|
||||||
"""
|
"""
|
||||||
totients = [0] * max_one
|
totients = [0] * max_one
|
||||||
|
|
||||||
for i in range(0, max_one):
|
for i in range(max_one):
|
||||||
totients[i] = i
|
totients[i] = i
|
||||||
|
|
||||||
for i in range(2, max_one):
|
for i in range(2, max_one):
|
||||||
|
|
|
@ -49,7 +49,7 @@ def check_bouncy(n: int) -> bool:
|
||||||
raise ValueError("check_bouncy() accepts only integer arguments")
|
raise ValueError("check_bouncy() accepts only integer arguments")
|
||||||
str_n = str(n)
|
str_n = str(n)
|
||||||
sorted_str_n = "".join(sorted(str_n))
|
sorted_str_n = "".join(sorted(str_n))
|
||||||
return sorted_str_n != str_n and sorted_str_n[::-1] != str_n
|
return str_n not in {sorted_str_n, sorted_str_n[::-1]}
|
||||||
|
|
||||||
|
|
||||||
def solution(percent: float = 99) -> int:
|
def solution(percent: float = 99) -> int:
|
||||||
|
|
|
@ -88,7 +88,7 @@ def quantum_full_adder(
|
||||||
|
|
||||||
quantum_circuit = qiskit.QuantumCircuit(qr, cr)
|
quantum_circuit = qiskit.QuantumCircuit(qr, cr)
|
||||||
|
|
||||||
for i in range(0, 3):
|
for i in range(3):
|
||||||
if entry[i] == 2:
|
if entry[i] == 2:
|
||||||
quantum_circuit.h(i) # for hadamard entries
|
quantum_circuit.h(i) # for hadamard entries
|
||||||
elif entry[i] == 1:
|
elif entry[i] == 1:
|
||||||
|
|
|
@ -53,7 +53,7 @@ def calculate_turn_around_time(
|
||||||
loc = 0
|
loc = 0
|
||||||
# Saves the current response ratio.
|
# Saves the current response ratio.
|
||||||
temp = 0
|
temp = 0
|
||||||
for i in range(0, no_of_process):
|
for i in range(no_of_process):
|
||||||
if finished_process[i] == 0 and arrival_time[i] <= current_time:
|
if finished_process[i] == 0 and arrival_time[i] <= current_time:
|
||||||
temp = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[
|
temp = (burst_time[i] + (current_time - arrival_time[i])) / burst_time[
|
||||||
i
|
i
|
||||||
|
@ -87,7 +87,7 @@ def calculate_waiting_time(
|
||||||
"""
|
"""
|
||||||
|
|
||||||
waiting_time = [0] * no_of_process
|
waiting_time = [0] * no_of_process
|
||||||
for i in range(0, no_of_process):
|
for i in range(no_of_process):
|
||||||
waiting_time[i] = turn_around_time[i] - burst_time[i]
|
waiting_time[i] = turn_around_time[i] - burst_time[i]
|
||||||
return waiting_time
|
return waiting_time
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ if __name__ == "__main__":
|
||||||
)
|
)
|
||||||
|
|
||||||
print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time")
|
print("Process name \tArrival time \tBurst time \tTurn around time \tWaiting time")
|
||||||
for i in range(0, no_of_process):
|
for i in range(no_of_process):
|
||||||
print(
|
print(
|
||||||
f"{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t"
|
f"{process_name[i]}\t\t{arrival_time[i]}\t\t{burst_time[i]}\t\t"
|
||||||
f"{turn_around_time[i]}\t\t\t{waiting_time[i]}"
|
f"{turn_around_time[i]}\t\t\t{waiting_time[i]}"
|
||||||
|
|
|
@ -49,7 +49,7 @@ def counting_sort(collection):
|
||||||
|
|
||||||
# place the elements in the output, respecting the original order (stable
|
# place the elements in the output, respecting the original order (stable
|
||||||
# sort) from end to begin, updating counting_arr
|
# sort) from end to begin, updating counting_arr
|
||||||
for i in reversed(range(0, coll_len)):
|
for i in reversed(range(coll_len)):
|
||||||
ordered[counting_arr[collection[i] - coll_min] - 1] = collection[i]
|
ordered[counting_arr[collection[i] - coll_min] - 1] = collection[i]
|
||||||
counting_arr[collection[i] - coll_min] -= 1
|
counting_arr[collection[i] - coll_min] -= 1
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ def cycle_sort(array: list) -> list:
|
||||||
[]
|
[]
|
||||||
"""
|
"""
|
||||||
array_len = len(array)
|
array_len = len(array)
|
||||||
for cycle_start in range(0, array_len - 1):
|
for cycle_start in range(array_len - 1):
|
||||||
item = array[cycle_start]
|
item = array[cycle_start]
|
||||||
|
|
||||||
pos = cycle_start
|
pos = cycle_start
|
||||||
|
|
|
@ -16,9 +16,9 @@ def double_sort(lst):
|
||||||
"""
|
"""
|
||||||
no_of_elements = len(lst)
|
no_of_elements = len(lst)
|
||||||
for _ in range(
|
for _ in range(
|
||||||
0, int(((no_of_elements - 1) / 2) + 1)
|
int(((no_of_elements - 1) / 2) + 1)
|
||||||
): # we don't need to traverse to end of list as
|
): # we don't need to traverse to end of list as
|
||||||
for j in range(0, no_of_elements - 1):
|
for j in range(no_of_elements - 1):
|
||||||
if (
|
if (
|
||||||
lst[j + 1] < lst[j]
|
lst[j + 1] < lst[j]
|
||||||
): # applying bubble sort algorithm from left to right (or forwards)
|
): # applying bubble sort algorithm from left to right (or forwards)
|
||||||
|
|
|
@ -33,7 +33,7 @@ def oe_process(position, value, l_send, r_send, lr_cv, rr_cv, result_pipe):
|
||||||
# we perform n swaps since after n swaps we know we are sorted
|
# we perform n swaps since after n swaps we know we are sorted
|
||||||
# we *could* stop early if we are sorted already, but it takes as long to
|
# we *could* stop early if we are sorted already, but it takes as long to
|
||||||
# find out we are sorted as it does to sort the list with this algorithm
|
# find out we are sorted as it does to sort the list with this algorithm
|
||||||
for i in range(0, 10):
|
for i in range(10):
|
||||||
if (i + position) % 2 == 0 and r_send is not None:
|
if (i + position) % 2 == 0 and r_send is not None:
|
||||||
# send your value to your right neighbor
|
# send your value to your right neighbor
|
||||||
process_lock.acquire()
|
process_lock.acquire()
|
||||||
|
@ -123,7 +123,7 @@ def odd_even_transposition(arr):
|
||||||
p.start()
|
p.start()
|
||||||
|
|
||||||
# wait for the processes to end and write their values to the list
|
# wait for the processes to end and write their values to the list
|
||||||
for p in range(0, len(result_pipe)):
|
for p in range(len(result_pipe)):
|
||||||
arr[p] = result_pipe[p][0].recv()
|
arr[p] = result_pipe[p][0].recv()
|
||||||
process_array_[p].join()
|
process_array_[p].join()
|
||||||
return arr
|
return arr
|
||||||
|
|
|
@ -38,7 +38,7 @@ def rabin_karp(pattern: str, text: str) -> bool:
|
||||||
continue
|
continue
|
||||||
modulus_power = (modulus_power * alphabet_size) % modulus
|
modulus_power = (modulus_power * alphabet_size) % modulus
|
||||||
|
|
||||||
for i in range(0, t_len - p_len + 1):
|
for i in range(t_len - p_len + 1):
|
||||||
if text_hash == p_hash and text[i : i + p_len] == pattern:
|
if text_hash == p_hash and text[i : i + p_len] == pattern:
|
||||||
return True
|
return True
|
||||||
if i == t_len - p_len:
|
if i == t_len - p_len:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user