Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue.

Similar presentations


Presentation on theme: "CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue."— Presentation transcript:

1 CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue. 11/16-17 Caffeine Anyone choose Gauss or Conway for Halloween? Coming up in CS 5:

2 Mandelbrot sets… !

3 Mutable vs. Immutable data Mutable types: dictionary Immutable types: list tuple string int float bool What's a dictionary? I guess I'll have to look it up! s 'hi' s = 'hi' you can change what is named, but you can't change already-named data!

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

5 Pass By Value 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

6 7 Pass By Value 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 “Pass by value” means that data is copied when sent to a function 42

7 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 What gets passed by value here? fav L[0]L[1] 7 11 fav

8 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 L[0]L[1] 7 11 can change data elsewhere! The reference is copied! 42 fav

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

10 Views of the world Where does CS fit ?

11 Views of the world Engineers think their equations approximate reality.

12 Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations.

13 Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care.

14 Views of the world Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care. Axioms Definitions Creating structure from a few simple facts... Proof 60˚ 70˚ 20˚ 10˚ ?

15 60˚70˚ 20˚ 10˚ ? (without using trig) The John Conway Challenge….

16 Views of the world Axioms Definitions Creating structure from a few simple facts... Creating structure from a few simple actions... if/else while for arithmetic operations variables arrays ProofAlgorithm CSMathematics

17 Views of the world Axioms Definitions Creating structure from a few simple facts... Creating structure from a few simple actions... if/else while for arithmetic operations variables arrays ProofAlgorithm Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care. CS

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

19 42.0 75.0 70.0 double list A 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 float list A

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

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

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

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

24 list A A[0] A[1] A[2] A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ] Rows within 2d arrays need not be the same length… No "jagged" arrays… at least not in hw 9

25 Rectangular arrays list A A[0] A[1] A[2] How many rows does A have, in general ? How many columns does A have, in general ? What does each component of A[1][2] mean ? A[1][2] = 42 A[2][3] A[0][0]

26 Sizing up arrays… How could we create this rectangular array of 0 s? or A = 5*[ 3*[0] ] A = 3*[ 5*[0] ] [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]

27 Sizing it up… How could we create a rectangular array (of default data, 0 ), given its height and width ? but NEITHER ONE works! A = height*[ [0]*width ] A = width*[ [0]*height ] this is the right size, but doesn't work! because lists are handled by reference !

28 What's really going on? A = 3*[ 5*[0] ] inner = 5*[0] A = 3*[inner] Inner esting! list A inner copies the list reference, not the list data "shallow copy"

29 Safely creating arrays… def createOneRow( width, height ): """ does just that """ row = [] # start with nothing for col in range( width ): return row So, how would you create a list of rows!?

30 Problem 2 Menu An array of array handlers... Functions printMenu enterValues print multRow addRowSIntoRowD addMofRowSIntoRowD solve (1) Enter the size and values of an array (2) Print the array (3) Multiply an array row by a constant (4) Add one row into another (5) Add a multiple of one row to another (6) Solve! (9) Quit Which choice would you like? S ~ source row index D ~ destination row index M ~ a multiplier

31 “Quiz” def mystery(A): """ what happens to A ? """ NUM_ROWS = len(A) NUM_COLS = len(A[0]) for row in range( 0,NUM_ROWS ): for col in range( 0,NUM_COLS ): if row == col: A[row][col] = 42 else: A[row][col] += 1 1 2 3 4 5 6 7 8 9 10 11 12 Before After A A Starting with the 2d array A shown here, what are the values in A after running this code? row 0 row 1 row 2 col 0 col 1col 2col 3 What are the resulting values in A? Name(s)

32 “Quiz” def add2ofRow1IntoRow2( A ): 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00 before after A A 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00 two of row 1 are to be added to row 2 row 0 row 1 row 2 row 0 row 1 row 2 Write a method that adds two times the values in row # 1 into the values in row # 2. Only row 2's values change. You may assume that A has at least three rows! How could you make the source and destination rows inputs to this function - and then use those inputs? The values in row 1 should not change

