1 / 28

Computing Science 1P

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

hume
Download Presentation

Computing Science 1P

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 need help. 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 contain the digits 1 – 9. Computing Science 1P Tutorial 19 - Simon Gay

  4. 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

  5. What kind of data structure? • Based on lists • Based on dictionaries • Something else • Don't know Computing Science 1P Tutorial 19 - Simon Gay

  6. 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

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

  8. 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

  9. 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

  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 the idea. Write a Python definition if you have time. Computing Science 1P Tutorial 19 - Simon Gay

  11. 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

  12. Can you see any problems with check ? • Yes • No • Don't know Computing Science 1P Tutorial 19 - Simon Gay

  13. 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

  14. 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

  15. contains def contains(x,v): for a in x: if a == v: return True return False 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 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 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 Computing Science 1P Tutorial 19 - Simon Gay

  19. Have we forgotten anything? • Yes • No • Don't know 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: • 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

  21. 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

  22. 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

  23. 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

  24. 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

  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 6 7 8 3 row=1,col=2 4 5 Computing Science 1P Tutorial 19 - Simon Gay

  26. Exercise (discussion) Using check, getRow, getCol, getSquare, define checkGrid. 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 Computing Science 1P Tutorial 19 - Simon Gay

  28. Should we add a check that grid is a 9x9 matrix? • Yes • No • Don't know Computing Science 1P Tutorial 19 - Simon Gay

More Related