Open tlienart opened 5 years ago
interview question perm:
def perm(l: list):
m = len(l)
if m == 2:
return [[l[0], l[1]], [l[1], l[0]]]
else:
r = []
for i in range(m):
p = perm(l[:i] + l[i+1:])
for j in range(len(p)):
r += [[l[i]] + p[j]]
return r
might be improvable (if anything it should be possible just to write the numbers and have basically no memory requirement, another thing is that we end up calling perm for a list of size (m) multiple times as opposed to using the permutation multiple times which would be more efficient maybe.
optim
stats/ml