Title: Computer Science 111
1Computer Science 111
- Fundamentals of Programming I
- Nested Loops
- Grids
- Two-Dimensional Lists
- Matrices
2Simple Loops
for i in xrange(upper) sum i
Just count through a sequence of values The
sequences are linear
for element in lyst sum element
for element in myfile sum int(element)
3Nested Loops
for row in xrange(upper1) for column in
xrange(upper2) ltdo something with row
and columngt
0
1
2
3
4
5
6
0
- Often used to process grid-like structures
- Game boards
- Bitmaps of images
- Matrices
- Maps and diagrams
1
2
3
4
5
6
4Grid Positions as (row,col) Pairs
for row in xrange(3) for col in xrange(3)
print (row, col), print
In this 3 by 3 imaginary grid, each row has 3
positions and each column has 3 positions
col0 col1 col2 (0,0) (0,1) (0,2)
row0 (1,0) (1,1) (1,2) row1 (2,0) (2,1) (2,2)
row2
5Using a while Loop
row 0 while row lt 3 col 0 while col
lt 3 print (row, col), col 1
print row 1
col0 col1 col2 (0,0) (0,1) (0,2)
row0 (1,0) (1,1) (1,2) row1 (2,0) (2,1) (2,2)
row2
6Representing a Grid as Data
- Use a list of lists
- Sometimes called a two-dimensional list or matrix
- Access each element with a pair of subscripts of
the form rowcolumn - Usually process the grid with a nested loop
7What a List of Lists Is Like
Build a 3 by 2 grid of integers (3 rows of 2
columns each)
g 6, 3, 2, 5, 8, 1
8What a List of Lists Is Like
Build a 3 by 2 grid of zeros (3 rows of 2 columns
each)
g 6, 3, 2, 5, 8, 1
Print the number of rows and columns
print len(g), len(g0)
We assume that the grid is rectangular, not ragged
9What a List of Lists Is Like
Build a 3 by 2 grid of zeros (3 rows of 2 columns
each)
g 6, 3, 2, 5, 8, 1
Print the number of rows and columns
print len(g), len(g0)
Print the contents of the grid in two-dimensional
format
for row in g for element in row
print element, print
6 3 2 5 8 1
10What a List of Lists Is Like
Build a 3 by 2 grid of zeros (3 rows of 2 columns
each)
g 6, 3, 2, 5, 8, 1
Print the number of rows and columns
print len(g), len(g0)
Or access elements in g using their index
positions
for row in xrange(len(g)) for col in
xrange(len(grow)) print growcol,
print
6 3 2 5 8 1
11What a List of Lists Is Like
Build a 3 by 2 grid of zeros (3 rows of 2 columns
each)
g 6, 3, 2, 5, 8, 1
Or use a while loop
row 0 while row lt len(g) col 0 while
col lt len(grow) print growcol,
col 1 print row 1
6 3 2 5 8 1
12A Grid Type
- Like the list type or the string type, the grid
type is actually a set of operations on a set of
data values (grids) - Define these operations as functions in a module
- Import the module to use grids
- The module hides the details of the data
structures and operations used to represent and
manipulate a grid
13The grid Modules Functions
14Using the grid Module
Create a 3 by 3 grid of ones
from grid import
g makeGrid(3, 3, 1)
g makeGrid(3, 3)
g makeGrid(3)
g makeGrid()
15Print of Rows and Columns
print getRows(g), getCols(g)
16Sum the Elements
sum 0 for row in g for element in row
sum element
17Increment the Elements
for row in xrange(getRows(g)) for col in
xrange(getCols(g)) growcol 1
18Defining makeGrid
def makeGrid(rows 3, cols 3, value 1)
g for row in xrange(rows) lyst
for col in xrange(cols)
lyst.append(value) g.append(lyst)
return g
Build and return a two-dimensional list The
outer list contains the rows (nested lists) Each
row or nested list contains the elements
19Defining getRows and getCols
def getRows(g) return len(g) def
getCols(g) return len(g0)
20Defining gridPrint
def gridPrint(g) for row in g for
element in row print element,
print
Formats data in rows and columns
21Matrix Arithmetic
- A matrix is a two-dimensional grid of numbers
- Two matrices can be added, multiplied, and so
forth - The result is a third, new matrix
22Matrix Addition
- The two matrices must be of the same order (width
and height) - Obtain the sums of each pair of values at each
position in the two matrices - Save the sums in the corresponding positions in
the result matrix
23In Python
m1 makeGrid(3, 3, 2) m2 makeGrid(3, 3,
3) sum matrixAdd(m1, m2) gridPrint(sum)
5 5 5 5 5 5 5 5 5
24Implementing matrixAdd
def matrixAdd(m1, m2) sum
makeGrid(getRows(m1), getCols(m1)) for row in
xrange(getRows(sum)) for col in
xrange(getCols(sum)) sumrowcol
m1rowcol m2rowcol return sum
25For Friday
- Read Section 7.3 on Image Processing