-9

I have this Python code used by Peter Norvig in his Sudoku solver. I don't understand why assign should do anything at all to 'values' because because no where in the code does 'values' get updated and 'values' is only used in the if conditional statement. Could you please explain, thanks in advance!

def assign(values, s, d):
    """Eliminate all the other values (except d) from values[s] and 
propagate.
     Return values, except return False if a contradiction is 
detected."""
    other_values = values[s].replace(d, '')
    if all(eliminate(values, s, d2) for d2 in other_values):
        return values
    else:
        return False


 def eliminate(values,s,d):
    '''Eliminate d from values[s]; propagate when values or places <=2.
Return values, except return False if a contradiction is detected.'''
    if d not in values[s]:
        return values ## Already eliminated
    values[s] = values[s].replace(d,'')

    if len(values[s]) == 0:
        return False ##Contradiction: removed last value
    elif len(values[s]) == 1:
        d2 = values[s]
        if not all(eliminate(values, s2, d2) for s2 in peers[s]):
            return False

    for u in units[s]:
        dplaces = [s for s in u if d in values[s]]
        if len(dplaces) == 0:
            return False ## Contradiction: no
        elif len(dplaces) == 1:
            # d can only be in one place in unit; assign it there
            if not assign(values,dplaces[0],d):
                return False
    return values
wdc
  • 99
  • 3
  • What exactly don't you understand? – DeepSpace Jul 19 '17 at 13:30
  • What does `eliminate` do? What does `replace` do? – Scott Hunter Jul 19 '17 at 13:31
  • 1
    why should values be different after calling the function? – wdc Jul 19 '17 at 13:32
  • 1
    What is the overall code, we can't tell you the use of a function without knowing in what circumstances it will be used. And it tells you what the function does in the comments – Professor_Joykill Jul 19 '17 at 13:32
  • Okay, I've included the eliminated function – wdc Jul 19 '17 at 13:38
  • I don't understand why so many downvotes... :(, I still don't know what the answer is... – wdc Jul 19 '17 at 13:53
  • 1
    @PythonCPPMATLAB: I expect you're getting down-voted for lack of required research. Simple debugging practice, even a couple of strategically-place `print` statements, would track down exactly where and how `values` gets updated. See this lovely [debug](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) blog for help. – Prune Jul 19 '17 at 15:48

1 Answers1

0

values is updated as a side effect of eliminate:

values[s] = values[s].replace(d,'')

This removes all values d from the s entry of values. See replace.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Thank you! Also for future reference: https://stackoverflow.com/questions/44738949/recursion-help-peter-norvigs-sudoku-exercise asks about the same thing and the answer is also good there (should have checked before posting my question). – wdc Jul 20 '17 at 14:14