Checking the Boxes

Using python to code a sudoku validating program.
Daniel Bell
Grade 8

Problem

Sudoku puzzles are hard to solve. Once you have a completed puzzle, how do you know it is correct?

In order to start coding Python, my brother suggested that I use a Python Editor. Editors let programmers create, change, edit, open, and view plain text files. I then created an account on Replit and made a Python project. Replit is an Editor website, known as an online integrated development environment (IDE). I needed to learn some basics of the language, so I completed online tutorials and wrote sample code in Replit. I had to come up with a set of instructions to make the program be able to validate a Sudoku puzzle. This set of instructions is known as an algorithm.

Method

I learned the computer programming language Python. I then created an algorithm and coded it to validate an inputted Sudoku puzzle.

My first step was to learn Python (a computer programming language). I started researching Python training websites because I prefer to learn by watching videos. Once I found good videos, I watched them and completed many sets of tutorials. Before I started coding, I installed Python and the corresponding Editor (Replit) on my computer.

I experimented with Python by writing small, simple programs. This gave me confidence about programming techniques while learning how the Editor works. I made mistakes and then I learned from those mistakes.  I started by learning how to make Python accept inputs and display outputs, Then I continued to work my way through other tutorials.

The next step was to build an algorithm with text that could be followed to verify a Sudoku puzzle solution. An algorithm is a series of steps that you follow to complete a task. I started by writing a simple algorithm that didn't have much detail and worked my way up until I had a much more complicated algorithm that provided me with the steps necessary to validate a Sudoku puzzle.

I then implemented the algorithm by writing the steps as computer code with Python. This meant writing Python code that performed the steps as outlined in the algorithm.  Once I had a working program, I tested the program to make sure it ran and performed as expected. I tested the program by inputting valid and invalid puzzles to see how the program reacted. As errors were found, I reviewed the code and debugged any mistakes that I made. Once I had a working program I reviewed my code to see if there was any way that I could make the program easier to read by using fewer lines of code and organizing it better.

The code that I used in my project is shown below.

 

Analysis

The following images show the output of my program with an invalid puzzle, a valid puzzle, and invalid input lines.

This project taught me a lot about programming. I had to learn lots of key concepts, including:

  • Print
  • Input
  • Strings
  • Comparison operators
  • Logical operators
  • Comments
  • If statements
  • Convert strings to numbers
  • While loops
  • Lists
  • List methods
  • For loops
  • Ranges
  • Tuples
  • Variables
  • Math operators

I also learned the importance of using an editor that is specifically designed for Python. The editor is important because it helped me identify coding problems. I now have a new appreciation for how difficult and complex it is to be a professional computer programmer.

Conclusion

I now have a good understanding of many of Python’s basic concepts.  After writing several versions that did not work, I managed to write a program that functioned properly and accurately validated Sudoku puzzle solutions!  There are definitely some improvements I could make in future versions of my program, which would be more complicated and take additional time. 

My program is useful to lots of people because anyone can input their Sudoku puzzle and have the program check if it is valid.  My program was not designed to solve the Sudoku puzzle for people, but instead to tell them how many errors they have in the solutions they have created, in order to set them up to continue to enjoy and solve the puzzle.

There were many challenges that came up while I was making the program. Examples include:

  • While coding in Python, there is a difference between ‘=’ and “==” which I forgot about many times. A single ‘=’ is used to assign a value to something, but a “==” is used to check if an equation is true. I mixed these symbols up several times, resulting in many error messages!
  • I made many mistakes when I was trying to get my program to validate a list of numbers. This was the key to my program working, but it was extremely complicated for me to code this.  I had to try again and again.
  • Another challenge was forgetting to use square brackets [ ] and instead using round brackets ( ) when creating lists.
  • I also had trouble learning how to use functions since they can be quite complicated. The concept of parameters, which is what the function needs to work, was a difficult one to understand and use consistently.
  • Python uses indenting to define nested code.  This concept caught me on many occasions!
  • Another challenge coders face is knowing when to stop.  My program could always be improved to work faster, look nicer or add additional features.  Due to the complexity and some constraints of Python, I had to stop when I had a basic working program which validated 9x9 Sudoku puzzles.

While I developed a working program, there are several things that I could have added if I had more time and a better understanding of Python. These include:

  • Highlighting the errors in red
    • I counted the number of errors in a row, column, and smaller box. Highlighting the errors would require much more coding to both identify and display the problem numbers. This would require me to build another algorithm.
    • Trying to figure out exactly where you have gone wrong in a completed but incorrect Sudoku is difficult so this would be the next thing that I would look at. In my next version, I would have the program show exactly which digit is incorrect.
  • Provide a better way to input the puzzle
    • An example of this would be providing a empty Sudoku grid to input the numbers into. Adding something like this would require some extra functionality that is outside the limits of Python.
  • More efficient / less lines of code
    • If I could have spent more time learning Python, then I would have been able to look at my code and figure out ways to shorten it.

 

Citations

 

 

Bell, D. (2024). Grade 8 science fair sudoku solver. Replit. https://replit.com/@danielmbell7/grade-8-science-fair-sudoku-solver#main.py 

Hamedani, M. (2018, November 6). Python functions | python tutorial for absolute beginners #1. YouTube. https://www.youtube.com/watch?v=u-OmVr_fT4s&ab_channel=ProgrammingwithMosh

Hamedani, M. (2020, September 16). Python for beginners - learn python in 1 hour. YouTube. https://www.youtube.com/watch?v=kqtD5dpn9C8&ab_channel=ProgrammingwithMosh

Institute, O. P. (2020). Python® – the language of today and Tomorrow. About Python. https://pythoninstitute.org/about-python#:~:text=Python%20was%20created%20by%20Guido,called%20Monty%20Python%27s%20Flying%20Circus

Newlyn-Jones, B. (2018). Original sudoku. Igloo Books Limited.

Replit, R. (2016). The software creation platform. Ide, AI, and deployments. https://replit.com/

Saha, S. (2023, October 16). Replit’s mad obsession with ’ai for all’ . Analytics India Magazine. https://analyticsindiamag.com/replits-mad-obsession-with-ai-for-all/

Staff, C. (2023, November 20). What is python used for? A beginner’s guide. What Is Python Used For? A Beginners Guide. https://www.coursera.org/articles/what-is-python-used-for-a-beginners-guide-to-using-python

Tsuneoka, C. (2021, August 20). Sudoku’s godfather, Maki Kaji, made a business out of play. https://www.wsj.com/articles/sudokus-godfather-maki-kaji-made-a-business-out-of-play-11629468002

Venners, B. (2003, January 13). ABC’s influence on Python. artima - The Making of Python. https://www.artima.com/articles/the-making-of-python

Acknowledgement

I'd like to thank my parents for all their love and support. My dad helped me with the programming and the technical details. My mom helped with the testing and the creation of the trifold.