Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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}
share|improve this question
    
What does your code do ? What is it supposed to do ? Does it work ? I've tried replacing one by 1 and it is now running fine but it doesn't seem to be doing much. –  Josay Jul 25 '13 at 19:43
    
one should not be replace with 1. The one in this case is 1 in GF2. So one+one=0 where as 1+1 in R = 2 –  Antarr Byrd Jul 25 '13 at 19:48
    
@Josay As stated about I'm trying to find subset of S that when summed over GF2 you get the value of u or v respectively. I left out the GF2 portion, sorry. The code works just fine. I just was wondering how I could make it cleaner and with less lines maybe. –  Antarr Byrd Jul 25 '13 at 19:50
    
Thanks for the clarification. That makes sense now. Can you give the definition of one (and any other things we might need for testing purposes) ? –  Josay Jul 25 '13 at 20:00
    
@Josay you can go to resources.codingthematrix.com and Download GF2.py –  Antarr Byrd Jul 25 '13 at 20:38
add comment

1 Answer

up vote 1 down vote accepted

Please have a look at PEP 8 giving the Style Guide for Python Code.

Now, in AddVectors (or add_vectors), it seems like you assume that A and B have the same size. You could achieve the same result with zip.

def add_vectors(a, b):
    assert(len(a)==len(b))
    return [sum(i) for i in zip(a,b)]

I have to go, I'll some more later :-)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.