MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solve

Function solve

project_euler/problem_122/sol1.py:40–59  ·  view source on GitHub ↗

Checks if nums can have a sum equal to goal, given that length of nums does not exceed depth. >>> solve([1], 2, 2) True >>> solve([1], 2, 0) False

(nums: list[int], goal: int, depth: int)

Source from the content-addressed store, hash-verified

38
39
40def solve(nums: list[int], goal: int, depth: int) -> bool:
41 """
42 Checks if nums can have a sum equal to goal, given that length of nums does
43 not exceed depth.
44
45 >>> solve([1], 2, 2)
46 True
47 >>> solve([1], 2, 0)
48 False
49 """
50 if len(nums) > depth:
51 return False
52 for el in nums:
53 if el + nums[-1] == goal:
54 return True
55 nums.append(el + nums[-1])
56 if solve(nums=nums, goal=goal, depth=depth):
57 return True
58 del nums[-1]
59 return False
60
61
62def solution(n: int = 200) -> int:

Callers 1

solutionFunction · 0.70

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected