Class 28 — Wednesday October 28
Functionization
Social distancing — Does not mean we cannot share — Love, hopes, and friendship
I have a program — That will correctly predict — Election Winner
Look both ways
Agenda
- Being functional
- Convert specifications dealing with strings into working functions
Download at start of class
- Module go.py
- Tester program forth.py
Post class
- Look over your artifacts.
- Continue to try CodingBat.
- Consider signing up for Peer Mentoring
Homework
- Implement four functions manipulating lists of strings
- Predicting the 2020 US Presidential Election.
- Optional — no help from instructor or teaching assistants allowed.
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 ): []
- Possible algorithm
- ???
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
- ???
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
- ???
© 2020 Jim Cohoon | Resources from previous semesters are available. |