Class 28 — Friday April 9
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
Download (available at start of class)
- Program stripping.py
- Module go.py
- Tester program forth.py
- Module quad.py
- Tester program riga.py
Post class
- Review musings.
- Try CodingBat.
- Complete Homework 24.
Program stripping.py
- Background
- The string function
strip()
can remove more that just leading and trailing whitespace in producing its return string.
- The
strip()
function can take a string as an argument. Any leading or trailing character in the argument string is not part of the return string
- Some program runs
Enter text: abra-cadabra
Enter stripping characters: a
bra-cadabr
Enter text: abra-cadabra
Enter stripping characters: A
abra-cadabra
Enter text: abra-cadabra
Enter stripping characters: bar
-cad
Enter text: abra-cadabra
Enter stripping characters: c
abra-cadabra
Module go.py
— function ints( ns )
- Parameter
ns
is a string that composed of zero or more integer substrings and nothing else.
- The function returns a list of integers corresponding to the integer substrings in
ns
.
- FYI: The following strings do not meet the specification for the
ints()
parameterns
, as they all contain at least non-integer substring.
bad1 = "abc"
bad2 = "3 def 4 ghi"
bad3 = "3.14"
- FYI: The following the variable definitions all meet the specification for the
ints()
parameterns
, as they are composed of zero or more integer substrings and nothing else.
ns1 = " 3 "
ns2 = "12 11 -63"
ns3 = "31 415 92 653 5 9"
ns4 = " "
- The
ints()
tester when given those strings as arguments to the function should produce the following output.
ints( ns1 ): [3]
ints( ns2 ): [12, 11, -63]
ints( ns3 ): [31, 415, 92, 653, 5, 9]
ints( ns4 ): []
- Possible algorithm
- ???
Module go.py
— function parse_phone_string( pn )
- Parameter
pn
is a string representing a USA phone number with an area code but without a country code. The first three digits inpn
are the area code; the next three digits inpn
are the prefix; and the last four digits inpn
are the line number.
- The function returns a three-element integer list. The first element of the list is the
pn
area code in integer form; the middle element of the list is thepn
prefix in integer form; the last element of the list is thepn
line number in integer form
- Consider the following phone number strings
pn1 = "(201) 867-5309"
pn2 = "636-555-3226"
pn3 = "212 555 2368 (help line)"
pn4 = "888.799.9666 (customer service)"
- The
parse_phone_string()
tester when given those strings as arguments to the function should produce the following output.
parse_phone_string( pn1 ): [201, 867, 5309]
parse_phone_string( pn2 ): [636, 555, 3226]
parse_phone_string( pn3 ): [212, 555, 2368]
parse_phone_string( pn4 ): [888, 799, 9666]
- Possible algorithm
- ???
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 )
- 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 )
- 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 )
- 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 )
- 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.
🦆 © 2022 Jim Cohoon | Resources from previous semesters are available. |