CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities…...
-
Upload
madlyn-conley -
Category
Documents
-
view
215 -
download
2
Transcript of CS 5 Today HW 9 (lab + 2 probs) due Sunday, 11/8 at midnight Dizzying arrays of possibilities…...
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:
Mandelbrot sets… !
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!
Reference vs. Value
dictionary
list
tuple
string
int
float
bool
LL[0] L[1] L[2]
Reference,Pointer,
id
L = [5,42,'hi']
5 42 'hi's
'hi'
s = 'hi'
Whee!
Mutable types: Immutable types:
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
fav
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
fav
PASSBY VALUE
“Pass by value” means that data is copied when sent to a function
42
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?
favL[0] L[1]
7 11
fav
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!
4242
fav
fav
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)
Views of the world
Where does CS fit ?
Views of the world
Engineers think their equations approximate reality.
Views of the world
Engineers think their equations approximate reality. Physicists think reality approximates their equations.
Views of the world
Engineers think their equations approximate reality. Physicists think reality approximates their equations.
Mathematicians don't care.
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˚
?
60˚ 70˚
20˚10˚
?
(without using trig)
The John Conway Challenge….
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
variablesarrays
Proof Algorithm
CSMathematics
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
variablesarrays
Proof Algorithm
Engineers think their equations approximate reality. Physicists think reality approximates their equations. Mathematicians don't care.
CS
Lists’ flexibility
Lists can hold ANY type of dataA = [ 42., 75., 70. ] 42.0 75.0 70.0
float float floatlistA
they don’t have to be horizontal lists!
42.0
75.0
70.0
double
double
double
listAthey don’t
have to be horizontal lists!
Lists’ flexibility
Lists can hold ANY type of dataA = [ 42., 75., 70. ] 42.0 75.0 70.0
float float floatlistA
Lists’ flexibility
Lists can hold ANY type of data
42.0 75.0 70.0double double doublelist
A
42 7 -11int int intlist
A
“go” “red” “sox!”
String String StringlistA
2d lists or arrays
Lists can hold ANY type of data -- including lists !
listA
A = [ [1,2,3,4], [5,6], [7,8,9,10,11] ]
listA
2d arrays
list
list
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] ]
listA
Jagged arrays
list
list
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…
listA
list
list
list
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
Rectangular arrays
listA
list
list
list
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]
Sizing up arrays…
How could we create this rectangular array of 0s?
or
A = 5*[ 3*[0] ]
A = 3*[ 5*[0] ]
[[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]
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 !
What's really going on?
A = 3*[ 5*[0] ]
inner = 5*[0]
A = 3*[inner]
Inneresting!
listA
list
list
list
inner
inner
inner
copies the list reference, not the list data
"shallow copy"
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!?
Problem 2
Menu
An array of array handlers...
Functions
printMenu
enterValues
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
“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 1 col 2 col 3
What are the resulting values in A?
Name(s)
“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 1should not change
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.
sd
sd
How about a multiple, m, of the source row?
s
d
~ index of source row
~ index of destination row
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
multRow
addRowSIntoRowD
addMofRowSIntoRowD
solve
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!
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
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?
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!
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… !
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
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
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 columnsas 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
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 anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)
John Conway
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 anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)
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 anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)
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 anexisting cell alive• Any other number of neighbors killthe central cell (or keep it dead)
life out there...
Keep going!
Problem 1 -- Creating Life
0 1 2 3 4 50 1 2 3 4 5
0
1
2
3
4
5
0
1
2
3
4
5
updateNextLife( oldB, newB )
old generation or "board" new generation or "board"
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...
?
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
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"
Today you'll be
Lifing it up
in lab!
on over…so
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?
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?
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 …
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
multRow
addRowaToRowb
addMxRowaToRowb
solve
public void update(int[][] last, int[][] next)
0 1 2 3 4 50 1 2 3 4 5
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)
“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 1 col 2 col 3
What are the resulting values in A?
“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
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!)
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?!
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
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:
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?
“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!”); }
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
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:
Affordances~ physical and cultural expectations
Microsoft Access
Windows NT
Microsoft Outlook
User interface Hall of Shame
http://homepage.mac.com/bradster/iarchitect/shame.htm
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