Bread
Bread

Reputation: 135

Unable to update dictionary

I am facing this problem while trying to code a Sudoku solver (with some parts referencing to http://norvig.com/sudoku.html)

Here's the code I have made so far with reference to the above URL.

puzzle = '003198070890370600007004893030087009079000380508039040726940508905800000380756900'
cell = '123456789'
cell_break = ['123','456','789']


def generate_keys(A, B):
  "Cross product of elements in A and elements in B."
  return [a+b for a in A for b in B]

#print generate_keys(cell,cell)

def dict_puzzle(puzzle,cell):
  'Making a dictionary to store the key and values of the puzzle'
  trans_puzzle = {}
  key_list = generate_keys(cell,cell)
  i=0
  for x in puzzle:
    trans_puzzle[str(key_list[i])] = x
    i = i + 1
    return trans_puzzle

dict_puzzle(puzzle,cell)['11'] = 'die'
print dict_puzzle(puzzle,cell)['11']

for the last 2 lines of the code, I tried to mutate the dictionary but to no avail. It just returns me 0, which is the original value. (i.e the mutation wasnt successful)

I am not sure why is this happening :(

Upvotes: 1

Views: 44

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 600059

You're calling the function again, so it returns a new dictionary. You need to assign the result of the first call to a variable and mutate that.

result = dict_puzzle(puzzle,cell)
result['11'] = 'die'
print(result)

Upvotes: 1

Related Questions