Skip to content
Open
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
27 changes: 27 additions & 0 deletions 5.Task-design/Task_1/Queen-placement-v.1.0.0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import itertools


# Переборное решение
def enumeration_solution(n):
if n < 0:
print("Введите неотрицательное число")
return 0
count = 0
for permutation in itertools.permutations(range(n)):
valid = True
for i in range(n):
for j in range(i + 1, n):
if abs(i - j) == abs(permutation[i] - permutation[j]):
valid = False
break
if not valid:
break
if valid:
count += 1
return count


if __name__ == "__main__":
size = int(input("Введите число N: "))
result = enumeration_solution(size)
print(f"Количество решений: {result}")
Empty file.
82 changes: 82 additions & 0 deletions 5.Task-design/Task_1/Queen-placement-v.1.2.0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from multiprocessing import Pool
from multiprocessing import cpu_count


# Быстрое решение
def recursive_solution(n):
if n <= 0:
print("Введите неотрицательное число")
return 0
count = 0
array = [0] * n

def check(line, column):
for i in range(line):
if array[i] == column or abs(array[i] - column) == abs(i - line):
return False
return True

def solve(line):
nonlocal count
if line == n:
count += 1
return

for column in range(n):
if check(line, column):
array[line] = column
solve(line + 1)

solve(0)
return count


def wrapper(args):
n, first_col = args

array = [first_col] + [0] * (n - 1)
count = 0

def check(line, column):
for i in range(line):
if array[i] == column or abs(array[i] - column) == abs(i - line):
return False
return True

def solve(line):
nonlocal count
if line == n:
count += 1
return

for column in range(n):
if check(line, column):
array[line] = column
solve(line + 1)

solve(1)
return count


def parallel_solution(n):
if n <= 0:
print("Введите неотрицательное число")
return 0

tasks = [(n, col) for col in range(n)]

with Pool(processes=cpu_count()) as pool:
results = pool.map(wrapper, tasks)

return sum(results)


if __name__ == "__main__":
size = int(input("Введите число N: "))

if size <= 8:
result = recursive_solution(size)
else:
result = parallel_solution(size)

print(f"Количество решений: {result}")
33 changes: 33 additions & 0 deletions workflows/ruff.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Ruff Code Check

on:
push:
branches:
- '**'
pull_request:
branches:
- '**'

jobs:
ruff:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install Ruff
run: |
apt-get install -y pipx
pipx ensurepath
pipx install ruff

- name: Run Ruff check
run: |
ruff check .