iq.lab
Python starts when a code cell comes near or you run one
01 Python, one step at a time

Checkpoint

Python, one step at a time

Twelve questions from the whole module, on inputs you have not seen. Most aim at a classic slip: a second name for one list, a tie, a return in the wrong place, a guard on the wrong side, a bug far from the line its traceback names. Work each one on paper before you answer, with a small table of names, the way you would in a round with no run button. To pass you need 80%, which is 10 of the 12.

  1. Question 1 of 12.

    Type exactly what this prints. A list prints in square brackets, like [1, 2].

    a = [2, 4, 6]
    b = a
    c = a[:]
    b[0] = 9
    a = [1]
    print(a, b, c)
    
  2. Question 2 of 12.

    A job ran for 200 seconds. Type exactly what this prints, one line per print.

    seconds = 200
    print(seconds // 60, seconds % 60)
    print(seconds / 100, seconds // 100)
    
  3. Question 3 of 12.

    This is the busiest-day walk with one character changed: >= instead of >. What does it print? Type a whole number.

    orders = [12, 30, 7, 30]
    best_day = 0
    for i in range(len(orders)):
        if orders[i] >= orders[best_day]:
            best_day = i
    print(best_day)
    best_day

    Type a number: 12, -1, 0.5 and 3/4 all work.

  4. Question 4 of 12.

    How many times does the body of this while loop run? Type a whole number.

    n = 4071
    digits = 0
    while n > 0:
        n = n // 10
        digits += 1
    passes

    Type a number: 12, -1, 0.5 and 3/4 all work.

  5. Question 5 of 12.

    find should return the position of target in nums, or -1 if it is not there. What does find([5, 8, 2], 8) return?

    def find(nums, target):
        for i in range(len(nums)):
            if nums[i] == target:
                return i
            else:
                return -1
  6. Question 6 of 12.

    With nums = [4, 9], which of these lines raise an IndexError? Choose every line that does.

    Choose every option that applies.

  7. Question 7 of 12.

    Type exactly what this prints.

    nums = [3, 1, 2]
    a = sorted(nums)
    b = nums
    nums.sort()
    print(a, b, nums.sort())
    
  8. Question 8 of 12.

    The string below starts with two spaces, has two spaces after ETL, one between runs and nightly, and one at the end. Type exactly what this prints.

    line = "  ETL  runs nightly "
    print(len(line.split()), len(line.split(" ")))
    print("_".join(line.split()))
    
  9. Question 9 of 12.

    What is counts[13] after this runs? Type a whole number.

    counts = [0] * 26
    for c in "banana":
        counts[ord(c) - ord("a")] += 1
    counts[13]

    Type a number: 12, -1, 0.5 and 3/4 all work.

  10. Question 10 of 12.

    Type exactly what this prints.

    row = [0, 0]
    grid = [row] * 3
    grid[1][0] = 7
    row[1] = 4
    print(grid)
    
  11. Question 11 of 12.

    first_word should return the first word in lowercase, so this program should print load. Instead it stops with the traceback below (file path shortened). Where is the bug?

    def clean(words):
        out = []
        for w in words:
            out.append(w.lower())
    
    def first_word(words):
        cleaned = clean(words)
        return cleaned[0]
    
    print(first_word(["Load", "Table"]))
    Traceback (most recent call last):
      File "solution.py", line 10, in <module>
        print(first_word(["Load", "Table"]))
              ~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
      File "solution.py", line 8, in first_word
        return cleaned[0]
               ~~~~~~~^^^
    TypeError: 'NoneType' object is not subscriptable
    
  12. Question 12 of 12.

    Run the tests before you submit. This question counts as correct if your latest run passes every check.

    Code itThe busiest day, by name

    Write busiest(days, orders). days holds day names and orders holds each day's order count, in the same order. Return a tuple (day, count) for the day with the most orders. On a tie, return the earliest of the tied days. If there are no days, return None. For example, busiest(["mon", "tue", "wed"], [12, 30, 7]) is ("tue", 30).

    ⌘+Enter runsPython starts when a code cell comes near or you run one
    Write your code where the starter says raise NotImplementedError, then press Run tests. Each check says what it expects.
0 of 12 answered. Unanswered questions count as wrong.