Section #3 Solutions

July 12, 2021


Written by Juliette Woodrow, Anna Mistele, John Dalloul, and Parth Sarin


String Slicing

  1. s[1:6]
  2. s[:2] or s[0:2]
  3. s[6:9]
  4. s[6:] or s[6:10]
  5. s[6] or s[6:7]
  6. s[:] or s[0:10] (or just s)
  7. s.upper()
  8. s.lower()
  9. s[-1]
  10. s[-3:]

String Searching

                    
def defront(s):
    if len(s) >= 2:
        return s[2:]
    return s

def x_end(s):
    found = s.find('x')
    if found != -1:
        return s[found:]
    return ''

def is_valid_password(password):
    exclamation_index = password.find("!")
    # check for presence of !
    if exclamation_index == -1:
        return False
    and_index = password.find("&")
    # check for presence of &
    if and_index == -1:
        return False
    # check for ordering of ! and &
    if and_index < exclamation_index:
        return False
    # check for number of characters
    if exclamation_index <= (len(password) - and_index - 1):
        return False
    # if checks don't return false, password checks out
    return True

def at_word(s):
    at1 = s.find('@')
    if at1 != -1:
        at2 = s.find('@', at1 + 1)
        if at2 != -1:
            return s[at1 + 1:at2]
    return ''

                    
                

String Construction

                
def make_gerund(s):
    """This function adds 'ing' to the end of the given string s and returns this new word. If the given world already 
    ends in 'ing' the function adds an 'ly' to the end of s instead before returning.
    >>> make_gerund('ringing')
    'ringly'
    >>> make_gerund('run')
    'runing'
    >>> make_gerund('')
    'ing'
    >>> make_gerund('ing')
    'ly'
    """
    #if it already ends in ing, add an 'ly' instead 
    if len(s) >= 3 and s[len(s)-3:] == 'ing':
        s = s[0:len(s)-3] + 'ly'
    else:
        s = s + 'ing'

    return s

def put_in_middle(outer, inner):
    """This function inserts the string inner into the middle of the string outer and returns this new value
    >>> put_in_middle('Absolutely', 'freaking')
    'Absolfreakingutely'

    >>> put_in_middle('ss', 'mile')
    'smiles'

    >>> put_in_middle('hit', 'obb')
    'hobbit'
        """

    middle = len(outer) // 2
    return outer[0:middle] + inner + outer[middle:]



                
            

Word Puzzle

            
ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'




def is_stacatto(word):
    """
    Returns whether a word is a stacatto word, i.e., whether the letters in
    even positions are vowels.

    Arguments:
        word -- The word to check

    >>> is_stacatto('AUTOMATIC')
    True
    >>> is_stacatto('POPULATE')
    True
    >>> is_stacatto('')
    True
    >>> is_stacatto('PYTHON')
    False
    >>> is_stacatto('SPAGHETTI')
    False
    """
    VOWELS = 'AEIOUY'
    for i in range(len(word)):
        if i % 2 == 1:
            even_letter = word[i]
            if not even_letter in VOWELS:
                return False # we've found an even letter that isn't a vowel,
                             # so we can return immediately.

    return True
            
        

Fun with Lists


def append_evens(n):
    result = []
    for i in reversed(range(n+1)):
        if i % 2 == 0:
            result.append(i)
    return result

def all_substrings(s):
    result = []
    for i in range(len(s)):
        for j in range(i, len(s)):
            result.append(s[i: j+1])
    return result

© Stanford 2020 | CS106A has been developed over decades by many talented teachers. Website designed by Chris Piech.