33 def addRowSIntoRowD( s, d, A ): 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00 row 0 row 1 row 2 row 0 row 1 row 2 Write a method that adds row # s into the values in row # d. s d s d How about a multiple, m, of the source row? s d ~ index of source row ~ index of destination row

34 Problem 2 Menu An array of array handlers... (1) Enter the size and values of an array (2) Print the array (3) Multiply an array row by a constant (4) Add one row into another (5) Add a multiple of one row to another (6) Solve! (9) Quit Which choice would you like? Functions printMenu enterValues print multRow addRowSIntoRowD addMofRowSIntoRowD solve

35 Gaussian Elimination 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 Goal: Find p,n,q Carl Gauss is so money!

36 Gaussian Elimination 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 Goal: get 1s along the diagonal get 0s elsewhere on the left Find p,n,q 1p + 0n + 0q = 1.00 0p + 1n + 0q = 5.00 0p + 0n + 1q = 25.00

37 Gaussian Elimination 2p + 3n + -1q = -8.00 -3p + -1n + 2q = 42.00 1p + -9n + 4q = 56.00 Goal: get 1s along the diagonal get 0s elsewhere on the left Find p,n,q 1p + 0n + 0q = 1.00 0p + 1n + 0q = 5.00 0p + 0n + 1q = 25.00 Just the array is necessary ! We can get rid of the variables... Using only row operations (our methods) ! where to start?

38 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 Which direction should we head next !?! GOAL: to get this side to look like the identity matrix. RESULT: this column will be our solution!

39 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 a hint as to the direction to head next… !

40 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2

41 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 1.00 0.00 -0.71 -16.85 0.00 1.00 0.14 30.00 0.00 0.00 6.00 150.00 multiply Row 1 by 1/3.5 add a multiple of Row 1 to Row 0 add a multiple of Row 1 to Row 2 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2

42 Solve 1.00 1.50 -0.50 -4.00 -3.00 -1.00 2.00 42.00 1.00 -9.00 4.00 56.00 multiply Row 0 by 0.5 1.00 1.50 -0.50 -4.00 0.00 3.50 0.50 30.00 0.00 -10.50 4.50 44.00 add 3 times Row 0 to Row 1 add -1 times Row 0 to Row 2 1.00 0.00 -0.71 -16.85 0.00 1.00 0.14 30.00 0.00 0.00 6.00 150.00 1.00 0.00 0.00 1.00 0.00 1.00 0.00 5.00 0.00 0.00 1.00 25.00 same for other columns as far as possible... multiply Row 1 by 1/3.5 add a multiple of Row 1 to Row 0 add a multiple of Row 1 to Row 2

43 Lab Problem -- “Life” Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty 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) John Conway

44 Problem 1 -- Life Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty 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)

45 Problem 1 -- Life Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty 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)

46 Problem 1 -- Life Evolutionary rules Grid World Everything depends on a cell’s eight neighbors red cells are alive white cells are empty 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) life out there... Keep going!

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

48 Problem 1 -- Details 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 before updating any of them http://www.math.com/students/wonders/life/life.html updateNextLife( oldB, newB ) old generation or "board" new generation or "board" life out there... ?

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

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? How sophisticated can the structures in the life universe be? http://www.ibiblio.org/lifepatterns/ Are all feasible configurations reachable? "glider" "Gosper glider gun"

51 Today you'll be Lifing it up in lab! on over…so

