Skip to content

Commit 2dd8724

Browse files
cclaussgithub-actions
authored andcommitted
Euler problem 551 sol 1: Reduce McCabe code complexity (TheAlgorithms#2141)
* Euler problem 551 sol 1: Reduce McCabe code complexity As discussed in TheAlgorithms#2128 * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 38f7ac6 commit 2dd8724

File tree

3 files changed

+15
-18
lines changed

3 files changed

+15
-18
lines changed

Diff for: backtracking/knight_tour.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55

66
def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
7-
'''
7+
"""
88
Find all the valid positions a knight can move to from the current position.
99
1010
>>> get_valid_pos((1, 3), 4)
1111
[(2, 1), (0, 1), (3, 2)]
12-
'''
12+
"""
1313

1414
y, x = position
1515
positions = [
@@ -20,7 +20,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
2020
(y + 2, x + 1),
2121
(y + 2, x - 1),
2222
(y - 2, x + 1),
23-
(y - 2, x - 1)
23+
(y - 2, x - 1),
2424
]
2525
permissible_positions = []
2626

@@ -33,23 +33,23 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
3333

3434

3535
def is_complete(board: List[List[int]]) -> bool:
36-
'''
36+
"""
3737
Check if the board (matrix) has been completely filled with non-zero values.
3838
3939
>>> is_complete([[1]])
4040
True
4141
4242
>>> is_complete([[1, 2], [3, 0]])
4343
False
44-
'''
44+
"""
4545

4646
return not any(elem == 0 for row in board for elem in row)
4747

4848

4949
def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int) -> bool:
50-
'''
50+
"""
5151
Helper function to solve knight tour problem.
52-
'''
52+
"""
5353

5454
if is_complete(board):
5555
return True
@@ -67,7 +67,7 @@ def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int)
6767

6868

6969
def open_knight_tour(n: int) -> List[List[int]]:
70-
'''
70+
"""
7171
Find the solution for the knight tour problem for a board of size n. Raises
7272
ValueError if the tour cannot be performed for the given size.
7373
@@ -78,7 +78,7 @@ def open_knight_tour(n: int) -> List[List[int]]:
7878
Traceback (most recent call last):
7979
...
8080
ValueError: Open Kight Tour cannot be performed on a board of size 2
81-
'''
81+
"""
8282

8383
board = [[0 for i in range(n)] for j in range(n)]
8484

Diff for: dynamic_programming/max_non_adjacent_sum.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
def maximum_non_adjacent_sum(nums: List[int]) -> int:
7-
'''
7+
"""
88
Find the maximum non-adjacent sum of the integers in the nums input list
99
1010
>>> print(maximum_non_adjacent_sum([1, 2, 3]))
@@ -15,14 +15,15 @@ def maximum_non_adjacent_sum(nums: List[int]) -> int:
1515
0
1616
>>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6])
1717
500
18-
'''
18+
"""
1919
if not nums:
2020
return 0
2121
max_including = nums[0]
2222
max_excluding = 0
2323
for num in nums[1:]:
2424
max_including, max_excluding = (
25-
max_excluding + num, max(max_including, max_excluding)
25+
max_excluding + num,
26+
max(max_including, max_excluding),
2627
)
2728
return max(max_excluding, max_including)
2829

Diff for: project_euler/problem_551/sol1.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ def next_term(a_i, k, i, n):
4040
ending term is a_10=62, then (61, 9) is returned.
4141
"""
4242
# ds_b - digitsum(b)
43-
ds_b = 0
44-
for j in range(k, len(a_i)):
45-
ds_b += a_i[j]
46-
c = 0
47-
for j in range(min(len(a_i), k)):
48-
c += a_i[j] * base[j]
43+
ds_b = sum(a_i[j] for j in range(k, len(a_i)))
44+
c = sum(a_i[j] * base[j] for j in range(min(len(a_i), k)))
4945

5046
diff, dn = 0, 0
5147
max_dn = n - i

0 commit comments

Comments
 (0)