Last Time…

Played with lists and strings. Learned about slicing data.

pi = [3, 1, 4, 1, 5, 9, 2, 6, 5]

# Basic slicing
print len(pi)
print pi[2:5]
print pi[::-1]

# Ohh!  Something new...
print 0 in pi
print 6 in pi
print "cat" in "Dogs are cooler than cats"
print "dog" in "Dogs are cooler than cats"

Two important lessons to start out the day.

import random

# Remember kids, random numbers are cool and ctrl-c is your friend.
while True:
    number = random.choice(range(0, 10))
    print "The magic number is", number

Laying Down a Few Guidelines

Functions can be super duper, but they can also be a super pain. Here are a few guidelines to help push those functions away from pain and towards duper.

  • Use meaningful function and variable names.

  • Include a docstring describing what your function does, what it takes as inputs, and what it returns.

  • Testing rocks! Debugging sucks. (Although testing is a topic for another day.)

  • Functions with text spanning multiple pages are probably too long.

  • Functions with characters sitting out past the 80 column mark are probably too wide.

A Minimal Example

Write a function to recursively find the minimum value in a list. Recall that a base case is very important when writing a recursive function.

def verbose_find_min(values, min=None):
    """Return the minimum value from the list values."""
    if len(values) == 0:
        # Base cases.  If there are no elements in values then return
        # the current value of min.
        return min
    else:
        if not min or min > values[0]:
            # Update min if value[0] is smaller.  Assume anything is
            # smaller than an undefined min value.
            min = values[0]
        # Use recursion to find the minimum value in the rest of the
        # list.
        rest = values[1:]
        best_min = verbose_find_min(rest, min)
        return best_min

We can make that a bit more compact if desired.

def find_min(values, min=None):
    """Return the minimum value from the list values."""
    if len(values) == 0:
        # Base cases.  If there are no elements in values then return
        # the current value of min.
        return min
    else:
        if not min or min > values[0]:
            # Update min if value[0] is smaller.  Assume anything is
            # smaller than an undefined min value.
            min = values[0]
        return find_min(values[1:], min)

Math math math…

All of the following must use a recursive solution.

  • Write a function to find the length of a list. You may NOT use the len() function. Called using length(list).

  • Write a function to find the maximum element in a list using recursion. You may not use the max() function. Called using maximum(list).

  • Write a function that returns b raised to the power p. You may assume that p is non-negative. Called using power(b, p).

Now onto a function that does not use recursion.

  • Write a function that linearly interpolates between two values by a given amount. Note that you’ll need to understand the math before you can write the function.

    • interpolate(4, 7, 0) returns 4.

    • interpolate(4, 7, 1) returns 7.

    • interpolate(4, 7, 0.5) returns 5.5.

    • interpolate(4, 7, 2) returns 10.