Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECS 110: Lec 12: Mutable Data

Similar presentations


Presentation on theme: "EECS 110: Lec 12: Mutable Data"— Presentation transcript:

1 EECS 110: Lec 12: Mutable Data
Aleksandar Kuzmanovic Northwestern University

2 EECS 110 Today infinitely nested structure… HW 5 Preview:
Pr 1 (Lab): The Game of Life Pr 2: Markov Text Generation required optional Pr 3: ASCII Art Pr 4: A program that reads Homework 5: due on 5/16 Pr 5: Gaussian Elimination

3 Mutable vs. Immutable data
Changeable types: Unchangeable types: tuple dictionary float string list bool int

4 Functions and (immutable) Variables
def fact(a): result = 1 while a > 0: result *= a a -= 1 return result >>> x = 5 >>> y = fact(x) >>> x ?? NO SIDE EFFECTS!

5 Functions and (immutable) Variables
def fact(a): result = 1 while a > 0: result *= a a -= 1 return result >>> x = 5 >>> y = fact(x) >>> x 5 NO SIDE EFFECTS!

6 Functions and (immutable) Variables
def swap(a, b): temp = a a = b b = temp >>> x = 5 >>> y = 10 >>> swap(x, y) >>> print x, y ?? x y a b temp

7 Functions and (immutable) Variables
def swap(a, b): temp = a a = b b = temp >>> x = 5 >>> y = 10 >>> swap(x, y) >>> print x, y 5, 10 x y a b temp

8 Functions and Mutable Types
MyL def swap(L, i1, i2): temp = L[i1] L[i1] = L[i2] L[i2] = temp >>> MyL = [2, 3, 4, 1] >>> swap(myL, 0, 3) >>> print myL ?? L i1 i2 RAM 42 43 44 45

9 Functions and Mutable Types
MyL def swap(L, i1, i2): temp = L[i1] L[i1] = L[i2] L[i2] = temp >>> MyL = [2, 3, 4, 1] >>> swap(myL, 0, 3) >>> print myL [1,3,4,2] L i1 i2 RAM 42 43 44 45

10 Reference vs. Value Mutable types: Unmutable types: tuple dictionary
float string list bool int L = [5,42,'hi'] L = 42 Reference, Pointer, id 42 5 42 'hi' L L L[0] L[1] L[2]

11 “Pass By Value” 7 def main(): """ calls conform """
print " Welcome to Conformity, Inc. " fav = 7 conform(fav) print " My favorite number is", fav def conform(fav): """ sets input to 42 """ fav = 42 return fav 7 fav fav

12 “Pass by value” means that data is copied when sent to a method
def main(): """ calls conform """ print " Welcome to Conformity, Inc. " fav = 7 conform(fav) print " My favorite number is", fav def conform(fav): """ sets input to 42 """ fav = 42 return fav 7 fav PASS BY VALUE 7 42 fav “Pass by value” means that data is copied when sent to a method

13 Passing lists by value…
def main() """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav def conform2(fav) """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 7 11 fav L[0] L[1] fav What gets passed by value here?

14 Passing lists by value…
def main(): """ calls conform2 """ print " Welcome to Conformity, Inc. " fav = [ 7, 11 ] conform2(fav) print " My favorite numbers are", fav def conform2(fav): """ sets all of fav to 42 """ fav[0] = 42 fav[1] = 42 7 11 fav L[0] L[1] The reference is copied! fav can change data elsewhere!

15 Those changes will be visible everywhere.
The conclusion You can change the contents of lists in functions that take those lists as input. (actually, lists or any mutable objects) Those changes will be visible everywhere. (immutable objects are safe, however)

16 More examples def zeroOdd1( L ): for i in range(len(L)):
if L[i] % 2 == 1: L[i] = 0 def zeroOdd2( L ): for i in L: if i % 2 == 1: i = 0 >>> L = [1, 2, 3, 4, 5] >>> zeroOdd1(L) >>> L ?? >>> zeroOdd2(L)

17 More examples def zeroOdd1( L ): for i in range(len(L)):
if L[i] % 2 == 1: L[i] = 0 def zeroOdd2( L ): for i in L: if i % 2 == 1: i = 0 >>> L = [1, 2, 3, 4, 5] >>> zeroOdd1(L) >>> L [0,2,0,4,0] >>> zeroOdd2(L) ??

