Class 28 — Friday, October 29
Functionization
Social distancing — Does not mean we cannot share — Love, hopes, and friendship
Look both ways
Agenda
- Being functional
- Convert specifications dealing with strings into working functions
- Module quad.py
- Tester program riga.py
Post class
- Review musings.
- Try CodingBat.
- Complete Homework.
Module quad.py
- Defines four functions for cleaning up lists of strings. Program riga.py performs simple testing of quad.py.
- Observation: none of the function implementations need nested
for
-loops.
- Requirement: none of the functions are allowed to modify their parameters.
Suppose the following definitions are in effect below
test1 = ['AbCdE', 'ABCDE', '!"#\'x$&)|}~', '- x -_', 'AAcc', ' ', 'x\n']
test2 = ['\tx X']
test3 = []
test4 = ['a', 'B', 'r', 'A', 'c', 'a', 'D', 'a', 'B', 'r', 'a']
Function to_lower( strings )
- Parameter
strings
is a list of strings.
- Returns a new list whose elements are lower case versions of those in
strings
.
- Given the above definitions of
test1
,test2
,test3
, andtest4
, the following is the expected invocation results forto_lower()
.
to_lower( test1 ) = ['abcde', 'abcde', '!"#\'x$&)|}~', '- x -_', 'aacc', ' ', 'x\n']
to_lower( test2 ) = ['\tx x']
to_lower( test3 ) = []
to_lower( test4 ) = ['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
- Observation: an accumulator is needed. Lower-case versions of elements of
strings
need to be added to the accumulator one-at-a-time.
Function cleanup( strings )
- Parameter
strings
is a list of strings.
- Returns a new list whose elements correspond to the elements in
strings
with leading and trailing extraneous characters (punctuation or whitespace) removed.
- The following constants are defined for the function
PUNCTUATION = '''!"#$%&'()*+,-./:;<=>?@[]^_`{|}~'''
WHITE_SPACE = ' \t\n\r\v\f'
EXTRANEOUS = PUNCTUATION + WHITE_SPACE
- Given the above definitions of
test1
,test2
,test3
, andtest4
, the following is the expected invocation results forcleanup()
.
cleanup( test1 ) = ['AbCdE', 'ABCDE', 'x', 'x', 'AAcc', '', 'x']
cleanup( test2 ) = ['x X']
cleanup( test3 ) = []
cleanup( test4 ) = ['a', 'B', 'r', 'A', 'c', 'a', 'D', 'a', 'B', 'r', 'a']
- Observation: an accumulator is needed. Stripped versions of elements of
strings
need to be added to the accumulator one-at-a-time.
Function unique( strings )
- Parameter
strings
is a list of strings.
- Returns a new version of
strings
without any duplicate values.
- Given the above definitions of
test1
,test2
,test3
, andtest4
, the following is the expected invocation results forunique()
.
unique( test1 ) = ['AbCdE', 'ABCDE', '!"#\'x$&)|}~', '- x -_', 'AAcc', ' ', 'x\n']
unique( test2 ) = ['\tx X']
unique( test3 ) = []
unique( test4 ) = ['a', 'B', 'r', 'A', 'c', 'D']
- Observation: an accumulator is needed. The elements of
strings
need to be added to the accumulator one-at-a-time if they are not already there.
Function canonical( strings )
- Parameter
strings
is a list of strings.
- Returns a new list cleaned up lower case version of
strings
without duplicates. It should be obvious that making use of functionscleanup()
,to_lower()
, andunique()
should be helpful.
- To ensure that you do make use of functions
cleanup()
,to_lower()
, andunique()
should be helpful, no loops are allowed incanonical()
code.
- Because functions
to_lower()
andcleanup()
both have the potential for returns lists with duplicates, functioncanonical()
needs to make use ofunique()
after usingto_lower()
andcleanup()
.
- Given the above definitions of
test1
,test2
,test3
, andtest4
, the following is the expected invocation results forcanonical()
.
canonical( test1 ) = ['abcde', 'x', 'aacc', '']
canonical( test2 ) = ['x x']
canonical( test3 ) = []
canonical( test4 ) = ['a', 'b', 'r', 'c', 'd']
Module quad.py
musings
- Functions
to_lower()
andcleanup()
seem similar.
- 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 lists 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()
has 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, it 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 to a second of the functions.
- Pass that result to the third function to produce the desired canonical result.
Because both
cleanup()
andto_lower()
have the potential to return duplicates,unique()
needs to be the third function. It is up you to determine whether one of the other two needs to be first.