Sorry that this is vague as I am not sure what I want to do can be done on python.

If I have a system whereby I am trying to fill a grid but my current system can’t fully complete it

Is there a way I can save a copy of the system, try putting in one number and seeing if that works

then if it doesnt, go back to the original system and see if the other number does work

That is very vague. Given python has if/elif/else and try/except, this sounds possible on paper, but your details are vague

Recursion fits that behaviour

Make the change and call self, abort if it doesn’t work, undo, do next number

```
def search(system):
if invalid:
return None
if finished:
return system
for nextnum in range(10):
system.changewith(nextnum)
result = search(system)
if result:
return result
system.undo(nextnum)
return None
```

Note that pruning has to be very aggressive as otherwise this has exponential growth

And also it’ll hit recursion limit when it gets a few thousand steps deep, in which case you’d need a stack (like a regular list) to remember the numbers in the loops

1 Like