18 More examples def zeroOdd1( L ): for i in range(len(L)):
if L[i] % 2 == 1: L[i] = 0 def zeroOdd2( L ): for i in L: if i % 2 == 1: i = 0 >>> L = [1, 2, 3, 4, 5] >>> zeroOdd1(L) >>> L [0,2,0,4,0] >>> zeroOdd2(L) [1,2,3,4,5]

19 they don’t have to be horizontal lists!
Lists’ flexibility Lists can hold ANY type of data A = [ 42., 75., 70. ] 42.0 75.0 70.0 list float float float A they don’t have to be horizontal lists!

20 they don’t have to be horizontal lists!
Lists’ flexibility Lists can hold ANY type of data A = [ 42., 75., 70. ] 42.0 75.0 70.0 list float float float A 42.0 list double they don’t have to be horizontal lists! A 75.0 double 70.0 double

21 Lists’ flexibility Lists can hold ANY type of data 42.0 75.0 70.0 42 7
double double double A 42 7 -11 list int int int A “go” “red” “sox!” list String String String A

22 2d lists or arrays Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list A

23 2d arrays Lists can hold ANY type of data -- including lists !

24 Rows within 2d arrays need not be the same length…
Jagged arrays Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list A[0] A list A[1] list A[2] Rows within 2d arrays need not be the same length…

25 We will not use jagged arrays
at least in hw 5 Lists can hold ANY type of data -- including lists ! A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] list list A[0] A list A[1] list A[2] Rows within 2d arrays need not be the same length…

26 Rectangular arrays Handling rectangular arrays …
list list A[0] A A[0][0] list A[1] list A[2] A[2][3] How many rows does A have, in general ? How many columns does A have, in general ?

27 Rectangular arrays Handling rectangular arrays … len(A)
list list A[0] A A[0][0] list A[1] list A[2] A[2][3] len(A) How many rows does A have, in general ? How many columns does A have, in general ?

28 Rectangular arrays Handling rectangular arrays … len(A) len(A[0])
list list A[0] A A[0][0] list A[1] list A[2] A[2][3] len(A) How many rows does A have, in general ? len(A[0]) How many columns does A have, in general ?

29 Which one works? A = [ [0]*width ]*height A = [ [0]*height ]*width
How could we create a rectangular array (of default data, 0), given its height and width ? A = [ [0]*width ]*height or A = [ [0]*height ]*width

30 Which one works? A = [ [0]*width ]*height A = [ [0]*height ]*width
How could we create a rectangular array (of default data, 0), given its height and width ? A = [ [0]*width ]*height A = [ [0]*height ]*width

31 What's really going on? A = [ [0]*width ]*height inner = [0]*width
A = [inner]*height

32 Creating a 2d array def create2dArray( width, height ):
""" does just that """ A = [] # start with nothing for row in range( height ): for col in range( width ):

33 Creating a 2d array def create2dArray( width, height ):
""" does just that """ A = [] # start with nothing for row in range( height ): A = A + [[]] for col in range( width ): A[row] = A[row] + [0] return A

34 "Quiz" What are the values of A, B, C and D at the indicated points?
Name(s)__________________________________ What are the values of A, B, C and D at the indicated points? def mystery1(L1, L2): S = L1 for i in range(len(L2)): S[i] += L2[i] return L2 def mystery2(L1, L2): S = [0]*len(L1) S[i] = L1[i] + L2[i] >>> A = [22, 10, 30] >>> B = [20, 32, 12] >>> C = [] >>> D = [] >>> C = mystery1(A, B) >>> D = mystery2(A, B) 1) 2) 3) 1 2 3

35 "Quiz" What are the values of A, B, C and D at the indicated points?
Name(s)__________________________________ What are the values of A, B, C and D at the indicated points? def mystery1(L1, L2): S = L1 for i in range(len(L2)): S[i] += L2[i] return L2 def mystery2(L1, L2): S = [0]*len(L1) S[i] = L1[i] + L2[i] >>> A = [22, 10, 30] >>> B = [20, 32, 12] >>> C = [] >>> D = [] >>> C = mystery1(A, B) >>> D = mystery2(A, B) 1) 2) 3) A: [22, 10, 30] B: [20, 32, 12] C: [] D: [] 1 2 3

