Sequences - Bryn Mawr Computer Science

Download Report

Transcript Sequences - Bryn Mawr Computer Science

Sequences
CMSC 120: Visualizing Information
2/26/08
Step by Step
 Algorithm: a set of sequential instructions that are
followed to solve a problem
 Computer programs are algorithms that provide a
set of sequential instructions to the CPU
 CPU processes instructions one at a time, in the
order that they are received; i.e., sequentially
Step by Step
def drawStuff(P1, P2, P3, win):
# create a line
L = Line(P2, P3)
# create a circle
C = Circle(P3, 10)
C.setFill('red')
# create a rectangle
R = Rectangle(P1, P3)
R.setFill('blue')
# draw the objects
L.draw(win)
R.draw(win)
C.draw(win)
Graphics Window
Step by Step
def drawStuff(P1, P2, P3, win):
# create a line
L = Line(P2, P3)
# create a circle
C = Circle(P3, 10)
C.setFill('red')
# create a rectangle
R = Rectangle(P1, P3)
R.setFill('blue')
# draw the objects
C.draw(win)
R.draw(win)
L.draw(win)
Graphics Window
The Alternative
 Sequential Processing
 Perception
 Programming
 Parallel Processing
 Perception
 Programming
 Multiple CPUs
 Control Flow Statements
Control Flow
 Alter the direction of the flow of the code
 No longer sequential
 Loops: repetition
 Conditionals: make decisions
 Gotos: jump to another section
Repetition
 Print out the numbers from 0 to 9
>>> for i in range(10):
print i
 for-statement
 loop statement
 Repeat something a fixed number of times
for-Statements
Sets up number of times the loop is
repeated or iterated
for <variable> in <seqeunce>:
<do something>
<do something>
Body: stuff to be repeated
...
for-Statements
for <variable> in <seqeunce>:
 <variable>
is the index for the loop
 Assign successive values of <sequence>
to <variable>
 <sequence> is a list of values
 Statements in the body are executed for
each value
Repetition
 Print out the numbers from 0 to 9
>>> for i in range(10):
print i
 <sequence> is range(10)
 generates a sequence of numbers up to but not
including the specified parameter value
>>> range(5)
[0, 1, 2, 3, 4]
Repetition
 Print out the numbers from 0 to 9
>>> for i in range(10):
print i
 The index variable, i will take on the
values of 0 through 9 and for each
value, it will execute the print
statement
0
1
2
3
4
5
6
7
8
9
Lists
 Collections of information
 Any sequence of “things”
 Numbers
 Letters
 Strings
 Points
 figures
Types of Lists
 Empty List
>>> []
[]
or
>>> L = []
>>> print L
[]
 An empty list does not contain anything
Types of Lists
N = [2, 43, 6, 27]
FamousNumbers = [3.1415, 2.718, 42]
Cities = ['Philadelphia', 'Hicksville', 'Troy', 'Austin']
MyPoints = [Point(100, 42), Point(75, 64), Point(83, 310)]
MyCar = ['Mazda Protege', 1999, 'red']
 Any collection of things, even those with different
data types
Lists Operations
>>> N = [2, 43, 6, 27]
>>> len(N)
4
 len(<sequence>): takes a list and returns the
length, or the number of objects in the list
Lists Operations
>>>
>>>
>>>
[2,
N = [2, 43, 6, 27]
FamousNumbers = [3.1415, 2.718, 42]
N + FamousNumbers
43, 6, 27, 3.1415, 2.718, 42]
 Concatenate (‘+’): joins two lists together
List Operations
>>> Cities = ['Philadelphia', 'Hicksville',
'Troy', 'Austin']
>>> Cities[0]
'Philadelphia'
 Indexing: allows access individual elements in a
list by their index
 First element has an index of 0
 Last element has an index of n-1 where n is the length
of the list
Lists Operations
>>> Cities = ['Philadelphia', 'Hicksville',
'Troy', 'Austin']
>>> Cities[1:3]
['Hicksville', 'Troy']
 Slice: allows access to a sublist by a set of indices
 List[a:b] returns the elements from a to b-1
Lists Operations
>>> Cities = ['Philadelphia', 'Hicksville',
'Troy', 'Austin']
>>> 'Troy' in Cities
True
>>> 'Cairo' in Cities
False
 in: checks whether an element is in a list
Lists are Objects
>>>
>>>
>>>
[2,
N = [2, 43, 6, 27]
N.sort()
print N
6, 27, 43]
 sort: arrange elements in ascending order
Lists are Objects
>>> N = [2, 43, 6, 27]
>>> N.reverse()
>>> print N
[27, 6, 43, 2]
 reverse: reverse order of elements
Lists are Objects
>>>
>>>
>>>
[2,
N = [2, 43, 6, 27]
N.append(3)
print N
43, 6, 27, 3]
 append: add an element to the end of the list
All Lists are Sequences
 All lists can be used to perform repetitions
from graphics import *
MyPoints = [Point(100, 42), Point(75, 64), Point(83, 310)]
win = GraphWin('My Win', 400, 400)
for point in MyPoints:
c = Circle(point, 5)
c.setFill('red')
c.draw(win)
Strings
 Strings are also sequences
>>> ABC = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> len(ABC)
26
>>> for letter in ABC:
print letter
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
Lists in Pylab
 All the data we have plotted so far has been stored
as a set of lists:
Year = [1979, 1980, 1982,…]
Marijuana = [14.1, 13.3, 13.3, 12.5, …]
Lists in Pylab
 Thus, the actual syntax of the plot command is:
plot(<x-sequence>)
Plot(<x-sequence>, <y-sequence>)
where the x- and y-data are actually lists of either int or
float values
 The syntax of the mean command is:
mean(<sequence>)
 And the function calculates the mean of a list of int or
float values
Lists in Pylab
Lists in Pylab
 Ticks:
Labels = ['Marijuana', 'Hallucinogens',
'Cocaine', 'Heroin', 'Cigarettes',
'Alcohol']
xticks(arange(6), Labels)
xticks(arange(<num-ticks>), <label-sequence>)
Lists in Pylab