2D Array and Matrix.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 8 Multidimensional.
Advertisements

CS305j Introduction to Computing Two Dimensional Arrays 1 Topic 22 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right.
Sum Things Missing ! Let’s Play Sum Things Missing ! Rules: The Clue (dark box) is the Sum of the Digits (white boxes) Only use Digits 1 thru 9 (no 0)
What's ahead? Conway gives a whole new outlook on "loopy" ! John Conway 10/18 no class - Fall break 10/19 no new hw due 10/11 Dynamic & binary data 10/12.
Lists in Python.
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.
The Game of Life A simulation of "life". From simple rules, complex behavior arises Rules –A cell that is alive and has fewer than two live neighbors dies.
Topic 26 Two Dimensional Arrays "Computer Science is a science of abstraction -creating the right model for a problem and devising the appropriate mechanizable.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
1 Data Structures CSCI 132, Spring 2014 Lecture 4 Implementing Life.
This week in CS 5 HW 9 (2 problems) M/T sections W/Th sections due Sunday, 11/4 at midnight due Monday, 11/5 at midnight Recitation for HW9 -- Friday 11/2.
Homework 9 Due ( M & T sections ) ( W & Th sections ) at midnight Sun., 11/3 Mon., 11/4 Problems
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Homework 8 Due ( MT sections ) ( WTh sections ) at midnight Sun., 10/28 Mon., 10/29 Problems Reading is under week 7, however.
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
Ionut Trestian Northwestern University
AddData Number Sense Subtract.
Week 9 - Wednesday.  What did we talk about last time?  2D arrays  Queen attacking pawn example  Started Game of Life.
EECS 110: Lec 12: Mutable Data Aleksandar Kuzmanovic Northwestern University
Practical Session 9 Computer Architecture and Assembly Language.
Repetitive Structures
EECS 110: Lec 12: Mutable Data
Matrices Rules & Operations.
EGR 2261 Unit 10 Two-dimensional Arrays
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Two-Dimensional Arrays
Week 13: Searching and Sorting
Sequences and Indexing
Topic Dimensional Arrays
support structure ACDTopCoreThickness + 2 * ACDFaceThickness
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
2-D Lists Taken from notes by Dr. Neil Moore
Week 11 - Friday CS221.
Week 9 - Monday CS 121.
While Loops BIS1523 – Lecture 12.
CMSC 341 Lecture 10 B-Trees Based on slides from Dr. Katherine Gibson.
Shortest Path Graph represents highway system Edges have weights
Arrays … The Sequel Applications and Extensions
Each column represents another power of the base
Some Basics for Problem Analysis and Solutions
CSCE 411 Design and Analysis of Algorithms
More Loop Examples Functions and Parameters
HMC’s Galileo Auditorium Some of Cope’s generated MP3s:
CS 220: Discrete Structures and their Applications
Chapter 8 Search and Sort
Topic 26 Two Dimensional Arrays
Two-Dimensional Arrays
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Numbers and their bases
Ionut Trestian Northwestern University
Microsoft Excel 101.
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Two-Dimensional Arrays
Lecture 13: Two-Dimensional Arrays
Multidimensional Arrays
Computer Architecture and Assembly Language
2D Arrays Just another data structure Typically use a nested loop
ARRAYS 2 GCSE COMPUTER SCIENCE.
Northwestern University
Functions continued.
EET 2259 Unit 9 Arrays Read Bishop, Sections 6.1 to 6.3.
CSE 1020:Software Development
Computer Architecture and Assembly Language
EECS 110: Lec 12: Mutable Data
2-D Lists Taken from notes by Dr. Neil Moore
More 2D Array and Loop Examples Functions and Parameters
2D Array, Nested Loops.
2D Array and Matrix Application: Game of Life
Announcements Exam 2 Next Week!! Final Exam :
Presentation transcript:

2D Array and Matrix

Lists’ flexibility Lists can hold ANY type of data x = [ 42, 75, 70 ] 42 75 70 list int int int x We can equally well imagine them as vertical structures. 42 list int x 75 int 70 int

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

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

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

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

Rectangular arrays What does x[1] refer to? list list x[0] x x[0][0] list x[1] list x[2] x[2][3] What does x[1] refer to? What value is changed with x[1][2]=42 ? How many rows does x have, in general ? How many columns does x have, in general ?

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

Sizing up arrays… but NEITHER ONE works! x = 3*[ 5*[0] ] [[0,0,0,0,0], How could we create this rectangular array of 0s? x = 3*[ 5*[0] ] but NEITHER ONE works! [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]] or because lists are handled by reference ! x = 5*[ 3*[0] ]

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

Safely creating arrays… def createOneRow( width ): """ does just that """ row = [] # start with nothing for col in range( width ): row = row + [0] return row # loop and append! I need to verify this. Why can't we just do return [0]*width ? I tested it and it worked, so I'm probably missing something? Anyway, this is in the script for the slides. So, how would you create a list of rows!?

