## Class 28 — Wednesday October 28

#### Functionization

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

### Check out Homework 25 ### Agenda

• Being functional
• Better able to convert problem specifications dealing with strings into working functions

### 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()` parameter `ns`, as they all contain at least non-integer substring.

bad2 = "3 def 4 ghi"

• FYI: The following the variable definitions all meet the specification for the `ints()` parameter `ns`, 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 ): 

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 in `pn` are the area code; the next three digits in `pn` are the prefix; and the last four digits in `pn` 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 the `pn` prefix in integer form; the last element of the list is the `pn` line number in integer form
• Consider the following phone number strings

pn1 = "(201) 867-5309"

pn2 = "860+294+1986"

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]

• Possible algorithm
• ???

 © 2020 Jim Cohoon Resources from previous semesters are available.