2 points · by varrakesh · 13y ago · 1 comment
def subset_sum(A, target): T = {0} for i in A: T |= {x + i for x in T} return target in T
Seems to be pretty efficient.
>>> {1,2,3} | {2,4,6}
set([1, 2, 3, 4, 6])
By the way, AND-ing would be a straight intersection >>> {1,2,3} & {2,4,6}
set([2])