Class 25 — Friday October 26
Reaching an understanding with functions
Functional living — Problem-solving strategy — Embrace this future
Look both ways
Agenda
- Another try at functions
Last chance
- Mentoring support
Downloads
- Module primal.py
- Program / tester factoid.py
-
Slides: PDF
To do
- Review artifacts and prior function materials.
- Watch the recording of today's class.
- Complete the homework due Tuesday.
Example de jeur — module primal
- Definition:
x
is a factor ofy
if the remainder ofy
/
x
is 0.
-
Module primal.py
Function
is_factor( x, y )
- Parameters
x
andy
are integers. The function returns whethery
is a factor ofx
.
Observations
y
is a factor ofx
, if the remainder ofx
divided byy
is zero.
- The
%
is the Python remainder operator.
Possible algorithm
- Determine remainder of
x
divided byy
- By looking at the remainder we can tell whether to return
True
orFalse
Function
is_prime( x )
- Parameter
x
is an integer. The function returns whetherx
is prime; i.e., its only factors are 1 and itself
Question
- Could
is_factor()
help?
Function
are_relative_primes( x, y )
- Parameters
x
andy
are integers. The function returns whetherx
andy
are relatively prime; i.e., whethery
is not a factor ofx
and vice-versa.
Questions
- Could
is_prime()
help?
- Could
is_factor()
help?
© 2019 Jim Cohoon | Resources from previous semesters are available. |