280 likes | 381 Views
Computing Science 1P. Large Group Tutorial 19. Simon Gay Department of Computing Science University of Glasgow. 2006/07. Remember. No lecture on Friday this week. After the Easter break, Monday 9 th April is also a holiday – no labs that day – go to another lab that week if you need
E N D
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07
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 need help. Computing Science 1P Tutorial 19 - Simon Gay
Example: Sudoku You are probably familiar with sudoku puzzles: Each row, column and 3x3 square must be filled in to contain the digits 1 – 9. Computing Science 1P Tutorial 19 - Simon Gay
Data Structure for Sudoku The first step in designing a program to work with sudoku puzzles is to choose a data structure. We have the usual options… Computing Science 1P Tutorial 19 - Simon Gay
What kind of data structure? • Based on lists • Based on dictionaries • Something else • Don't know Computing Science 1P Tutorial 19 - Simon Gay
Data Structure for Sudoku A sudoku grid is basically a 9x9 matrix, so the natural data structure 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 long as we remember which it is. Whatever we do, dealing with the 3x3 squares will be a little awkward. Computing Science 1P Tutorial 19 - Simon Gay
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. Computing Science 1P Tutorial 19 - Simon Gay
Checking a sudoku grid A key operation in any implementation of sudoku is checking whether 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, these conditions obviously also guarantee that the digits 1 – 9 appear once each in every row, column and 3x3 square. Computing Science 1P Tutorial 19 - Simon Gay
Question for discussion Can you think of a useful function to define, in order to implement 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 Computing Science 1P Tutorial 19 - Simon Gay
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 the idea. Write a Python definition if you have time. Computing Science 1P Tutorial 19 - Simon Gay
check – idea 1 Take advantage of the fact that Python provides a sort method for lists. If we sort a list, we can then easily check whether it contains the numbers 1 – 9. def check(x): x.sort() # into increasing numerical order return x == [1,2,3,4,5,6,7,8,9] Computing Science 1P Tutorial 19 - Simon Gay
Can you see any problems with check ? • Yes • No • Don't know Computing Science 1P Tutorial 19 - Simon Gay
check – idea 1 The problem is that x.sort() modifies x and this change persists after the function (see Lecture 16). To use this function we would have to be very careful about what 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] Computing Science 1P Tutorial 19 - Simon Gay
check – idea 2 Another idea is to check that the list contains 1, check that it contains 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 Computing Science 1P Tutorial 19 - Simon Gay
contains def contains(x,v): for a in x: if a == v: return True return False Computing Science 1P Tutorial 19 - Simon Gay
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 Computing Science 1P Tutorial 19 - Simon Gay
check using contains : alternative def check(x): i = 1 while i <= 9: if not(contains(x,i)): return False i = i + 1 return True Computing Science 1P Tutorial 19 - Simon Gay
check using contains : alternative 2 def check(x): for i in range(1,10): if not(contains(x,i)): return False return True Computing Science 1P Tutorial 19 - Simon Gay
Have we forgotten anything? • Yes • No • Don't know Computing Science 1P Tutorial 19 - Simon Gay
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: • Note carefully that check only works when its parameterhas length 9, and make sure we call it properly • 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 Computing Science 1P Tutorial 19 - Simon Gay
Checking a sudoku grid Now that we have the function check, how do we use it to check 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] Computing Science 1P Tutorial 19 - Simon Gay
Exercise (discussion) Define getCol(grid,col) [ [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] ] Computing Science 1P Tutorial 19 - Simon Gay
getCol def getCol(grid,col): c = [] for i in range(9): c = c + [grid[i][col]] return c Computing Science 1P Tutorial 19 - Simon Gay
Exercise (discussion) Define getSquare(grid,row,col) row=1,col=2 [ [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] ] Computing Science 1P Tutorial 19 - Simon Gay
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 6 7 8 3 row=1,col=2 4 5 Computing Science 1P Tutorial 19 - Simon Gay
Exercise (discussion) Using check, getRow, getCol, getSquare, define checkGrid. Computing Science 1P Tutorial 19 - Simon Gay
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 Computing Science 1P Tutorial 19 - Simon Gay
Should we add a check that grid is a 9x9 matrix? • Yes • No • Don't know Computing Science 1P Tutorial 19 - Simon Gay