mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 15:01:08 +00:00
Euler problem 551 sol 1: Reduce McCabe code complexity (#2141)
* Euler problem 551 sol 1: Reduce McCabe code complexity As discussed in #2128 * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
parent
d034add61f
commit
fdc5bee7af
|
@ -4,12 +4,12 @@ from typing import List, Tuple
|
|||
|
||||
|
||||
def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
|
||||
'''
|
||||
"""
|
||||
Find all the valid positions a knight can move to from the current position.
|
||||
|
||||
>>> get_valid_pos((1, 3), 4)
|
||||
[(2, 1), (0, 1), (3, 2)]
|
||||
'''
|
||||
"""
|
||||
|
||||
y, x = position
|
||||
positions = [
|
||||
|
@ -20,7 +20,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
|
|||
(y + 2, x + 1),
|
||||
(y + 2, x - 1),
|
||||
(y - 2, x + 1),
|
||||
(y - 2, x - 1)
|
||||
(y - 2, x - 1),
|
||||
]
|
||||
permissible_positions = []
|
||||
|
||||
|
@ -33,7 +33,7 @@ def get_valid_pos(position: Tuple[int], n: int) -> List[Tuple[int]]:
|
|||
|
||||
|
||||
def is_complete(board: List[List[int]]) -> bool:
|
||||
'''
|
||||
"""
|
||||
Check if the board (matrix) has been completely filled with non-zero values.
|
||||
|
||||
>>> is_complete([[1]])
|
||||
|
@ -41,15 +41,15 @@ def is_complete(board: List[List[int]]) -> bool:
|
|||
|
||||
>>> is_complete([[1, 2], [3, 0]])
|
||||
False
|
||||
'''
|
||||
"""
|
||||
|
||||
return not any(elem == 0 for row in board for elem in row)
|
||||
|
||||
|
||||
def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int) -> bool:
|
||||
'''
|
||||
"""
|
||||
Helper function to solve knight tour problem.
|
||||
'''
|
||||
"""
|
||||
|
||||
if is_complete(board):
|
||||
return True
|
||||
|
@ -67,7 +67,7 @@ def open_knight_tour_helper(board: List[List[int]], pos: Tuple[int], curr: int)
|
|||
|
||||
|
||||
def open_knight_tour(n: int) -> List[List[int]]:
|
||||
'''
|
||||
"""
|
||||
Find the solution for the knight tour problem for a board of size n. Raises
|
||||
ValueError if the tour cannot be performed for the given size.
|
||||
|
||||
|
@ -78,7 +78,7 @@ def open_knight_tour(n: int) -> List[List[int]]:
|
|||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: Open Kight Tour cannot be performed on a board of size 2
|
||||
'''
|
||||
"""
|
||||
|
||||
board = [[0 for i in range(n)] for j in range(n)]
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ from typing import List
|
|||
|
||||
|
||||
def maximum_non_adjacent_sum(nums: List[int]) -> int:
|
||||
'''
|
||||
"""
|
||||
Find the maximum non-adjacent sum of the integers in the nums input list
|
||||
|
||||
>>> print(maximum_non_adjacent_sum([1, 2, 3]))
|
||||
|
@ -15,14 +15,15 @@ def maximum_non_adjacent_sum(nums: List[int]) -> int:
|
|||
0
|
||||
>>> maximum_non_adjacent_sum([499, 500, -3, -7, -2, -2, -6])
|
||||
500
|
||||
'''
|
||||
"""
|
||||
if not nums:
|
||||
return 0
|
||||
max_including = nums[0]
|
||||
max_excluding = 0
|
||||
for num in nums[1:]:
|
||||
max_including, max_excluding = (
|
||||
max_excluding + num, max(max_including, max_excluding)
|
||||
max_excluding + num,
|
||||
max(max_including, max_excluding),
|
||||
)
|
||||
return max(max_excluding, max_including)
|
||||
|
||||
|
|
|
@ -40,12 +40,8 @@ def next_term(a_i, k, i, n):
|
|||
ending term is a_10=62, then (61, 9) is returned.
|
||||
"""
|
||||
# ds_b - digitsum(b)
|
||||
ds_b = 0
|
||||
for j in range(k, len(a_i)):
|
||||
ds_b += a_i[j]
|
||||
c = 0
|
||||
for j in range(min(len(a_i), k)):
|
||||
c += a_i[j] * base[j]
|
||||
ds_b = sum(a_i[j] for j in range(k, len(a_i)))
|
||||
c = sum(a_i[j] * base[j] for j in range(min(len(a_i), k)))
|
||||
|
||||
diff, dn = 0, 0
|
||||
max_dn = n - i
|
||||
|
|
Loading…
Reference in New Issue
Block a user