Optimize the following code: import itertools as it def powerset(lst): """Return the…
- User
Optimize the following code:
import itertools as it
def powerset(lst): """Return the powerset of a list.""" n = len(lst) powerset = [] for i in range(1 << n): subset = [lst[j] for j in range(n) if (i & (1 << j))] powerset.append(subset) return powerset
def chain(graph, tail=[[0]]): """ (co)Recursively construct the chain for a graph. """ acceptable = set() blacklist = set()
for node in tail[-1]: options = graph[node] for target in options: if target in acceptable: blacklist.add(target) acceptable.remove(target) elif target in blacklist: pass else: acceptable.add(target) acceptable = list(acceptable) if not acceptable: return tail if acceptable in tail: return tail + [acceptable] else: return chain(graph, tail=tail + [acceptable])
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.