0

I want to make some king of bruteforcer where we have treee inputs and each of them has many propositions:

e  = ['B[key-mini] = B[key-mini] +1','A[j] = key', 'A[key] = B[key-mini]', 'B[key-mini] = A[key-mini]','B[key]=B[key]-1','B[j]=B[j]+1','B[key+mini] = key', 'B[i] = key']
f  = ['num = maxi -mini +1', 'num = maxi', 'num = mini','num = num +1','num =B[j-mini]','num =B[-mini]','num =A[j-mini]','num =A[i]']
g  = ['A[i] =num','A[i] =j','B[j] =num','A[i] =B[j]','A[i] =num+j','A[j] =i','B[i] =i','B[j] =A[i]','B[i] =A[i]']

the objective is to moddify A = [1,-7,4,3,5,1,4,0,4] and get at the end A = [-7,0,1,1,3,4,4,4,5]

this is the code:

def main(l1,l2,l3):
    for o in l1:
        for p in l2:
            for q in l3:
                if 'A' in list(globals()): del globals()["A"]
                if 'B' in list(globals()): del globals()["B"]
                if 'num' in list(globals()): del globals()["num"]
                globals()['A']=[1,-7,4,3,5,1,4,0,4]
                
                
                #---------------------------------------------------
                maxi = max(A)
                mini = min(A)
                B = [0] * (maxi-mini+1)
                for j in range(mini,maxi,+1): B[j-mini] = 0
                
                for i in range(len(A)):
                    key=A[i]
                    
                    exec(o) # executing a command from the ¨e¨ list
                

                i = 0
                for j in range(mini,maxi):
                    num = 0
                    exec(p) # executing a command from the ¨f¨ list
                    
                    while num > 0:
                        exec(q) # executing a command from the ¨g¨ list
                        
                        i = i+1
                        num = num -1
                print('rep1 =',o,'rep2 =',p,'rep3 =',q)
                print(A)
main(e,f,g)

my problem is when I try to print A at the end, it doesn't change the list even tho the code modifies it

MyTricker
  • 11
  • 3
  • Why don't you use exec and eval? – FLAK-ZOSO Aug 17 '22 at 11:25
  • I use them look closely – MyTricker Aug 17 '22 at 11:47
  • The problem is that `num` is a local variable and thus it cannot be updated via `exec`. An alternative solution is suggested [here](https://stackoverflow.com/a/32828675/3767239). Basically, you define a custom class `StateMachine` and then `main` is defined as one of its methods. This requires you to change every mention of the relevant (to-be-updated) names `A, B, num` and prefix them with `self.` (i.e. these variables define the current state). Then you would have for example `self.A[i] = self.num` as the first element of the `g` list. – a_guest Aug 17 '22 at 13:22
  • Or, you could declare all relevant names, i.e. `A, B, num`, as global via `global A, B, num` at the beginning of the function definition. Then you also need to prefix every command in the lists with `global A, B, num; ` to mark these variables global explicitly, For example: `q = f'global A, B, num; {q}'`. – a_guest Aug 17 '22 at 13:33

0 Answers0