diff --git a/src/Homework #2/Extended Euclidean algorithm/alghoritm.py b/src/Homework #2/Extended Euclidean algorithm/alghoritm.py index f390697..eb2d340 100644 --- a/src/Homework #2/Extended Euclidean algorithm/alghoritm.py +++ b/src/Homework #2/Extended Euclidean algorithm/alghoritm.py @@ -1,16 +1,16 @@ 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 diff --git a/src/Homework #5/Code style and linters/formatted_homework.py b/src/Homework #5/Code style and linters/formatted_homework.py new file mode 100644 index 0000000..a559343 --- /dev/null +++ b/src/Homework #5/Code style and linters/formatted_homework.py @@ -0,0 +1,34 @@ +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]]] = [[]] +while stack: + placements: list[tuple[int, int]] = stack.pop() + + if len(placements) == N: + counter += 1 + else: + current_row = len(placements) + 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: + stack.append(placements + [(current_row, column)]) + +print(f"The number of possible different arrangements is {counter}")