36 "Quiz" What are the values of A, B, C and D at the indicated points?
Name(s)__________________________________ What are the values of A, B, C and D at the indicated points? def mystery1(L1, L2): S = L1 for i in range(len(L2)): S[i] += L2[i] return L2 def mystery2(L1, L2): S = [0]*len(L1) S[i] = L1[i] + L2[i] >>> A = [22, 10, 30] >>> B = [20, 32, 12] >>> C = [] >>> D = [] >>> C = mystery1(A, B) >>> D = mystery2(A, B) 1) 2) 3) A: [22, 10, 30] B: [20, 32, 12] C: [] D: [] A: [22, 10, 30] B: [20, 32, 12] C: [20, 32, 12] D: [] 1 2 3

37 "Quiz" What are the values of A, B, C and D at the indicated points?
Name(s)__________________________________ What are the values of A, B, C and D at the indicated points? def mystery1(L1, L2): S = L1 for i in range(len(L2)): S[i] += L2[i] return L2 def mystery2(L1, L2): S = [0]*len(L1) S[i] = L1[i] + L2[i] >>> A = [22, 10, 30] >>> B = [20, 32, 12] >>> C = [] >>> D = [] >>> C = mystery1(A, B) >>> D = mystery2(A, B) 1) 2) 3) A: [22, 10, 30] B: [20, 32, 12] C: [] D: [] A: [22, 10, 30] B: [20, 32, 12] C: [20, 32, 12] D: [] A: [22, 10, 30] B: [20, 32, 12] C: [20, 32, 12] D: None 1 2 3

38 += mutates Mutable Data (lists)
WARNING: For mutable data types, the following are NOT the same MUTATES A (B changes too) A = [] B = A A += [42, 42] NOT THE SAME! A = [] B = A A = A + [42, 42] COPIES A (B does not change)

39 The Game of Life: History
Created by John Horton Conway, a British Mathematician Inspired by a problem presented by John Von Neumann: Build a hypothetical machine that can build copies of itself First presented in 1970, Scientific American

40 Problem 1 -- “Life” Grid World red cells are alive Evolutionary rules
Everything depends on a cell’s eight neighbors Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead) white cells are empty

41 Problem 1 -- Life Grid World red cells are alive Evolutionary rules
Everything depends on a cell’s eight neighbors Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead) white cells are empty

42 Problem 1 -- Life Grid World red cells are alive Evolutionary rules
Everything depends on a cell’s eight neighbors Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead) white cells are empty

43 Problem 1 -- Life Grid World red cells are alive Evolutionary rules
Everything depends on a cell’s eight neighbors Exactly 3 neighbors give birth to a new, live cell! Exactly 2 or 3 neighbors keep an existing cell alive Any other number of neighbors kill the central cell (or keep it dead) Keep going! white cells are empty life out there...

44 Problem 1 -- Creating Life
updateNextLife(oldB, newB) old generation or "board" new generation or "board" 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5

45 Problem 1 -- Creating Life
updateNextLife(oldB, newB) old generation or "board" new generation or "board" 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5

46 Problem 1 -- Details updateNextLife(oldB, newB) For each generation…
old generation or "board" new generation or "board" For each generation… 0 represents an empty cell 1 represents a living cell outermost edge should always be left empty (even if there are 3 neighbors) compute all cells based on their previous neighbors life out there...

47 Problem 1 – Main Loop def life( width, height ):
""" will become John Conway's Game of Life... """ B = createBoard(width, height) csplot.showAndClickInIdle(B) while True: # run forever csplot.show(B) # show current B time.sleep(0.25) # pause a bit oldB = B B = createBoard( width, height ) updateNextLife( oldB, B ) # gets a new board

48 Problem 1 – Main Loop Update MUTATES the list B
def life( width, height ): """ will become John Conway's Game of Life... """ B = createBoard(width, height) csplot.showAndClickInIdle(B) while True: # run forever csplot.show(B) # show current B time.sleep(0.25) # pause a bit oldB = B B = createBoard( width, height ) updateNextLife( oldB, B ) # gets a new board Update MUTATES the list B Why not just have update RETURN a new list? (i.e., why bother with mutation at all?)

49 Problem 1 -- to  and beyond!
"rocks" Are there stable life configurations? "plants" Are there oscillating life configurations? period 3 period 2 "animals" Are there self-propagating life configurations?

50 Problem 1 -- to  and beyond!
Are there life configurations that expand forever? What is the largest amount of the life universe that can be filled with cells? Are all feasible configurations reachable? How sophisticated can the structures in the life universe be?

51 See you in Lab!


Download ppt "EECS 110: Lec 12: Mutable Data"

Similar presentations


Ads by Google