def partition_iterative(li, f):
a, b = [], []
for x in li:
if f(x):
a.append(x)
else:
b.append(x)
return a, b
def partition_recursive(li, f):
def helper(li, f, a=(), b=()):
if len(li) == 0: return a, b
x, *xs = li
if f(x):
return helper(xs, f, (*a, x), b)
return helper(xs, f, a, (*b, x))
return helper(li, f)
li = (1,2,3,4,5,6)
odds, evens = partition_iterative(li, lambda x: x % 2 == 1)
print(odds, evens)
odds, evens = partition_recursive(li, lambda x: x % 2 == 1)
print(odds, evens)
To embed this project on your website, copy the following code and paste it into your website's HTML: