Class 29 — October 29
Moving on
Working Together – On a cautionary tale – New understandings
Look both ways
Module quad.py
- Musings on problem solving
Nuance downloads
- Program cautionary.py
- Module tale.py
- Program wipe.py
- Module zero.py
Agenda
- Consider module development
- Problem solve
- Consider parameter passing nuances
- Show
if ( __name__ == '__main__' )
statement for aiding testing
Test 2
- November 13
To do
- Examine parameter passing nuances.
- Complete current homework module quad.py.
- Consider optional election forecasting homework.
- Examine next homework module aid.py.
What the heck happens (PDF, slide show)
import tale | def f( x, y ) :
city1 = 'London' | rmbr = x
city2 = 'Paris' | x = y
tale.f( city1, city2 ) | y = rmbr
print( city1, city2 ) | return
- Does the above code print
London Paris
- Or does the it print
Paris London
Module self-testing downloads
- Module im_a_module.py
- Tester im_a_tester_program.py
Tester statement if ( __name__ == '__main__' )
- Along with some built-in functions, Python provides each Python file with a built-in variable
__name__
.
- For a Python file being imported by another Python file, the variable is set to the name of imported file.
- For a Python file being run as a program, the variable is set to
'__main__'
- With this
if
statement, a module can provide self-testing if run as a program.
- Why do you care about this functionality?
- Such testing will also be part of the modules for ongoing homework and future tests
- Homework
aid.py
uses theif ( __name__ == '__main__' )
statement to support testing usingabet.py
.
if ( __name__ == '__main__' ): # __name__ is a built in python variable.
import abet # it is set to __main__ if you are running
# that module as a program
# it is set to the name of the module
# if it is being imported
abet.test_rotate()
abet.test_rotate_k_times()
abet.test_common()
Module quad.py
musings
- Functions
to_lower()
andcleanup()
seem very similar to me.
- Both functions want to return a new list, where there will be a separate entry for each string in their string list parameter
strings
.
- The new list must be accummulated.
- For each string in
strings
, a conversion is produced and the conversion is added to the accummulator.
- In WWAHD speak
Create a new empty list
For each string in
strings
do— Get a converted version of the string
— Add conversion to the new list
Hand back completed new list
- Function
unique()
also has some similarities to functionsto_lower()
andcleanup()
.
- Needs to build and return a new list.
- The new list needs to be accumulated.
- Needs to consider each string in the string list parameter
strings
for the accumulation.
- However, it does not need to do a conversion. Instead, a test needs to individually check each string in
strings
whether to add it to the accumulator.
- In WWAHD speak
Create a new empty list
For each string in
strings
do— Test whether string needs to be added the new list. If so, add string to new list
Hand back completed new list
- The job of function
canonical()
is to produce a cleaned up, lowercase version ofstrings
with no duplicates. It should be obvious that making use of functionscleanup()
,to_lower()
, andunique()
should be helpful.
- Use one of three functions to start the conversion process.
- Pass its result with a second of the functions.
- Pass that result to the third function to produce the desired canonical result.
What is left to do is figure out does it make a difference which of functions
cleanup()
,to_lower()
, andunique()
should be applied first, second, and third. I will just say not any order of the three will work in general.
Module for next class aid.py
— homework 27
Function rotate( x )
- Updates list
x
by moving the last element of listx
(if any) to the beginning of listx
. This type of list transformation is often called a circular shift.
- In carrying out its action, the function modifies the contents of the list
x
in place (e.g., by list-modification methods likepop()
,append()
, andinsert()
, or by directly modifying individual elements of listx
.
- The function returns
None
.
Function rotate_k_times( x, k )
- Updates list
x
by performingk
circular shifts.
- In carrying out its action, the function modifies the contents of the list
x
in place.
- The function returns
None
Function common( x, y )
- Returns a new list whose elements are those elements in
x
that are also iny
. The ordering of elements in the return list should reflect their ordering inx
.
- The function does not modify parameters
x
andy
in any way.
Slide show
© 2020 Jim Cohoon | Resources from previous semesters are available. |