### CS30 - Spring 2016 - Class 21

#### Example code in this lecture

matrix_aliasing.py

#### Lecture notes

• classes next semester

• assignment 8 solution review

- init
- there's a difference between how we're storing the board and how we display the board
- we're storing a matrix of entries that have one of two things
- integer (if it's been filled in)
- list of integer if it hasn't been
- we can display this any way we want
- __str__ does it in a "pretty" way by only displaying the filled in entries
- get_raw_string prints out all of the information

- look at matrix_aliasing.py code
- there are 6 different examples that create matrices with the rows [1,2,3]
- which are correct?
- semi-correct: when we print it out it looks correct
- fully-correct: each row is independent and changing one doesn't affect the others
- we can test semi-correctness by running them all and printing them out
- look at the print_out_all function in matrix_aliasing.py code
- creates a list of functions called "functions"
- remember, functions are just like any other type
- we can refer to them by using their name without the parenthesis (with the parenthesis calls that function)
- functions[i] references entry i in the function list
- and we call that function with parameter 3: functions[i](3)
- this is the same as if we'd done:
f = functions[i]
f(3)
- all of them look like they're working correctly
- what does test_all do?
- creates a matrix using each method
- mutates (i.e. changes) the first entry to be 100
- checks if that changed more than just the first entry (in this case, the second row, first column)
- if we run test_all we see:
Testing create_matrix1: Passed! :)
Testing create_matrix2: Failed aliasing :(
Testing create_matrix3: Passed! :)
Testing create_matrix4: Passed! :)
Testing create_matrix5: Failed aliasing :(
Testing create_matrix6: Failed aliasing :(
- why did 2, 5 and 6 not pass and 1, 3 and 4 pass?
- 1, 3 and 4 all create NEW lists for each row
- 3: range creates a new list
- 4: row[:] copies the list, creating a new list
- 2, 5 and 6 result in a matrix where all of the rows references the SAME list

- a couple of simple checks for init to avoid aliasing:
- do the example in the assignment and make sure it works
- this won't check for aliasing, but it will check that the right information is there
- change an entry in the board and make sure only that entry changes:
>>> s = SudokuState()
>>> s.board[0][0][0] = 100
>>> print s.get_raw_string()

- test each method individually
- look at the examples from assignment 8 about how to do this
- try and isolate the one method you're working on as much as possible
- don't move on and code any of the other methods until you're *very* confident that method works correctly

- think about what each method is doing and how you might use that in other methods

- if your code doesn't work, put in print statements to understand *why* it's not working
- try and isolate the point at which the code does something that you don't expect

- look at starter

- run a few examples through the code
- look at what the boards look like after being setup, but before solving