Class 24 — Wednesday October 20
Module implementation and usage
Practice makes perfect — Is more than a platitude — Take it to your heart
Look both ways
Agenda
- Function-based chrestomathics
Reddit video
Slides
Downloads
- Module olio.py
- Demonstrates programmer-defined functions
- None of the functions should get input
- Tester program use_olio.py
- Performs basic testing of module
olio.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.
- Complete Homework 20
- Preview next class activities activities
Module olio
Function voting_age()
- Does not take any parameters. Returns an integer indicating how old you need to be to vote. For example, the following code segment initializes both x and y to the integer
18
.
x = olio.voting_age()
y = olio.voting_age()
Function has_blanks( s )
- Has one parameter
s
. Returns whethers
contains at least one blank character; i.e., returnsTrue
ifs
contains a blank, and returnsFalse
otherwise. For example, the following code segment initializesb1
andb2
toTrue
andFalse
respectively.
x = 'CS 1112'
y = 'the_aardvark_said_arf_arf'
b1 = olio.has_blanks( x )
b2 = olio.has_blanks( y )
Function great_seal()
- Does not have any parameters and does not return a value. The function prints text from the Great Seal of the United States, i.e., E Pluribus Unum.
- The code segment
olio.great_seal( )
print()
olio.great_seal( )
print()
produces as output
E Pluribus Unum
E Pluribus Unum
Function a_ing( n )
- Has one parameter
n
. The function does not perform a return, instead it printsn
lines of output.
- The first line prints a single
'a'
, the second line prints'aa'
, the third line prints'aaa'
, and so on. The string operator*
should prove useful.
- For example usage, the code segment
olio.a_ing( 5 )
print()
olio.a_ing( 1 )
print()
olio.a_ing( 3 )
produces as output
a
aa
aaa
aaaa
aaaaa
a
a
aa
aaa
Module primal
Purpose
- Report on factorization and primality
Terminology
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
Observation
- The
%
is the Python remainder operator.
Function is_factor( x, y )
- Parameters
x
andy
are integers. The function returns whethery
is a factor ofx
.
-
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