From 02fb9fbb475b9170feec3b7a784801139915b721 Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 1 Oct 2025 13:26:17 +0300 Subject: [PATCH 1/6] Remove HW2-PersonalPage folder --- HW2-PersonalPage/_config.yml | 3 --- HW2-PersonalPage/index.md | 10 ---------- 2 files changed, 13 deletions(-) delete mode 100644 HW2-PersonalPage/_config.yml delete mode 100644 HW2-PersonalPage/index.md diff --git a/HW2-PersonalPage/_config.yml b/HW2-PersonalPage/_config.yml deleted file mode 100644 index 418485a..0000000 --- a/HW2-PersonalPage/_config.yml +++ /dev/null @@ -1,3 +0,0 @@ -title: Grezin Danil -description: Personal page of Danil Grezin hosted via GitHub -theme: jekyll-theme-hacker diff --git a/HW2-PersonalPage/index.md b/HW2-PersonalPage/index.md deleted file mode 100644 index e1c0c7c..0000000 --- a/HW2-PersonalPage/index.md +++ /dev/null @@ -1,10 +0,0 @@ ---- -layout: default ---- - -# Education -- Bachelor's degree student at St. Petersburg State University in the field of Programming Technology - -# Contacts -- email: grezindanil@gmail.com -- telegram: @dane4ka0_0 From 1b459cdec9b0ebe47ae4838b429cd2f26bd3a384 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 12 Oct 2025 20:54:29 +0300 Subject: [PATCH 2/6] Add brute-force algorithm to count valid queens positions in chess --- HW4/chessQueenPosition/src/brute_force.py | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 HW4/chessQueenPosition/src/brute_force.py diff --git a/HW4/chessQueenPosition/src/brute_force.py b/HW4/chessQueenPosition/src/brute_force.py new file mode 100644 index 0000000..678565d --- /dev/null +++ b/HW4/chessQueenPosition/src/brute_force.py @@ -0,0 +1,34 @@ +import itertools + + +def check_diagonal_attacks(queen_positions): + for i in range(len(queen_positions) - 1): + for j in range(i + 1, len(queen_positions)): + # Diagonal attack occurs when |row1 - row2| == |col1 - col2| + if abs(queen_positions[i][0] - queen_positions[j][0]) == abs(queen_positions[i][1] - queen_positions[j][1]): + return False + return True + + +def brute_force_n_queens(n): + solutions_count = 0 + # Generate all collumn non-repeating permutations (queens will be in different rows anyway) + all_col_permutations = itertools.permutations(range(n)) + + for col_positions in all_col_permutations: + # Assign rows to each column and put results in the list + queen_positions = list(enumerate(col_positions)) + if check_diagonal_attacks(queen_positions): + solutions_count += 1 + + return solutions_count + + +def main(): + BOARD_SIZE = 8 + print("result:") + print(brute_force_n_queens(BOARD_SIZE)) + + +if __name__ == "__main__": + main() From 290b3260f153f241d8135f9ed1a8f84856daaf8c Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 12 Oct 2025 20:54:58 +0300 Subject: [PATCH 3/6] Add recursive algorithm to count valid queens positions in chess --- HW4/chessQueenPosition/src/recursive.py | 44 +++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 HW4/chessQueenPosition/src/recursive.py diff --git a/HW4/chessQueenPosition/src/recursive.py b/HW4/chessQueenPosition/src/recursive.py new file mode 100644 index 0000000..0fde684 --- /dev/null +++ b/HW4/chessQueenPosition/src/recursive.py @@ -0,0 +1,44 @@ +def is_pos_safe(queen_positions, new_pos): + for q_pos in queen_positions: + # Diagonal attack occurs when |row1 - row2| == |col1 - col2| + if abs(q_pos[0] - new_pos[0]) == abs(q_pos[1] - new_pos[1]): + return False + return True + + +def recursive_n_queens(n, row=0, queen_positions=None, used_cols=None): + if queen_positions is None: + queen_positions = [] + if used_cols is None: + used_cols = set() + + if row == n: + return 1 + + solutions_count = 0 + + for col in range(n): + if col in used_cols: + continue + + if is_pos_safe(queen_positions, [row, col]): + queen_positions.append([row, col]) + used_cols.add(col) + + solutions_count += recursive_n_queens(n, row + 1, queen_positions, used_cols) + + # Backtrack + queen_positions.pop() + used_cols.remove(col) + + return solutions_count + + +def main(): + BOARD_SIZE = 8 + print("result:") + print(recursive_n_queens(BOARD_SIZE)) + + +if __name__ == "__main__": + main() From e3c837cb09ca7e83daac32a7ee5ad8ce1ac9e60c Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 12 Oct 2025 20:55:13 +0300 Subject: [PATCH 4/6] Add bitwise algorithm to count valid queens positions in chess --- HW4/chessQueenPosition/src/bitwise.py | 36 +++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 HW4/chessQueenPosition/src/bitwise.py diff --git a/HW4/chessQueenPosition/src/bitwise.py b/HW4/chessQueenPosition/src/bitwise.py new file mode 100644 index 0000000..52a691b --- /dev/null +++ b/HW4/chessQueenPosition/src/bitwise.py @@ -0,0 +1,36 @@ +def bitwise_n_queens(n): + bitmask = (1 << n) - 1 + + def count_solutions(row=0, cols=0, ascending_diag=0, descending_diag=0): + if row == n: + return 1 + + solutions_count = 0 + + all_attacks = cols | ascending_diag | descending_diag + available_positions = ~all_attacks & bitmask # 1 = available + + while available_positions: + col_bit = available_positions & -available_positions # Rightmost available position + available_positions ^= col_bit + + solutions_count += count_solutions( + row + 1, + cols | col_bit, + (ascending_diag | col_bit) << 1, # Diagonals shift as we move down + (descending_diag | col_bit) >> 1, + ) + + return solutions_count + + return count_solutions() + + +def main(): + BOARD_SIZE = 8 + print("result:") + print(bitwise_n_queens(BOARD_SIZE)) + + +if __name__ == "__main__": + main() From 435e25e291f70ab6054593c2dac51a65845264c6 Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 12 Oct 2025 20:55:33 +0300 Subject: [PATCH 5/6] Add complexity analysis for each algorithm --- HW4/chessQueenPosition/src/complexity.md | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 HW4/chessQueenPosition/src/complexity.md diff --git a/HW4/chessQueenPosition/src/complexity.md b/HW4/chessQueenPosition/src/complexity.md new file mode 100644 index 0000000..42d1f54 --- /dev/null +++ b/HW4/chessQueenPosition/src/complexity.md @@ -0,0 +1,26 @@ +# Оценка сложности / Complexity analysis + +## 1. Переборное решение / Brute-force solution + +Сложность: O(N! * N^2) +Обоснование: N! перестановок * O(N^2) проверка корректности всех пар ферзей (N(N-1)/2 сравнений) + +Complexity: O(N! * N^2) +Explanation: N! permutations * O(N^2) validity check of all queen pairs (N(N-1)/2 comparisons) + +## 2. Рекурсивное решение / Recursive solution + +Сложность: O(N! * N) +Обоснование: N! расстановок * O(N) проверка корректности для каждого нового ферзя + +Complexity: O(N! * N) +Explanation: N! configurations * O(N) validity check for each new queen + +## 3. Побитовое решение / Bitwise solution + +Сложность: O(N!) +Обоснование: N! расстановок * O(1) битовые операции + +Complexity: O(N!) +Explanation: N! configurations * O(1) bitwise operations + From b750cd55aee48850bbe6b99e46123289eb58145f Mon Sep 17 00:00:00 2001 From: Dan Date: Sun, 12 Oct 2025 21:08:08 +0300 Subject: [PATCH 6/6] Add final newline --- HW4/chessQueenPosition/src/bitwise.py | 1 + HW4/chessQueenPosition/src/brute_force.py | 1 + HW4/chessQueenPosition/src/recursive.py | 1 + 3 files changed, 3 insertions(+) diff --git a/HW4/chessQueenPosition/src/bitwise.py b/HW4/chessQueenPosition/src/bitwise.py index 52a691b..f252670 100644 --- a/HW4/chessQueenPosition/src/bitwise.py +++ b/HW4/chessQueenPosition/src/bitwise.py @@ -34,3 +34,4 @@ def main(): if __name__ == "__main__": main() + diff --git a/HW4/chessQueenPosition/src/brute_force.py b/HW4/chessQueenPosition/src/brute_force.py index 678565d..5d6e08c 100644 --- a/HW4/chessQueenPosition/src/brute_force.py +++ b/HW4/chessQueenPosition/src/brute_force.py @@ -32,3 +32,4 @@ def main(): if __name__ == "__main__": main() + diff --git a/HW4/chessQueenPosition/src/recursive.py b/HW4/chessQueenPosition/src/recursive.py index 0fde684..d42cca9 100644 --- a/HW4/chessQueenPosition/src/recursive.py +++ b/HW4/chessQueenPosition/src/recursive.py @@ -42,3 +42,4 @@ def main(): if __name__ == "__main__": main() +