Lecture 13 - Exercises

13A - Strings and Tuples are Sequences

  1. Consider the following function:

    def pr(a):
        for c in a:
            print(c, c, end=" ")

    The result of calling this function with a particular string a is C C S S C C E E Y Y E E. What was a?

  2. What does the following program print?

    def fl(tu):
         r = ""
         for v in tu:
             r += str(v + v)
         return r
    print(fl(("A", "C", 3)))

13B - Indexing and Slicing

  1. Suppose s = "Winter is cold." Which of the following evaluates to "r"?

    1. s[5]
    2. s(5)
    3. s[6]
    4. s(6)
  2. Suppose last_name = "Wehrwein". Which of the following evaluates to "in"? List all that apply.

    1. last_name[7:8]
    2. last_name[-2:8]
    3. last_name[6:-1]
    4. last_name[-3:]
    5. last_name[6:]
  3. Which of the following evaluates to the last character of a string s?

    1. s[len(s) - 1]
    2. s[len(s)]
    3. s[len(s) + 1]
    4. s[42]
  4. Suppose last_name = "wehrwein". For which of the following a and b will last_name[a] == last_name[b] evaluate to True? List all that apply.

    1. a = 1, b = 5
    2. a = 1, b = 7
    3. a = 8, b = -4
    4. a = -2, b = -6
  5. What does the following code print?

    m = (4, 1, 2, 2, 2)
    s = "Scott"
    for i in range(len(s)):
        print(s[i] * m[i], end="")
  6. Consider the following function:

    def flop(value, number):
        output = ""
        for i in range(number, 0, -1):
            output = output + value[i-1]
    
        for i in range(number, len(value)):
            output = output + value[i]
        return output

    Suppose a contains an integer. Which of the following are possible return values of the following call:

    flop("no time", a)

    List all that apply:

    1. mit one
    2. nomite
    3. t onime
    4. on time
    5. emit on
    6. timeno time