Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Post on 28-Mar-2015
215 Views
Preview:
Transcript
Computing Science 1P
Large Group Tutorial 19
Simon GayDepartment of Computing Science
University of Glasgow
2006/07
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 2
Remember
No lecture on Friday this week.
After the Easter break, Monday 9th April is also a holiday –no labs that day – go to another lab that week if you needhelp.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 3
Example: Sudoku
You are probably familiar with sudoku puzzles:
Each row, column and 3x3 square must be filled in to containthe digits 1 – 9.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 4
Data Structure for Sudoku
The first step in designing a program to work with sudokupuzzles is to choose a data structure.
We have the usual options…
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 5
What kind of data structure?
• Based on lists• Based on dictionaries• Something else• Don't know
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 6
Data Structure for Sudoku
A sudoku grid is basically a 9x9 matrix, so the natural datastructure is a list of lists. (Could use a dictionary, but little point.)
We can either use a list of 9 rows, or a list of 9 columns.Each row or column is itself a list of 9 numbers.
It doesn't matter whether we choose rows or columns, as longas we remember which it is.
Whatever we do, dealing with the 3x3 squares will be a littleawkward.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 7
Sudoku as a list of rows[ [0,0,3,0,0,9,4,6,0], [0,0,6,0,0,0,1,0,0], [0,0,0,6,3,2,0,0,0], [5,0,0,0,0,1,0,0,2], [0,2,4,0,0,0,6,8,0], [8,0,0,2,0,0,0,0,7], [0,0,0,5,4,7,0,0,0], [0,0,2,0,0,0,8,0,0], [0,4,5,1,0,0,9,0,0] ]
0 represents an empty square.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 8
Checking a sudoku grid
A key operation in any implementation of sudoku is checkingwhether or not a particular grid is a solved puzzle.
A grid is a solved puzzle if all of the following are true:- each row contains the digits 1 – 9- each column contains the digits 1 – 9 - each 3x3 square contains the digits 1 – 9
Assuming that the whole grid is the correct size, theseconditions obviously also guarantee that the digits 1 – 9 appear once each in every row, column and 3x3 square.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 9
Question for discussion
Can you think of a useful function to define, in order toimplement a test for a solved puzzle?
A grid is a solved puzzle if all of the following are true:- each row contains the digits 1 – 9- each column contains the digits 1 – 9 - each 3x3 square contains the digits 1 – 9
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 10
A useful function
Are you thinking what I'm thinking?
My idea is to define a function check which, given a list of numbers, checks whether or not the list contains the numbers 1 – 9 exactly once each.
Exercise (discussion): how would you define check ?
First think of an idea, then think about how to implement theidea. Write a Python definition if you have time.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 11
check – idea 1
Take advantage of the fact that Python provides a sort methodfor lists. If we sort a list, we can then easily check whether itcontains the numbers 1 – 9.
def check(x): x.sort() # into increasing numerical order return x == [1,2,3,4,5,6,7,8,9]
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 12
Can you see any problems with check ?
• Yes• No• Don't know
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 13
check – idea 1
The problem is that x.sort() modifies x and this changepersists after the function (see Lecture 16).
To use this function we would have to be very careful aboutwhat we give it as a parameter.
A safe alternative:
def check(x): y = x + [] # creates a new list y.sort() # into increasing numerical order return y == [1,2,3,4,5,6,7,8,9]
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 14
check – idea 2
Another idea is to check that the list contains 1, check that itcontains 2, and so on; if all of these are true, then return true.
This suggests another function:
def contains(x,v): # return True if v occurs in x, # return False otherwise
Exercise (discussion): define contains
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 15
contains
def contains(x,v): for a in x: if a == v: return True return False
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 16
check using contains
def check(x): i = 1 result = True while i <= 9: if not(contains(x,i)): result = False i = i + 1 return result
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 17
check using contains : alternative
def check(x): i = 1 while i <= 9: if not(contains(x,i)): return False i = i + 1 return True
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 18
check using contains : alternative 2
def check(x): for i in range(1,10): if not(contains(x,i)): return False return True
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 19
Have we forgotten anything?
• Yes• No• Don't know
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 20
check using contains
Our definitions will also return True when called with the list
[1,2,3,4,5,6,7,8,9,1]
We have a choice:
1. Note carefully that check only works when its parameterhas length 9, and make sure we call it properly
2. Modify the definition: def check(x): if len(x) != 9: return False for i in range(1,10): if not(contains(x,i)): return False return True
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 21
Checking a sudoku grid
Now that we have the function check, how do we use it tocheck a whole grid?
We need to check the rows, the columns, and the 3x3 squares.In each case we can extract the relevant values from the grid.
So, let's define functions getRow, getCol, getSquare
Recall that the grid is represented by a list of rows.
def getRow(grid,row): return grid[row]
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 22
Exercise (discussion)
[ [0,0,3,0,0,9,4,6,0], [0,0,6,0,0,0,1,0,0], [0,0,0,6,3,2,0,0,0], [5,0,0,0,0,1,0,0,2], [0,2,4,0,0,0,6,8,0], [8,0,0,2,0,0,0,0,7], [0,0,0,5,4,7,0,0,0], [0,0,2,0,0,0,8,0,0], [0,4,5,1,0,0,9,0,0] ]
Define getCol(grid,col)
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 23
getCol
def getCol(grid,col): c = [] for i in range(9): c = c + [grid[i][col]] return c
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 24
Exercise (discussion)
[ [0,0,3,0,0,9,4,6,0], [0,0,6,0,0,0,1,0,0], [0,0,0,6,3,2,0,0,0], [5,0,0,0,0,1,0,0,2], [0,2,4,0,0,0,6,8,0], [8,0,0,2,0,0,0,0,7], [0,0,0,5,4,7,0,0,0], [0,0,2,0,0,0,8,0,0], [0,4,5,1,0,0,9,0,0] ]
Define getSquare(grid,row,col)
row=1,col=2
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 25
getSquare
def getSquare(grid,row,col): c = [] for i in range(2): for j in range(2): c = c + [grid[row*3+i][col*3+j]] return c
row=1,col=2
6 7 8345
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 26
Exercise (discussion)
Using check, getRow, getCol, getSquare, define checkGrid.
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 27
checkGrid
def checkGrid(grid): for i in range(9): if not(check(getRow(grid,i))): return False for i in range(9): if not(check(getCol(grid,i))): return False for i in range(2): for j in range(2): if not(check(getSquare(grid,i,j))): return False return True
2006/07 Computing Science 1P Tutorial 19 - Simon Gay 28
Should we add a check that grid is a 9x9 matrix?
• Yes• No• Don't know
top related