# Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.

## Presentation on theme: "Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07

Computing Science 1P Tutorial 19 - Simon Gay2 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 help.

2006/07Computing Science 1P Tutorial 19 - Simon Gay3 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.

2006/07Computing Science 1P Tutorial 19 - Simon Gay4 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…

2006/07Computing Science 1P Tutorial 19 - Simon Gay5 What kind of data structure? Based on lists Based on dictionaries Something else Don't know

2006/07Computing Science 1P Tutorial 19 - Simon Gay6 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.

2006/07Computing Science 1P Tutorial 19 - Simon Gay7 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/07Computing Science 1P Tutorial 19 - Simon Gay8 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.

2006/07Computing Science 1P Tutorial 19 - Simon Gay9 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

2006/07Computing Science 1P Tutorial 19 - Simon Gay10 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.

2006/07Computing Science 1P Tutorial 19 - Simon Gay11 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]

2006/07Computing Science 1P Tutorial 19 - Simon Gay12 Can you see any problems with check ? Yes No Don't know

2006/07Computing Science 1P Tutorial 19 - Simon Gay13 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]

2006/07Computing Science 1P Tutorial 19 - Simon Gay14 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

2006/07Computing Science 1P Tutorial 19 - Simon Gay15 contains def contains(x,v): for a in x: if a == v: return True return False

2006/07Computing Science 1P Tutorial 19 - Simon Gay16 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/07Computing Science 1P Tutorial 19 - Simon Gay17 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/07Computing Science 1P Tutorial 19 - Simon Gay18 check using contains : alternative 2 def check(x): for i in range(1,10): if not(contains(x,i)): return False return True

2006/07Computing Science 1P Tutorial 19 - Simon Gay19 Have we forgotten anything? Yes No Don't know

2006/07Computing Science 1P Tutorial 19 - Simon Gay20 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 parameter has 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/07Computing Science 1P Tutorial 19 - Simon Gay21 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]

2006/07Computing Science 1P Tutorial 19 - Simon Gay22 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/07Computing Science 1P Tutorial 19 - Simon Gay23 getCol def getCol(grid,col): c = [] for i in range(9): c = c + [grid[i][col]] return c

2006/07Computing Science 1P Tutorial 19 - Simon Gay24 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/07Computing Science 1P Tutorial 19 - Simon Gay25 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 678 3 4 5

2006/07Computing Science 1P Tutorial 19 - Simon Gay26 Exercise (discussion) Using check, getRow, getCol, getSquare, define checkGrid.

2006/07Computing Science 1P Tutorial 19 - Simon Gay27 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/07Computing Science 1P Tutorial 19 - Simon Gay28 Should we add a check that grid is a 9x9 matrix? Yes No Don't know

Similar presentations