Class 28 — Friday April 9

Functionization

Social distancing — Does not mean we cannot share — Love, hopes, and friendship


Look both ways


Agenda


Download (available at start of class)




Post class


Program stripping.py

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 )

bad1 = "abc"

bad2 = "3 def 4 ghi"

bad3 = "3.14"

ns1 = " 3 "

ns2 = "12 11 -63"

ns3 = "31 415 92 653 5 9"

ns4 = " "

ints( ns1 ): [3]

ints( ns2 ): [12, 11, -63]

ints( ns3 ): [31, 415, 92, 653, 5, 9]

ints( ns4 ): []


Module go.py — function parse_phone_string( pn )

pn1 = "(201) 867-5309"

pn2 = "636-555-3226"

pn3 = "212 555 2368 (help line)"

pn4 = "888.799.9666 (customer service)"

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]


Module quad.py


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 )

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']


Function cleanup( strings )

PUNCTUATION = '''!"#$%&'()*+,-./:;<=>?@[]^_`{|}~'''

WHITE_SPACE = ' \t\n\r\v\f'

EXTRANEOUS = PUNCTUATION + WHITE_SPACE

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']


Function unique( strings )

unique( test1 ) = ['AbCdE', 'ABCDE', '!"#\'x$&)|}~', '- x -_', 'AAcc', ' ', 'x\n']

unique( test2 ) = ['\tx X']

unique( test3 ) = []

unique( test4 ) = ['a', 'B', 'r', 'A', 'c', 'D']


Function canonical( strings )

canonical( test1 ) = ['abcde', 'x', 'aacc', '']

canonical( test2 ) = ['x x']

canonical( test3 ) = []

canonical( test4 ) = ['a', 'b', 'r', 'c', 'd']




Maria Saal Dom Grabbaurelief Reisewagen in die Unterwelt



Module quad.py musings

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

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

Because both cleanup() and to_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.