Skip to content
Closed
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
10 changes: 5 additions & 5 deletions src/Homework #2/Extended Euclidean algorithm/alghoritm.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
def extended_gcd(a: int, b: int) -> tuple[int, int, int]:
x_previous, x = 1, 0
y_previous, y = 0, 1

while b != 0:
q = a // b
r = a % b

x_previous, x = x, x_previous - q * x
y_previous, y = y, y_previous - q * y

a, b = b, r

return a, x_previous, y_previous


d, x, y = extended_gcd(54, 30)

print(f"GCD(54, 30) = {d}")
print(f"Coefficients: x = {x}, y = {y}")
print(f"Factorization: 54*({x}) + 30*({y}) = {54*x + 30*y}")
print(f"Factorization: 54*({x}) + 30*({y}) = {54 * x + 30 * y}")
44 changes: 44 additions & 0 deletions src/Homework #5/Coding style/biggest_homework.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# A function that verifies the correct placement of the queen vertically and diagonally
def is_safe_direction(
coordinate_1: tuple[int, int],
coordinate_2: tuple[int, int],
) -> bool:
if coordinate_1[1] == coordinate_2[1] or abs(
coordinate_1[1] - coordinate_2[1],
) == abs(coordinate_1[0] - coordinate_2[0]):
return False
return True


N = int(input("Enter the value N: "))

board_size = N**2

counter = 0


stack: list[list[tuple[int, int]]] = [[]]
# The main loop, sorting through the number of combinations of possible placements
# and putting them on the stack
while stack:
# Get the last placement from the top of the stack
placements: list[tuple[int, int]] = stack.pop()

# Check the numebr of placements
if len(placements) == N:
counter += 1
else:
current_row = len(placements)
# Go through the values of possible placements within the same horizontal
for column in range(N):
for placement_column, placement_row in placements:
if not is_safe_direction(
(placement_column, placement_row),
(current_row, column),
):
break
else:
# Add the placement to the stack if possible
stack.append(placements + [(current_row, column)])

print(f"The number of possible different arrangements is {counter}")