2D Array, Nested Loops.

Slides:



Advertisements
Similar presentations
4.4 Modeling and Optimization
Advertisements

Functions S S T : S P R E A D S H E E T S SST 5 Spreadsheet 5 Function.
Managerial Decision Making and Problem Solving Computer Lab Notes 1.
MIT and James Orlin © Game Theory 2-person 0-sum (or constant sum) game theory 2-person game theory (e.g., prisoner’s dilemma)
Spreadsheet Simulation
Mock test review Revision of Activity Diagrams for Loops,
Recursion Introduction to Computing Science and Programming I.
18 April, 2000 CS1001 Lecture 23 Quiz 5 Multi-Dimensional Array.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Lesson 4.4 Modeling and Optimization What you’ll learn about Using derivatives for practical applications in finding the maximum and minimum values in.
Array Math.
CS112 Scientific Computation Department of Computer Science Wellesley College Matrices Storing two-dimensional numerical data.
AP CALCULUS AB Chapter 4: Applications of Derivatives Section 4.4:
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
TA SURVEY Have the TA write their name on the board Fill out the survey at: The TA should walk out 5 minutes.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Week 2 Lab2 Practice Dina A. Said
CSC508 Convolution Operators. CSC508 Convolution Arguably the most fundamental operation of computer vision It’s a neighborhood operator –Similar to the.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
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:
UNIT 10 Multidimensional Arrays.
AddData Number Sense Subtract.
Two-Dimensional Data Class of 5 students Each student has 3 test scores Store this information in a two- dimensional array First dimension: which student.
Programming Loops (continued).
Repetitive Structures
Matrices Rules & Operations.
EGR 2261 Unit 10 Two-dimensional Arrays
CS1020 – Data Structures And Algorithms 1 AY Semester 2
Algorithmic complexity: Speed of algorithms
Introduction to Computing Science and Programming I
Introduction to Matrices
Understanding Spreadsheets
Variation Algebra 2/Trig Unit 3 Day 1.
Sit-In Lab 1 Ob-CHESS-ion
CS 1430: Programming in C++.
While Loops in Python.
Control Structure Senior Lecturer
More Loop Examples Functions and Parameters
Introduction to Programming
CNG 140 C Programming (Lecture set 8)
CS 220: Discrete Structures and their Applications
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Chapter 8 Search and Sort
Introduction to Matrices
25. Basic matrix operations
Gray Scale picture def pixBW(pixel): # given a pixel, change to BW
Microsoft Excel 101.
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
Algorithmic complexity: Speed of algorithms
2D Array and Matrix.
Optimization (Max/Min)
pencil, red pen, highlighter, GP notebook, graphing calculator
Multidimensional Arrays
Solve Absolute Value Inequalities
This shows the frameset container page which holds two additional pages in rows - the first row uses 50% of the page and the last row uses the rest. Note.
Algorithmic complexity: Speed of algorithms
Searching and Sorting (2)
2-Dimensional Lists (Matrices) in Python
Introduction to Computer Science
Assignment due Write a program the generates a random integer expression, presents the two operands and the result to the user, and asks the user to tell.
pencil, red pen, highlighter, GP notebook, graphing calculator
Matrix A matrix is a rectangular arrangement of numbers in rows and columns Each number in a matrix is called an Element. The dimensions of a matrix are.
EECS 110: Lec 12: Mutable Data
While Loops in Python.
More 2D Array and Loop Examples Functions and Parameters
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Announcements.
Announcements Exam 2 Next Week!! Final Exam :
Presentation transcript:

2D Array, Nested Loops

One dimensional arrays and lists We have learned lists and some applications. For example, for a given list of numbers a_list Compute the sum Compute the average def compute_sum(a_list): sum = 0 for i in range(len(a_list)): sum += a_list[i] return sum def compute_avg(a_list): sum = compute_sum(a_list) return sum / len(a_list)

2D arrays and lists Many other applications require 2D arrays and lists For example, if we want to compute the average test scores of a class in which we have n students and k tests. The data will look something like the following. In your up-coming labs and homework you will see other applications. We usually need nested loops to handle 2D data. Name/Test Test1 Test2 Test3 Allan Johnson 88 82 91 Marco Lima 83 79 86 Jane Rubio 77 93 Maria Zhang 85 92

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

Try ref_copy.py

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 create_one_row( 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 create2d_array( width, height ): """ does just that """ x = [] # start with nothing for row_count 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! 24_create_arrays.py

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 by the day : 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 How to computer the maximum difference? 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 min_diff_so_far = 9999999 for value1 in lst1: for value2 in lst2: diff = abs(value1 - value2) if diff < min_diff_so_far: min_diff_so_far = diff return min_diff_so_far 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 How to computer the maximum difference?

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