''' Purpose: do some functions produces lists from a string ''' def separate( s ) : # s is the parameter (if we pass in the argument 'apple', then we return # ['a','p','p','l','e'] ''' Returns a new list where each element in the list corresponds to a character of s ''' # .split() by default separates on whitespace! # 'apple'.split() will become ['apple'] (We're not separating on whitespace there's none!) result = [] # accumulator - we will add characters of our string s to it one by one! for ch in s: # Go through each character in string s result.append( ch ) # Add each character to our list one by one (each iteration) return result # result = result + [ch] # The + operator cannot be used to add things to a list # if you do result + ch ^ # + can be used in number addition and string concatenation # You do + when you want to glue two strings together # The reason why result + [ch] works is this is saying # make ch into a list ex. ['a'] and combine lists (result and # [ch]) but the append way is best for understanding def join( sl ) : ''' Returns a string composed of the strings in string list sl, where a blank is used to separate the strings from each other. ''' # sl is a list of strings i.e. ['wa','hoo','wa'] result = '' # accumulator - we want to build up and return a string! for s in sl: # Loop through each string in sl (the list passed in) result = result + s + " " # Update result to be the last version of result with the next # string s concatenated onto it and a space after each string s n = len( result ) # 0 to n - 1 are the first index and last index and we want # to get rid of the last space # Remember slicing s[i:j] gets me s from i to j-1 ! result = result[ 0 : n - 1 ] # gets the string from indexes 0 to n - 2 (no more space!) return result def ints( ns ) : ''' Returns list of integers corresponding to the integer substrings in ns. ''' # SUPER USEFUL I WOULD REMEMBER HOW TO DO THIS :) # We want to go from a string of numbers to a list of numbers! # "1 2 3" -> [1,2,3] result = [] # accumulator is going to be a list - build up list of numbers! list_numeric_strings = ns.split() # If ns is '1 2 3' now we have ['1','2','3'] for numeric_string in list_numeric_strings: # Go through each numeric string ^ nbr = int(numeric_string) # '1' -> 1 Convert numeric string to integer! result.append( nbr ) # Add nbr (integer version of numeric string to result) return result def parse_phone_string( pn ) : ''' Returns a three-element integer list. The first element of the list is the pn area code, the middle element of the list is the pn prefix; the last element is the pn line number ''' DIGITS = '1234567890' # All of our possible digits in a phone number # First go through and get the pn as a string of just the digits! Store it in # phone_number_string (ex. (201) 867-5309 -> 2018675309) phone_number_string = '' # Build up a string of just the numbers! for ch in pn: # pn is a string so let's go through each character in pn if ch in DIGITS: # Check if each ch is a digit phone_number_string = phone_number_string + ch # Add the number to our phone number # First get the string of the phone number, slice the pieces, and then convert to numbers. # If you converted it to a number first, you can't go back and slice the number! # How can we use the phone_number_string and slicing? area_code = phone_number_string[ 0 : 3 ] # gets s at indexes 0 to 2 prefix = phone_number_string[ 3 : 6 ] # gets s at indexes 3 to 5 line_number = phone_number_string[ 6 : ] # gets s at indexes 6 all the way to the end area_code = int( area_code ) prefix = int( prefix ) line_number = int( line_number) result = [area_code, prefix, line_number ] # Create a list of the three parts return result