I have a task that has to use brute force to solve.
The data is in a python dictionary where one value from each key is used to get correctly answer a sum is part of a wider solution so in that context there is only one solution but in the example, I am giving there is a possibility for multiple solutions I suppose. So let's just assume that the first solution is the correct one.
So for example say the target is 9036 The "rules" are that all we can do is addition and have to take one element from each list within the dictionary.
Therefore 9036 can be calculated from the dictionary below as x:9040,y:1247,w:242,z:-1493
I have been trying to achieve this via a loop but I cant get the code to "loop" how I want it to which is along the lines of iterating over x[0],y[0],w[0],z[0]
where 0 is just the first element in the list in the first itteration and then doing x[0],y[1],w[0],z[0], x[0],y[1],w[1],z[0], x[0],y[1],w[1],z[1]
... until it has solved the problem.
I have not added any code as I was simply running a loop that never got anywhere near what I needed it to do and as I have no real experience with these sorts of algorithms / brute-forcing I was hoping someone could help point me in the right direction.
EDIT::: I have added the dictionary so that a solution can be provided but the dictionary size can vary so it needs to be a dynamic solution.
The dictionary:
{'x': [11909.0, 9040.0], 'y': [4345.0, 1807.0, 1247.0, 0.0, 6152.0, 4222.0, 123.0], 'w': [538.0, 12.0, 526.0, 0.0, 242.0, 1.0, 128.0, 155.0], 'z': [7149.0, 3003.0, 4146.0, 3054.0, 0.0, -51.0, 1010.0, 189.0, 182.0, -1493.0, 5409.0, -1151.0]}
CodePudding user response:
With inputs from https://stackoverflow.com/a/61335465/14066512 to iterate over dictionary. permutations_dicts variable contains all the different types of "brute force" combinations
import itertools
keys, values = zip(*d.items())
permutations_dicts = [dict(zip(keys, v)) for v in itertools.product(*values)]
for i in permutations_dicts:
if sum(i.values())==9036:
print(i)
break
{'x': 9040.0, 'y': 1247.0, 'w': 242.0, 'z': -1493.0}
CodePudding user response:
From what I understood you want to try all combinations of values for each key until you reach the right answer.
This will add all possible values for each key until it finds 9036:
my_dict = {'x': [11909.0, 9040.0], 'y': [4345.0, 1807.0, 1247.0, 0.0, 6152.0, 4222.0, 123.0], 'w': [538.0, 12.0, 526.0, 0.0, 242.0, 1.0, 128.0, 155.0], 'z': [7149.0, 3003.0, 4146.0, 3054.0, 0.0, -51.0, 1010.0, 189.0, 182.0, -1493.0, 5409.0, -1151.0]}
looping = True
for x in range(len(my_dict['x'])):
if not looping:
break
for y in range(len(my_dict['y'])):
if not looping:
break
for w in range(len(my_dict['w'])):
if not looping:
break
for z in range(len(my_dict['z'])):
addition = my_dict['x'][x] my_dict['y'][y] my_dict['w'][w] my_dict['z'][z]
if addition == 9036:
new_dict = {'x':my_dict['x'][x], 'y':my_dict['y'][y], 'w':my_dict['w'][w], 'z':my_dict['z'][z]}
print(f"Correct answer is {new_dict}")
looping = False
break