''' Purpose: do some functions produces lists from a string ''' def separate( s ) : ''' Returns a new list where each element in the list corresponds to a character of s ''' # words = list(s) # Casting the string to a list (makes a list of characters from string s) # return words # Accumulator Way: new_list = [] for ch in s: # Loop through each character in the string s new_list.append( ch ) # Add each character to our list accumulator new_list return new_list # Return the list accumulator new_list we built up by adding each character to it # Great problem solving pattern for function problems where we "return a new list" * KEY WORDING # ACCUMULATION PATTERN # new_list = [] # Loop through a sequence # for ... in ... (usually sequence passed in): # new_list.append( what we wanna add ) # return new_list def ints( ns ) : ''' Returns list of integers corresponding to the integer substrings in ns. ''' # Accumulator result = [] # same as doing new_list = [] list_numeric_strings = ns.split() # Break up the string of numbers into a list of numeric strings for numeric_string in list_numeric_strings: # Loop through each numeric string in the list of numeric strings number = int( numeric_string ) # Convert each numeric_string to an integer result.append( number ) # Add each integer version of the numeric strings to the result list accumulator return result # Return final list of numbers from that string of numeric strings # Ask yourself what we're returning and work up to building up and creating that result # if we are returning a new list! ^ def parse_phone_string( pn ) : # String of a phone number ''' Returns a three-element integer list for string phone # pn: 1st element: pn area code 2nd element: pn prefix 3rd element: pn line number ''' # Get the digits from the string and then produce a list of the integers of the # area code, prefix, and line number digits = '0123456789' # Define a string of digits (the numbers we want out of the string pn) numbers = '' # Accumulator of only the numbers from the pn passed in for ch in pn: # Loop through pn string passed in if ( ch in digits ): # If the character is a number (if it's in the digits string) numbers = numbers + ch # Add it to our numbers string # print( 'numbers =', numbers ) # Break up the string numbers into the three parts of a phone number using slicing (getting characters in the # string at particular indices) area_code = numbers[ 0: 3 ] # numbers string at indices 0,1,2 (first three) prefix = numbers[ 3: 6 ] # numbers string at indices 3,4,5 (second three) line_number = numbers[ 6: 10 ] # numbers string at indices 6,7,8,9 (last four) # Alternate way that makes use of our function ints() above that converts a string of numbers to a list of numbers number = area_code + ' ' + prefix + ' ' + line_number result = ints( number ) # Use the function ints we defined above in the module return result # Another way of getting the number versions of the three parts without the ints() function: # Convert each string portion to an integer # area_code = int( area_code ) # prefix = int( prefix ) # line_number = int( line_number ) # # List of the three integer parts # result = [ area_code , prefix , line_number ] # return result pass