Safely creating arrays… def create2dArray( width, height ): """ does just that """ x = [] # start with nothing for rowCount in range( height ): row = [0] * width x = x + [row] return x Run this as well. They will need this for the lab. the same approach as before!

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

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

$Maximum Profit$ Your stock's prices: A good investment strategy: maximize your profit! Day Price Stocks valid to sell 0 40.0 40.0 1 80.0 40.0, 80.0 2 10.0 40.0, 80.0, 10.0 3 30.0 40.0, 80.0, 10.0, 30.0 4 27.0 … 5 52.0 … 6 5.0 … 7 15.0 … Illustrate how to solve this algorithmically. Answer should be: 42. Be sure to mention that they should not simply find min and max!!! You need to buy, and then sell AFTER you buy you must sell after you buy.

smallest difference >>> diff( [7,3],[0,6] ) 1 Example: smallest difference >>> diff( [7,3],[0,6] ) 1 Return the minimum difference between one value from lst1 and one value from lst2. Only consider absolute differences. def diff( lst1, lst2 ): lst1 and lst2 will be lists of numbers def diff(lst1, lst2): """ return the minimum difference between any number in lst1 and lst2 """ minSoFar = abs(lst1[0] - lst2[0]) for x in lst1: for y in lst2: if abs(x-y) < minSoFar: minSoFar = abs(x-y) return minSoFar

smallest difference >>> diff( [7,3],[0,6] ) 1 Example: smallest difference >>> diff( [7,3],[0,6] ) 1 Return the minimum difference between one value from lst1 and one value from lst2. Only consider absolute differences. def diff( lst1, lst2 ): lst1 and lst2 will be lists of numbers minDiffSoFar = 9999999 for value1 in lst1: for value2 in lst2: diff = abs(value1 - value2) if diff < minDiffSoFar: minDiffSoFar = diff return minDiffSoFar def diff(lst1, lst2): """ return the minimum difference between any number in lst1 and lst2 """ minSoFar = abs(lst1[0] - lst2[0]) for x in lst1: for y in lst2: if abs(x-y) < minSoFar: minSoFar = abs(x-y) return minSoFar

HW 8, Lab -- “Life” Grid World red cells are alive Evolutionary rules John Conway 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

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! The next several slides simulate the game. Assume this is a starting state Which cells will give birth to a new cell?" IN row,col form: 2,2 and 3,4 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

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! add (1,3) and (4,3) delete (2,3) and (3,3) 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

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

Problem 1 -- Creating Life next_life_generation( A ) returns new generation or "array" old generation or "array" 1 2 3 4 5 1 2 3 4 5 1 1 2 2 Adds (1,1), (2,3), and (3,1) Deletes (4,4) Answer next slide 3 3 4 4 5 5

Problem 1 -- Creating Life next_life_generation( A ) returns new generation or "array" old generation or "array" 1 2 3 4 5 1 2 3 4 5 1 1 2 2 3 3 4 4 5 5

Problem 1 -- Details For each generation… ? 0 represents an empty cell next_life_generation( A ) returns new generation or "array" old generation or "array" 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 life out there...

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?

A few matrix and array problems Given a matrix (2D array with equal dimension), how to compute the sum for the top-right half? [[3,2,6,8], [9,2,5,7], [0,3,2,3], [1,2,3,4]] The result should be 42

The key is to figure out the indices 0 1 2 3 columns 1 2 3 [[3,2,6,8], [9,2,5,7], [0,3,2,3], [1,2,3,4]] rows When row is 0, column goes from 0 to 3 When row is 1, column goes from 1 to 3 When row is 2, column goes from 2 to 3 When row is 3, column goes from 3 to 3 for row in range( 4 ): for col in range( row, 4 ): # do work

def sumUpperRight( matrix ): ''' Sum up the upper-right corner of a matrix. Matrix is a 2D array with equal dimensions ''' sum = 0 for row in range( len( matrix ) ): # row for col in range( row, len( matrix[0] ) ): # column sum += matrix[row][col] return sum matrix = [[3,2,6,8], [9,2,5,7], [0,3,2,3], [1,2,3,4]] value = sumUpperRight( matrix ) print( 'the sum of right upper corner is ', value )

Given a matrix (2D array with equal dimension), how to compute the maximum for each row and each column? # compute row max for a given ‘row’ rowMax = matrix[row][0] for i in range( len( matrix[row] ) ): if matrix[row][i] > max: rowMax = matrix[row][i] But how to go through a column to compute the maximum? # compute column max for a given ‘column’ colMax = matrix[0][col] for i in range( len( matrix ) ): if matrix[i][col] > max: rowMax = matrix[i][col]

In addition to the row and column maximum, find the maximum of the entire matrix? def findMax( matrix, rowMax, colMax ): ''' Given a matrix, find and return the global max, an array of row max and an array of column max ''' max = matrix[0][0] # current max for i in range( len( matrix) ): # find each row max rowMax[i] = findRowMax( matrix, i ) if rowMax[i] > max: max = rowMax[i] for i in range( len( matrix[0] ) ): # find each column max colMax[i] = findColMax( matrix, i ) if colMax[i] > max: max = colMax[i] return max