52 Problem 1 -- to  and beyond! public static void update(int[][] last, int[][] next) { for (int r=1 ; r<last.length-1 ; ++r) { for (int c=1 ; c<last[r].length-1 ; ++c) { int oldvalue = last[r][c]; if (oldvalue == 0) // look at last next[r][c] = 1; // assign to next else next[r][c] = 0; } What will this do?

53 Problem 2 -- Details public static void update(int[][] last, int[][] next) { for (int r=1 ; r<last.length-1 ; ++r) { for (int c=1 ; c<last[r].length-1 ; ++c) { int oldvalue = last[r-1][c-1]; if (oldvalue == 0) next[r][c] = 1; else next[r][c] = 0; } How does this change things?

54 Problem 2 – Multi-species Life (!) Create a set of rules to evolve two species of cells (plus empty). The Challenge updateMulti(int[][] last, int[][] next) Give both species a good chance of survival in the same environment. stability is an open biological question… more species are OK, too… 0 1 2 …

55 Lab this week Problem 1: Gaussian Elimination You’ll need to write (and use) Problem 2: Life and Multi-species Life! Extra Credit: printMenu enterValues print multRow addRowaToRowb addMxRowaToRowb solve public void update(int[][] last, int[][] next) 0123450 12345 0 1 2 3 4 5 0 1 2 3 4 5 A matrix-inverse feature for Problem 1... Doughnut Life for Problem 2 … Lab: M-Z public void updateMulti(int[][] last, int[][] next)

56 “Quiz” public static void mysteryMethod(int[][] A) { for (int r = 0 ; r < A.length ; ++r) { for (int c = 0 ; c < A[r].length ; ++c) { if (r == c) { A[r][c] = 42; } else { A[r][c] = A[r][c] + 1; } } // end of mystery method 1 2 3 4 5 6 7 8 9 10 11 12 Before After A A Starting with the 2d array A shown here, what are the values in A after running this code? row 0 row 1 row 2 col 0 col 1col 2col 3 What are the resulting values in A?

57 “Quiz” public static void addTwoOfRow1IntoRow2(double[][] A) { } 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 10.00 20.00 30.00 40.00 before after A A 0.00 1.00 0.00 1.00 3.00 0.50 0.50 3.00 16.00 21.00 31.00 46.00 two of row 1 are to be added to row 2 example row 0 row 1 row 2 row 0 row 1 row 2 Write a method that adds two times the values in row #1 into the values in row #2. The values in row #1 should not change! (The values in row #0 also don’t change!)

58 Watch out! public static void main(String[] args) { H.out.println(“Welcome to conformity, inc.”); int[] fav = new int[2]; fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s conform(fav); H.out.println(fav + “ and ” + fav + “! Me too!”); } int[] fav fav[0]fav[1] What will happen here?!

59 Watch out! public static void main(String[] args) { H.out.println(“Welcome to conformity, inc.”); int[] fav = new int[2]; fav[0] = 7; fav[1] = 11; // The user’s TWO favorite #s conform(fav); H.out.println(fav[0] + “ and ” + fav[1] + “! Me too!”); } public static void conform(int[] fav) { fav = new int[2]; fav[0] = 42; fav[1] = 42; return; } int[] fav fav[0]fav[1] int[] fav

60 This week in IS 313 HW 7 (2 problems) due Thursday, 11/7 at midnight A dizzying array of possibilities... Reading: Week 9’s online notes Still seeking a costume? John Conway Carl Gauss This week’s credits:

61 Survival of the stablest can they be unbounded spatially? what are stable “life” forms? http://www.math.com/students/wonders/life/life.html The Questions of Life... can they grow forever? how fast can they travel? how densely can they grow?

62 “Pass By Value” public static void main(String[] args) { H.out.println(“Welcome to Conformity, Inc.”); int fav = 7; // The user’s favorite # conform(fav); H.out.println(fav + “ is my favorite, too!”); }

63 42.00 -100.10 3.14 7.00 42.00 -100.10 3.14 7.00 42.00 -100.10 3.14 7.00 42 -100.1 3.14159 7 42 -100.1 3.14 7

64 Design - not just software Please see instructions on HW 9. You have some choice on what parts of this chapter to read. Conceptual Models –Watch crown vs. buttons –Directory structures Visibility and Cues –Banks of glass doors –Telephone buttons Feedback – A refrigerator – Phone settings Don Norman's observations:

65 Affordances ~ physical and cultural expectations

66 Microsoft Access Windows NT Microsoft Outlook User interface Hall of Shame http://homepage.mac.com/bradster/iarchitect/shame.htm

67 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 ): return A


Download ppt "CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities… John Conway Carl Gauss This week’s credits: Exam 2 on Mon.,Tue."

Similar presentations


Ads by Google