Computer Science 111 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Computer Science 111

Description:

Computer Science 111 – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 26
Provided by: KenLa2
Category:
Tags: col1 | computer | science

less

Transcript and Presenter's Notes

Title: Computer Science 111


1
Computer Science 111
  • Fundamentals of Programming I
  • Nested Loops
  • Grids
  • Two-Dimensional Lists
  • Matrices

2
Simple 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)
3
Nested 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
4
Grid 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
5
Using 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
6
Representing 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

7
What 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
8
What 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
9
What 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
10
What 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
11
What 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
12
A 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

13
The grid Modules Functions
14
Using 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()
15
Print of Rows and Columns
print getRows(g), getCols(g)
16
Sum the Elements
sum 0 for row in g for element in row
sum element
17
Increment the Elements
for row in xrange(getRows(g)) for col in
xrange(getCols(g)) growcol 1
18
Defining 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
19
Defining getRows and getCols
def getRows(g) return len(g) def
getCols(g) return len(g0)
20
Defining gridPrint
def gridPrint(g) for row in g for
element in row print element,
print
Formats data in rows and columns
21
Matrix 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

22
Matrix 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

23
In 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
24
Implementing 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
25
For Friday
  • Read Section 7.3 on Image Processing
Write a Comment
User Comments (0)
About PowerShow.com