Sudoku Python generator Watch
i am just trying to attempt with this extension task that was just given by my tutor to try but i am not sure how to tackle this
attachment is attached can someone help me so in future i'll be ready to do these when we learn about this. Its not like a class task its just for us to try so i really wanna try this.
The first thing to do is to choose a sensible data structure (e.g. a list) to store your numbers in. You could store it all in a single list, or you could store it in a "9x9" list-of-lists, whichever you feel is easier to visualise and work with. ). You should only need to read it once at the beginning of your program, then keep it in memory.
Next, solve the problem of reading the file into that "shape" (e.g. a list of 81 items, or list-of-9x9-lists). Have a search on Google for how to read numbers from a file into a list using Python - you will find loads of examples on StackOverflow. You might even find some which actually use sudokus as an example.
Next, consider writing separate functions which tackle each of the 'rules' of a sudoku. For example
- Take a 3x3 "grid" and check that each number from 1-9 appears exactly once (Remember a 3x3 grid could also be represented in Python as a simple list of 9 numbers.. your python data structure does not need to look the same as the physical real-world structure. Again, simple is usually better than complex)
- Take a "row" of 9 numbers and check that each number from 1-9 appears exactly once
- Take a "column" of 9 numbers and check that each number from 1-9 appears exactly once
(If you're paying attention, you might see that all 3 of these problems are actually the same programming problem, albeit phrased/framed in 3 different ways - but just because they mean different things in plain English doesn't mean the code for them needs to be different).
Consider how to pick a subset of numbers from the stored sudoku data - e.g. how would you just grab a 'row' of 9 numbers? or a 'column' of 9 numbers? or one of the '3x3 grids' (which is also 9 numbers..) -- it may be easier to solve everything in terms of those individual subsets.
Remember with programming, if you can solve a problem once, then you can repeat that solution as many times as you like, so always solve the simple case first without repetition - e.g. if you can solve for one row, then you already have the solution for all rows.
Lastly, do some Google research to see what kind of ideas other people have done - try searching something along the lines of "Python sudoku checker"