Skip to main content
added 6 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Refactor Python Code Find all subsets of a vector that sum up to another vector

I'm looking for a way to make my code more simple. This This code takes a list of vectors and returns all subset of that list list that sum up to another vector.

For example:

{'a','b','c'}{'a','b','c'} is the subset consisting of: a (1100000)a (1100000), b (0110000)b (0110000), and c (0011000)c (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}

Refactor Python Code

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), and c (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}

Find all subsets of a vector that sum up to another vector

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), and c (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}

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:

For example, {'a','b','c'} is the subset consisting of:

a (1100000), b (0110000), and c (0011000).

{'a','b','c'} is the subset consisting of: a (1100000), b (0110000), and c (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}

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), and c (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}

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), and c (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}
Source Link
Antarr Byrd
  • 693
  • 2
  • 5
  • 16

Refactor Python Code

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), and c (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}