Skip to content
Closed
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4fcf7ff
Create beautiful-arrangement
zirea3l Oct 27, 2025
3a5a18c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2025
1448767
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
fb5ecf4
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
69a579b
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
342669a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2025
8e9407b
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
e2c218b
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
cd38be7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2025
adf05c5
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
4965e59
Merge branch 'master' of https://github.com/zirea3l/Python
zirea3l Oct 27, 2025
d96f565
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2025
5e0a91d
Update beautiful_arrangement.py
zirea3l Oct 27, 2025
c2c10ee
Merge branch 'master' of https://github.com/zirea3l/Python
zirea3l Oct 27, 2025
88d1ecf
Create arithmetic_slices.py
zirea3l Oct 27, 2025
ca34f0a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2025
898b97f
Update arithmetic_slices.py
zirea3l Oct 27, 2025
cf8d7bb
Update arithmetic_slices.py
zirea3l Oct 27, 2025
61d5edd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 27, 2025
eff3196
Delete arithmetic_slices.py
zirea3l Oct 27, 2025
dab1c52
Update arithmetic_slices.py
zirea3l Oct 27, 2025
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
56 changes: 56 additions & 0 deletions dynamic_programming/beautiful_arrangement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Suppose you have n integers labeled 1 through n. A permutation of those n integers
perm (1-indexed) is considered a "beautiful arrangement" if for every i (1 <= i <= n),
either of the following is true:

-> perm[i] is divisible by i.
-> i is divisible by perm[i].
Given an integer n, return the number of the "beautiful arrangements" that you can construct.

Check failure on line 8 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/beautiful_arrangement.py:8:89: E501 Line too long (93 > 88)

"""
# Solution using Backtracking

class beautifularrange:

Check failure on line 13 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N801)

dynamic_programming/beautiful_arrangement.py:13:7: N801 Class name `beautifularrange` should use CapWords convention

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class names should follow the CamelCase naming convention. Please update the following name accordingly: beautifularrange

# funtion call; n is the size of the permutation (numbers 1..n)
def countarrangement(self, n: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function countarrangement

Please provide descriptive name for the parameter: n


Check failure on line 16 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

dynamic_programming/beautiful_arrangement.py:16:1: W293 Blank line contains whitespace
self.count = 0
"""
We initialize a counter to record how many valid arrangements we find.

Check failure on line 19 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

dynamic_programming/beautiful_arrangement.py:19:79: W291 Trailing whitespace
Using self.count lets the nested function modify it without nonlocal.
"""

Check failure on line 22 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

dynamic_programming/beautiful_arrangement.py:22:1: W293 Blank line contains whitespace
used = [False] * (n + 1)
"""
A boolean list to mark which numbers have

Check failure on line 25 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

dynamic_programming/beautiful_arrangement.py:25:50: W291 Trailing whitespace
already been placed in the permutation.
"""

def backtrack(pos):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide type hint for the parameter: pos

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: backtrack. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file dynamic_programming/beautiful_arrangement.py, please provide doctest for the function backtrack

Please provide type hint for the parameter: pos

"""
Define the recursive backtracking function.
pos is the current position in the permutation we are filling (1-indexed).
We try to assign a number to position pos.
"""
if pos > n:
self.count += 1

Check failure on line 36 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

dynamic_programming/beautiful_arrangement.py:36:32: W291 Trailing whitespace
# We found a complete valid arrangement, so increment the total count.
return
for num in range(
1, n + 1
): # Try every candidate number num for the current position pos.
"""
Two checks in one:
1. not used[num] — the number num has not been placed yet (we can use it).

Check failure on line 44 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/beautiful_arrangement.py:44:89: E501 Line too long (90 > 88)
2. (num % pos == 0 or pos % num == 0) — the beautiful-arrangement condition:

Check failure on line 45 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/beautiful_arrangement.py:45:89: E501 Line too long (92 > 88)
number num is compatible with position pos (either num divides pos or pos divides num).

Check failure on line 46 in dynamic_programming/beautiful_arrangement.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

dynamic_programming/beautiful_arrangement.py:46:89: E501 Line too long (103 > 88)
If both are true, num is a valid choice for pos.

"""
if not used[num] and (num % pos == 0 or pos % num == 0):
used[num] = True
backtrack(pos + 1)
used[num] = False

backtrack(1)
return self.count