Class 29 — Wednesday April 1
Functionization
Social distancing — Does not mean we cannot share — Love, hopes, and friendship
It's April Fools Day — I am sorry for past pranks — And those yet to come
Look both ways
Zoom
- Link to live meeting MWF @ 2 PM.
Download now
- Module go.py
- Tester program forth.py
Download when asked
- Module quad.py
- Tester program riga.py
The teaching assistant staff
- I strongly appreciate and hope you do so too, the efforts of the teaching assistants in making CS 1112 work.
- Not just to Nadia for staffing our virtual lectern and Ryan for keeping me aware of the chat questions and comments you communicate during out meetings, but also to Ann, Cecilia, Hallie, Hannah, Hunter, Jacob, Jeanine, Rachel, Sophia, and Xavier for their great efforts in answering chat questions, holding office hours, and monitoring Piazza.
All members of the CS 1112 community — students and staff
- I thank all of you for giving CS 1112 your attention in this unsettled time. I hope you, and your family and friends are doing well. I want to believe in the power of collective thoughts and prayers, and you are in mine.
Agenda
- Being functional
- Better able to convert problem specifications dealing with strings into working functions
To Do
- Look over your artifacts.
- Complete Homework 26
- Preview Homework 27
- Continue to try CodingBat.
Some scary datasets
Test 2
- Will be April 20. The final will be optional. The grading percentages will be updated to
- Homework: 20%
- Test 1: 35%
- Test 2: 35%
- Test 3: 10%
Function separate( s )
- Parameter
s
is a string.
- The function returns a new list where each element in the list corresponds to a character of
s
.
- Suppose
s1 = "apple"
s2 = "banana"
s3 = "1"
s4 = ""
- The
separate()
tester when given those strings as arguments to the function should produce the following output.
separate( s1 ): ['a', 'p', 'p', 'l', 'e']
separate( s2 ): ['b', 'a', 'n', 'a', 'n', 'a']
separate( s3 ): ['1']
separate( s4 ): []
Function join( sl )
- Parameter
sl
is a list of strings.
- Returns a string composed of the strings in string list
sl
, where a blank is used to separate the strings from each other.
- Suppose
sl1 = ['a', 'p', 'p', 'l', 'e']
sl2 = [ 'wa', 'hoo', 'wa ' ]
- The
join()
tester when given those lists as arguments to the function should produce the following output.
join( sl1 ): 'a p p l e'
join( sl2 ): 'wa hoo wa '
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.
b1 = "abc"
b2 = "3 def 4 ghi"
b3 = "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 ): []
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"
pn5 = "8602941986"
pn3 = "21 25 55 2368"
pn4 = "888.799.9666"
- 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 ): [860, 294, 1986]
parse_phone_string( pn3 ): [212, 555, 2368]
parse_phone_string( pn4 ): [888, 799, 9666]
Suppose the following constants are in effect
PUNCTUATION = '''!"#$%&'()*+,-./:;<=>?@[]^_`{|}~'''
WHITE_SPACE = ' \t\n\r\v\f'
EXTRANEOUS = PUNCTUATION + WHITE_SPACE
Suppose the following test case variable definitions are in effect
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 canonical( strings )
- Returns a new list without duplicates whose elements are lower case versions of those in
strings
with leading and trailing extraneous characters stripped.
- 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']
- To carry out this task does it matter the order that we do things? In other words,
- Does it make a difference which of the three activites we do first: removing duplicates, converting to lower case, or stripping?
Function 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 forlower()
.
lower( test1 ) = ['abcde', 'abcde', '!"#x$&)|}~', '- x -_', 'aacc', ' ', 'x\n']
lower( test2 ) = ['\tx x']
lower( test3 ) = []
lower( test4 ) = ['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a']
Function strip( strings )
- Returns a new list whose elements correspond to the elements in
strings
with leading and trailing extraneous characters (punctuation or whitespace) removed.
- Given the above definitions of
test1
,test2
,test3
, andtest4
, the following is the expected invocation results forstrip()
.
strip( test1 ) = ['AbCdE', 'ABCDE', 'x', 'x', 'AAcc', '', 'x']
strip( test2 ) = ['x X']
strip( test3 ) = []
strip( test4 ) = ['a', 'B', 'r', 'A', 'c', 'a', 'D', 'a', 'B', 'r', 'a']
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']
© 2020 Jim Cohoon | Resources from previous semesters are available. |