I'm looking for a way to make my code more simple. This code takes a list of vectors and returns all subset of that list that sum up to another vector.
For example:
{'a','b','c'}
is the subset consisting of:a (1100000)
,b (0110000)
, andc (0011000)
.
def AddVectors(A, B):
if not A:
return B
if not B:
return A
return [A[i] + B[i] for i in range(len(A))]
def SumVectorList(lst, SuperSet):
result = []
for l in lst:
if not result:
result = SuperSet[l]
else:
for j in range(len(l)):
result = AddVectors(result, SuperSet[l[j]])
return result
def GetPowerSet(lst):
result = [[]]
for x in lst:
result.extend([subset + [x] for subset in result])
return result
S = {'a': [one, one, 0, 0, 0, 0, 0], 'b': [0, one, one, 0, 0, 0, 0],
'c': [0, 0, one, one, 0, 0, 0], 'd': [0, 0, 0, one, one, 0, 0],
'e': [0, 0, 0, 0, one, one, 0], 'f': [0, 0, 0, 0, 0, one, one]}
P = GetPowerSet(S)
u = [0, 0, one, 0, 0, one, 0]
v = [0, one, 0, 0, 0, one, 0]
u_0010010 = {y for x in P for y in x if SumVectorList(x, S) == u}
u_0100010 = {y for x in P for y in x if SumVectorList(x, S) == v}
one
by1
and it is now running fine but it doesn't seem to be doing much. \$\endgroup\$one
(and any other things we might need for testing purposes) ? \$\endgroup\$