Link to home
Start Free TrialLog in
Avatar of merijn van der leek
merijn van der leek

asked on

Making a sudoku

Hey i need to print a sudoku for school but i am having a hard time doing so.

So i made a load function which prints a pre loaded sudoku like this:

['7,9,0,0,0,0,3,0,1',
 '0,0,0,0,0,6,9,0,0',
 '8,0,0,0,3,0,0,7,6',
 '0,0,0,0,0,5,0,0,2',
 '0,0,5,4,1,8,7,0,0',
 '4,0,0,7,0,0,0,0,0',
 '6,1,0,0,9,0,0,0,8',
 '0,0,2,3,0,0,0,0,0',
 '0,0,9,0,0,0,0,5,4']

For the new function i need to make the function show, which makes the sudoku look like this:

>>> from solver import load, show
>>> show(load("easy/puzzle1.sudoku"))
7 9 _   _ _ _   3 _ 1
_ _ _   _ _ 6   9 _ _
8 _ _   _ 3 _   _ 7 6

_ _ _   _ _ 5   _ _ 2
_ _ 5   4 1 8   7 _ _
4 _ _   7 _ _   _ _ _

6 1 _   _ 9 _   _ _ 8
_ _ 2   3 _ _   _ _ _
_ _ 9   _ _ _   _ 5 4

This is the code for the load function:

def load(filename):
    with open(filename) as sudoku_original:
        sudoku_original = sudoku_original.readlines()
        
        sudoku_original = [line.rstrip('\n') for line in sudoku_original]
        
        sudoku = []
        for line in sudoku_original:
            sudoku.append(line)        
        return sudoku

Open in new window


And this is the format of the show function:
def show(sudoku):
    # TODO
    pass

Open in new window

Avatar of Norie
Norie

Try this.
grid =  ['7,9,0,0,0,0,3,0,1',
 '0,0,0,0,0,6,9,0,0',
 '8,0,0,0,3,0,0,7,6',
 '0,0,0,0,0,5,0,0,2',
 '0,0,5,4,1,8,7,0,0',
 '4,0,0,7,0,0,0,0,0',
 '6,1,0,0,9,0,0,0,8',
 '0,0,2,3,0,0,0,0,0',
 '0,0,9,0,0,0,0,5,4']

def show(sudoku):
    for line in sudoku :
        print(line.replace('0','_').replace(',',' '))

show(grid)

Open in new window

Avatar of merijn van der leek

ASKER

Thanks so much thats a really good solution. Do u also know how to get the whitespace around the blocks of 3 by 3?
Just passing by, feel free to ignore this if it's not useful, but I would have expected the internal representation of the puzzle to be a 2 dimensional array of integers, not a one dimensional array of comma delimited strings?  Seems like it would be a lot easier to work with and would be the data structure I'd have used if I was building the game from ground up.  Then your load procedure parses the input text lines and loads the values as numbers into the array to make it easier to manipulate later.

But you may have been given this constraint already...


»bp
Hey good input what do i have to change in the load function to achieve this? Thanks for the feedback!
Welcome, I'm not a python jock, so I'll leave that to Norie if they are up for it, I know they can knock it out easier than I could piece together the appropriate python since it's not a language I work in normally.


»bp
Oh okay i get that thanks anyways :)
merijn

If you upload a sample file then I could post something for an alternative load function
Hey since the files are all within a site i am going the copy puzzle1 from easy:
7,9,0,0,0,0,3,0,1
0,0,0,0,0,6,9,0,0
8,0,0,0,3,0,0,7,6
0,0,0,0,0,5,0,0,2
0,0,5,4,1,8,7,0,0
4,0,0,7,0,0,0,0,0
6,1,0,0,9,0,0,0,8
0,0,2,3,0,0,0,0,0
0,0,9,0,0,0,0,5,4
That is how the file looks
Load & Show function using a 2d grid.

game = [
[ 7,9,0,0,0,0,3,0,1],
[ 0,0,0,0,0,6,9,0,0],
[ 8,0,0,0,3,0,0,7,6],
[ 0,0,0,0,0,5,0,0,2],
[ 0,0,5,4,1,8,7,0,0],
[ 4,0,0,7,0,0,0,0,0],
[ 6,1,0,0,9,0,0,0,8],
[ 0,0,2,3,0,0,0,0,0],
[ 0,0,9,0,0,0,0,5,4]]

def show(grid):
  row = 0
  print "|-------+-------+-------|"
  for rows in grid:
      cnt = 0
      buffer = '|'
      for cell in rows:
          if cell == 0 or cell =='0':
              buffer = buffer + " _"
          else:
              buffer = buffer + " {}".format(cell)
          cnt = cnt + 1
          if cnt % 3 == 0:
              buffer = buffer + " |"
      print (buffer);
      row = row + 1
      if row % 3 == 0:
          print "|-------+-------+-------|\n"

def load(file):
    grid = []
    with open(file,"r") as sudoku:
        for line in sudoku:
            line = line.rstrip("\n").replace(" ","")
            row = line.split(',')
            grid.append(row)
    return grid

show(game)
show(load("TheSudokuFile"))

Open in new window

Hey that is some good code! The problem is i just started with python so i dont really understand it. Is there any way to make that more for beginners?
ASKER CERTIFIED SOLUTION
Avatar of noci
noci

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ahh thanks so much that helps alot!
See also: https://docs.python.org/3/
for more info on Python. (All versions, they can be selected on top left).