Class 26 — Wednesday March 25
Module implementation and usage
Practice makes perfect — Is more than a platitude — Take it to your heart
Look both ways
Agenda
- Function-based chrestomathics
Buddying
- A possible form for finding a classmate buddy
Slides
Downloads
- Module uva.py
- Implement a worthwhile module with several functions based on your suggestions
- Tester program rotunda.py
- Needs to be augmented based on your ideas for your ideas on
uva.py
- Module primal.py
- Defines three functions relating to factorization and primality.
- None of the functions should get input or print output.
- Tester program factoid.py
- Performs basic testing of module
primal.py
To do
- Review artifacts and prior function materials.
- Watch the recording of today's class.
- Complete Homework 23
- Preview next class activities activities
Module uva
: freestyling
- Implement a worthwhile module with several functions based on your suggestions
- Some suggestions already received
- Function
credits_to_go()
- Has one integer parameter
n
, which is the number of credits earned so far.
- Returns the remaining number of credits needed to graduate.
- Assumption: CLAS student so 120 credits is amount needed.
- Function
how_old_is_UVA()
- Has no parameters
- Returns the number of years since UVA was founded.
- Not sure want to do this one — sticky wicket
- Function
grade_as_percent( score, total )
- Has two parameters numeric parameters
score
andtotal
, wherescore
is points earned andtotal
is the maximum number of points possible.
- Returns the percentage grade given
score
andtotal
. As student-specified, the return should include a%
at the end of the percentage. For example, the following code segment
s = 17
t = 20
pct = uva.grade_as_percent( s, t )
print( "grade_as_percent(", s, ",", t, "):", pct )
should print
grade_as_percent( 17, 20 ): 85.0%
Module primal
: to be submitted to document your participation
- Concerned with integer factorization and primality
y
is a factor ofx
, if the remainder ofx
/
y
is 0.
x
is prime, if its only factors are 1 and itself.
x
andy
are relatively prime, ify
is not a factor ofx
andx
is not a factor ofy
-
Function
is_factor( x, y )
- Parameters
x
andy
are integers. The function returns whethery
is a factor ofx
.
Observation
- 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.
Questions
- Could
is_factor()
help?
- What are the possible range of values do we need check for being possible factors of
x
.
- Does the function need a loop?
-
Function
are_relative_primes( x, y )
- Parameters
x
andy
are integers. The function returns whetherx
andy
are relatively prime.
Questions
- Could
is_prime()
help?
- Could
is_factor()
help?
- Do we need both of them?
- Does the function need a loop?
Slide show
© 2020 Jim Cohoon | Resources from previous semesters are available. |