Lab 12: Part 0: Warm up

Paper worksheet

Click here for worksheet

Part 0. chop

def chop(num):
    if num > 0:
       print(num)
       chop(num//2)  # integer division

Given the function chop above, predict what is printed:

>>> chop(0)

>>> chop(6)

>>> chop(15)

Make your predictions and discuss with your partner before running the code.

Part 1. fade

def fade(s):
    if len(s) > 0:
       print(s)
       fade(s[1:])

Given the function fade above, predict what is printed:

>>> fade('')

>>> fade('hat')

>>> fade('scarf')

Make your predictions and discuss with your partner before running the code.

Part 2. grow

def grow(s):
    if len(s) > 0:
       grow(s[1:])
       print(s)

Given the function grow above, predict what is printed:

>>> grow('')

>>> grow('hat')

>>> grow('scarf')

Make your predictions and discuss with your partner before running the code.

[OPTIONAL] Part 3. both

def both(s):
    if len(s) > 0:
       print(s)
       both(s[1:])
       print(s)

Given the function both above, predict what is printed:

>>> both('hat')

Table of Contents