Assignment 25 — function implementation that manipulate lists
Due November 1
Module quad.py
- Will be made available during class
- 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 should modify the parameters.
- Recommend you review
- String module documentation
- List module documentation
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 without duplicates whose elements are lower case versions of those in
strings
with leading and trailing extraneous characters removed.
- Because both functions
to_lower()
andcleanup()
have the potential for returns lists with duplicates, functioncanonical()
needs to make useunique()
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']
© 2020 Jim Cohoon | Resources from previous semesters are available. |