Advanced · Recursion · 167 of 264
Fair split
Can the numbers be split into two groups with the same sum? Every number must go into one of the groups.
Input: up to 18 positive whole numbers on one line. Output: yes or no
Examples
Input
1 5 11 5
Output
yes
Input
1 2 3 5
Output
no
Input
2 2
Output
yes
2 more tests check other inputs when you press Check.
Stuck? The idea behind recursion
A recursive function calls itself on a smaller version of the problem, until it reaches a case small enough to answer directly (the base case).
Trust the smaller call to be right, and only work out how to build this answer from it.
def factorial(n):
if n == 0:
return 1
return n * factorial(n - 1)
print(factorial(5))Watch for: Without a base case, or if the problem doesn't get smaller, it never stops: Python gives up with a RecursionError.