Today my CPSC professor assigned a quiz in python, the topic was recursion.
The whole class got stuck on question number two, which is below. Nobody was able to even come close to a solution.
def sub_set(A):
if A == []: return A
X = sub_set(A[1:])
result = []
for L in X:
result += _____
return _____
Example code:
print(sub_set([1, 2])) # [[], [1], [2], [1, 2]]
print(sub_set([1, 2, 3])) # [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
You are only allowed to modify the underscores, such as my example below may demonstrate.
My solution was so far from close, it really shouldn't even be considered:
def sub_set(A):
if A == []: return A
X = sub_set(A[1:])
result = []
for L in X:
result += _____
return result + [A[:1]] + [A] + [A[::2]]
#sub_set([1, 2, 3]) -> [[3], [3], [3], [2], [2, 3], [2], [1], [1, 2, 3], [1, 3]]
Does anyone know how this could be solved? It seems like quite a challenging problem when we have only 15 minutes to solve it.
Just for reference, he said he would drop the question considering nobody in the class - advanced comp sci class of about 10 carefully selected students - could solve it.