## ← Sudoku Solver - Software Testing

• 2 Followers
• 37 Lines

### Get Embed Code x Embed video Use the following code to embed this video. See our usage guide for more details on embedding. Paste this in your document somewhere (closest to the closing body tag is preferable): ```<script type="text/javascript" src='https://amara.org/embedder-iframe'></script> ``` Paste this inside your HTML body, where you want to include the widget: ```<div class="amara-embed" data-url="http://www.youtube.com/watch?v=Fe52q8QX46s" data-team="udacity"></div> ``` 1 Language

Showing Revision 3 created 05/24/2016 by Udacity Robot.

1. Like I said in the problem, I'm going to show you a fairly naive brute-force solution to this
2. that is definitely vulnerable to some things.
3. First of, we start up with the check Sudoku function from the first problem.
4. Exact same function as the base for solving this real board.
5. After that solve Sudoku is actually fairly short if not exactly pretty or copy in sys here
6. and copy is the only needed here, sys is used down below
7. for something that is entirely for testing purposes.
8. First we check the grid then check Sudoku
9. and if check Sudoku returns none or false, we returned the result.
10. You could also assert here, I wanted to be able to continue on
11. for testing purposes so I just return the result instead of asserting that,
12. although asserting would definitely be a good idea here as well
13. and we used deep copy to copy this over
14. and not going to get into what exactly the copy package and deep copy is.
15. Surprise to say that we made a copy of the initial input into another.
16. Now, we check through the grid for zero elements
17. and we change them to each of 1 through 9 and then recursively call solve Sudoku on the results.
18. This is a very very slow brute-force process but its also fairly easy to call that as well.
19. Each element in the grid, which I put zero and then for all and in the range 1 through 10
20. so all elements 1 through 9 then we set that grid position to that number
21. and then we call solve Sudoku again.
22. If this new grid isn't false, then we return it.
23. Otherwise, we backtrack and that it's it. It's all there is too bad. It's really pretty simple.
24. There are a lot of ways you could really really mess with this if you wanted it to
25. take a very very long time for instance and right here break it into forms.
26. Just to check real quick, let's check that my solution actually works.
27. and this is for all of the inputs that we gave as example test cases.
28. You can see that it does verify that the first two,
29. the L form and the invalid ones didn't work and then for the valid one and easy,
30. we didn't do ALG simply because it would take this out
31. on a little bit longer to do that and I didn't want to wait
32. so that it shows that the valid and already solved board is the exactly the same
33. and the easy board is filled out with a valid Sudoku board solution.
34. I hope you enjoyed that and I hope you looking at everyone else's code
35. and seeing how everyone thought about the problem and how you might think
36. about testing the problem itself.
37. I hope you had a lot of fun with that. See you in the next unit.