Class 32 — Friday November 7
Chrestomatics and patterning
The end is coming — Sooner than we want it to — Enjoy the moment
Election results — Will be coming anytime now — Which program is correct
Look both ways
Agenda
- Problem solving
- Image manipulation
- Admire past artistry TA favorites
- Prepare for image manipulation
To do
- Consider practice modules
- Homework 28: image drawing (
artistry.py
)
- Homework 29: problem solving (
tf.py
)
Downloads
- Practice module luna.py
- Practice module calc.py
- Practice module sigma.py
Module luna.py
- Defines a function
h()
. The function has a single numeric parameterx
. The function returns the number of hours it takes to get the moon while traveling at a speed ofx
miles per hour. For your information:
elapsed time = distance / speed
- For your convenience the module defines the constant
DISTANCE_IN_MILES_TO_MOON = 238900.0
- Observation: the function makes a straight-forward calculation and returns the result.
- The output of its built-in testing should be
h( 119.45 ) = 2000.0
h( 597.25 ) = 400.0
Module calc.py
- Defines a function
e()
. The function has three parametersx
,y
, ands
. Parametersx
andy
are decimals; parameters
is a string. Ifs
is either'+'
,'-'
,'*'
, or'/
', the function returns respectivelyx + y
,x - y
,x * y
, orx / y
. Otherwise, the functionsNone
.
- Observation: an
if-elif-...else
structure is needed to determine which of five possible cases applies based on parameters
. Knowing which case applies, allows a straight-forward calculation and then the immediate return of the calculation
- The output of its built-in testing should be
19.5 + 5.25 = 24.75
12.5 - 6.5 = 6.0
12.5 * 4.5 = 56.25
10.0 / 2.25 = 4.444444444444445
1.0 @ 5.0 = None
Module sigma.py
- Defines a function
s()
. The function has one parameterd
. Parameterd
is an already initialized integer dataset; that is, it is a list of integer lists. The function returns the sum of the dataset values. The function does not change its list parameter.
- Observation: built-in function
sum()
cannot handle a dataset. It can only handle a plain list.
- Observation: an accumulator is needed to store the sum of the dataset values.
- The built-in testing makes use of the following lists.
d1 = [ [ 0 ], [ 1, 2 ], [ 1, 2, 3 ], [ 0 ] ]
d2 = [ [ 1, 0, 1, 2, 2 ], [ 3, 0, 1, 1, 1, 0 ], [ 2 ], [ 0, 0, 1 ] ]
d3 = [ [ 3, 0, 3], [ 3, 0, 3, 0, 1], [ 1, 0, 2 ] ]
d4 = [ ]
- The output of its built-in testing should be
s( d1 ) = 9
s( d2 ) = 15
s( d3 ) = 16
s( d4 ) = 0
© 2020 Jim Cohoon | Resources from previous semesters are available. |