# Think Python/Fruitful functions

## Return values

Some of the built-in functions we have used, such as the math functions,
produce results. Calling the function generates a value, which we
usually assign to a variable or use as part of an expression.

``` python
e = math.exp(1.0)
height = radius * math.sin(radians)
```

All of the functions we have written so far are void; they print
something or move turtles around, but their return value is `None`.

In this chapter, we are (finally) going to write fruitful functions. The
first example is `area`, which returns the area of a circle with the
given radius:

``` python
def area(radius):
    temp = math.pi * radius**2
    return temp
```

We have seen the `return` statement before, but in a fruitful function
the `return` statement includes an expression. This statement means:
"Return immediately from this function and use the following expression
as a return value." The expression can be arbitrarily complicated, so we
could have written this function more concisely:

``` python
def area(radius):
    return math.pi * radius**2
```

On the other hand, **temporary variables** like `temp` often make
debugging easier.

Sometimes it is useful to have multiple return statements, one in each
branch of a conditional:

``` python
def absolute_value(x):
    if x < 0:
        return -x
    else:
        return x
```

Since these `return` statements are in an alternative conditional, only
one will be executed.

As soon as a return statement executes, the function terminates without
executing any subsequent statements. Code that appears after a `return`
statement, or any other place the flow of execution can never reach, is
called **dead code**.

In a fruitful function, it is a good idea to ensure that every possible
path through the program hits a `return` statement. For example:

``` python
def absolute_value(x):
    if x < 0:
        return -x
    if x > 0:
        return x
```

This function is incorrect because if `x` happens to be 0, neither
condition is true, and the function ends without hitting a `return`
statement. If the flow of execution gets to the end of a function, the
return value is `None`, which is not the absolute value of 0.

``` python
>>> print absolute_value(0)
None
```

By the way, Python provides a built-in function called `abs` that
computes absolute values.

### Exercise 1

\'\'Write a **\'`compare`**\' function that returns **\'`1`**\' if
**\'`x > y`**\', **\'`0`**\' if **\'`x == y`**\', and **\'`-1`**\' if
**\'`x < y`**\'.

## Incremental development

As you write larger functions, you might find yourself spending more
time debugging.

To deal with increasingly complex programs, you might want to try a
process called **incremental development**. The goal of incremental
development is to avoid long debugging sessions by adding and testing
only a small amount of code at a time.

As an example, suppose you want to find the distance between two points,
given by the coordinates (`<I>`{=html}x`</I>`{=html}~1~,
`<I>`{=html}y`</I>`{=html}~1~) and (`<I>`{=html}x`</I>`{=html}~2~,
`<I>`{=html}y`</I>`{=html}~2~). By the Pythagorean theorem, the distance
is:

$\text{distance} = \sqrt{ (x_2 - x_1)^2 + (y_2 - y_1)^2}$

The first step is to consider what a `distance` function should look
like in Python. In other words, what are the inputs (parameters) and
what is the output (return value)?

In this case, the inputs are two points, which you can represent using
four numbers. The return value is the distance, which is a
floating-point value.

Already you can write an outline of the function:

``` python
def distance(x1, y1, x2, y2):
    return 0.0
```

Obviously, this version doesn\'t compute distances; it always returns
zero. But it is syntactically correct, and it runs, which means that you
can test it before you make it more complicated.

To test the new function, call it with sample arguments:

``` python
>>> distance(1, 2, 4, 6)
0.0
```

I chose these values so that the horizontal distance is 3 and the
vertical distance is 4; that way, the result is 5 (the hypotenuse of a
3-4-5 triangle). When testing a function, it is useful to know the right
answer.

At this point we have confirmed that the function is syntactically
correct, and we can start adding code to the body. A reasonable next
step is to find the differences `<I>`{=html}x`</I>`{=html}~2~ −
`<I>`{=html}x`</I>`{=html}~1~ and `<I>`{=html}y`</I>`{=html}~2~ −
`<I>`{=html}y`</I>`{=html}~1~. The next version stores those values in
temporary variables and prints them.

``` python
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    print 'dx is', dx
    print 'dy is', dy
    return 0.0
```

If the function is working, it should display `'dx is 3'` and
`’dy is 4’`. If so, we know that the function is getting the right
arguments and performing the first computation correctly. If not, there
are only a few lines to check.

Next we compute the sum of squares of `dx` and `dy`:

``` python
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    dsquared = dx**2 + dy**2
    print 'dsquared is: ', dsquared
    return 0.0
```

Again, you would run the program at this stage and check the output
(which should be 25). Finally, you can use `math.sqrt` to compute and
return the result:

``` python
def distance(x1, y1, x2, y2):
    dx = x2 - x1
    dy = y2 - y1
    dsquared = dx**2 + dy**2
    result = math.sqrt(dsquared)
    return result
```

If that works correctly, you are done. Otherwise, you might want to
print the value of `result` before the return statement.

The final version of the function doesn't display anything when it runs;
it only returns a value. The `print` statements we wrote are useful for
debugging, but once you get the function working, you should remove
them. Code like that is called **scaffolding** because it is helpful for
building the program but is not part of the final product.

When you start out, you should add only a line or two of code at a time.
As you gain more experience, you might find yourself writing and
debugging bigger chunks. Either way, incremental development can save
you a lot of debugging time.

The key aspects of the process are:

-   Start with a working program and make small incremental changes. At
    any point, if there is an error, you should have a good idea where
    it is.
-   Use temporary variables to hold intermediate values so you can
    display and check them.
-   Once the program is working, you might want to remove some of the
    scaffolding or consolidate multiple statements into compound
    expressions, but only if it does not make the program difficult to
    read.

### Exercise 2

*Use incremental development to write a function called
**\'`hypotenuse`**\' that returns the length of the hypotenuse of a
right triangle given the lengths of the two legs as arguments. Record
each stage of the development process as you go.*

## Composition

As you should expect by now, you can call one function from within
another. This ability is called **composition**.

As an example, we'll write a function that takes two points, the center
of the circle and a point on the perimeter, and computes the area of the
circle.

Assume that the center point is stored in the variables `xc` and `yc`,
and the perimeter point is in `xp` and `yp`. The first step is to find
the radius of the circle, which is the distance between the two points.
We just wrote a function, `distance`, that does that:

``` python
radius = distance(xc, yc, xp, yp)
```

The next step is to find the area of a circle with that radius; we just
wrote that, too:

``` python
result = area(radius)
```

Encapsulating these steps in a function, we get:

``` python
def circle_area(xc, yc, xp, yp):
    radius = distance(xc, yc, xp, yp)
    result = area(radius)
    return result
```

The temporary variables `radius` and `result` are useful for development
and debugging, but once the program is working, we can make it more
concise by composing the function calls:

``` python
def circle_area(xc, yc, xp, yp):
    return area(distance(xc, yc, xp, yp))
```

## Boolean functions

Functions can return booleans, which is often convenient for hiding
complicated tests inside functions. For example:

``` python
def is_divisible(x, y):
    if x % y == 0:
        return True
    else:
        return False
```

It is common to give boolean functions names that sound like yes/no
questions; `is_divisible` returns either `True` or `False` to indicate
whether `x` is divisible by `y`.

Here is an example:

``` python
>>>   is_divisible(6, 4)
False
>>>   is_divisible(6, 3)
True
```

The result of the `==` operator is a boolean, so we can write the
function more concisely by returning it directly:

``` python
def is_divisible(x, y):
    return x % y == 0
```

Boolean functions are often used in conditional statements:

``` python
if is_divisible(x, y):
    print 'x is divisible by y'
```

It might be tempting to write something like:

``` python
if is_divisible(x, y) == True:
    print 'x is divisible by y'
```

But the extra comparison is unnecessary.

```{=html}
<DIV CLASS="theorem">
```
**Exercise 3**  *Write a function`is_between(x, y, z)`that returns
**\'`True`**\' if **\'`<I>`{=html}x`</I>`{=html} ≤
`<I>`{=html}y`</I>`{=html} ≤ `<I>`{=html}z`</I>`{=html}**\' or
**\'`False`**\' otherwise.*

```{=html}
</DIV>
```
## More recursion

We have only covered a small subset of Python, but you might be
interested to know that this subset is a *complete* programming
language, which means that anything that can be computed can be
expressed in this language. Any program ever written could be rewritten
using only the language features you have learned so far (actually, you
would need a few commands to control devices like the keyboard, mouse,
disks, etc., but that's all).

Proving that claim is a nontrivial exercise first accomplished by Alan
Turing, one of the first computer scientists (some would argue that he
was a mathematician, but a lot of early computer scientists started as
mathematicians). Accordingly, it is known as the Turing Thesis. For a
more complete (and accurate) discussion of the Turing Thesis, I
recommend Michael Sipser's book *Introduction to the Theory of
Computation*.

To give you an idea of what you can do with the tools you have learned
so far, we'll evaluate a few recursively defined mathematical functions.
A recursive definition is similar to a circular definition, in the sense
that the definition contains a reference to the thing being defined. A
truly circular definition is not very useful:

```{=html}
<DL CLASS="description">
```
```{=html}
<DT CLASS="dt-description">
```
**frabjuous:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An adjective used to describe something that is frabjuous.

```{=html}
</DD>
```
```{=html}
</DL>
```
If you saw that definition in the dictionary, you might be annoyed. On
the other hand, if you looked up the definition of the factorial
function, denoted with the symbol !, you might get something like this:

```{=html}
<TABLE CLASS="display dcenter">
```
```{=html}
<TR VALIGN="middle">
```
```{=html}
<TD CLASS="dcell">
```
```{=html}
<TABLE CELLSPACING=6 CELLPADDING=0>
```
```{=html}
<TR>
```
```{=html}
<TD ALIGN=right NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=center NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=left NOWRAP>
```
0! = 1

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
<TR>
```
```{=html}
<TD ALIGN=right NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=center NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=left NOWRAP>
```
`<I>`{=html}n`</I>`{=html}! = `<I>`{=html}n`</I>`{=html} (`<I>`{=html}n`</I>`{=html}−1)!

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
</TABLE>
```
```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
</TABLE>
```
This definition says that the factorial of 0 is 1, and the factorial of
any other value, `<I>`{=html}n`</I>`{=html}, is
`<I>`{=html}n`</I>`{=html} multiplied by the factorial of
`<I>`{=html}n`</I>`{=html}−1.

So 3! is 3 times 2!, which is 2 times 1!, which is 1 times 0!. Putting
it all together, 3! equals 3 times 2 times 1 times 1, which is 6.

If you can write a recursive definition of something, you can usually
write a Python program to evaluate it. The first step is to decide what
the parameters should be. In this case it should be clear that
`factorial` takes an integer:

``` python
def factorial(n):
```

If the argument happens to be 0, all we have to do is return 1:

``` python
def factorial(n):
    if n == 0:
        return 1
```

Otherwise, and this is the interesting part, we have to make a recursive
call to find the factorial of `<I>`{=html}n`</I>`{=html}−1 and then
multiply it by `<I>`{=html}n`</I>`{=html}:

``` python
def factorial(n):
    if n == 0:
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        return result
```

The flow of execution for this program is similar to the flow of
`countdown` in Section 5.8. If we call `factorial` with the value 3:

Since 3 is not 0, we take the second branch and calculate the factorial
of `n-1`\...

> Since 2 is not 0, we take the second branch and calculate the
> factorial of `n-1`\...
>
> > Since 1 is not 0, we take the second branch and calculate the
> > factorial of `n-1`\...
> >
> > > Since 0 *is* 0, we take the first branch and return 1 without
> > > making any more recursive calls.
> >
> > The return value (1) is multiplied by `<I>`{=html}n`</I>`{=html},
> > which is 1, and the result is returned.
>
> The return value (1) is multiplied by `<I>`{=html}n`</I>`{=html},
> which is 2, and the result is returned.

The return value (2) is multiplied by `<I>`{=html}n`</I>`{=html}, which
is 3, and the result, 6, becomes the return value of the function call
that started the whole process.

Here is what the stack diagram looks like for this sequence of function
calls:

\

```{=html}
<DIV CLASS="center">
```
`<IMG SRC="book009.png">`{=html}

```{=html}
</DIV>
```
\

The return values are shown being passed back up the stack. In each
frame, the return value is the value of `result`, which is the product
of `n` and `recurse`.

In the last frame, the local variables `recurse` and `result` do not
exist, because the branch that creates them does not execute.

## Leap of faith

Following the flow of execution is one way to read programs, but it can
quickly become labyrinthine. An alternative is what I call the "leap of
faith." When you come to a function call, instead of following the flow
of execution, you *assume* that the function works correctly and returns
the right result.

In fact, you are already practicing this leap of faith when you use
built-in functions. When you call `math.cos` or `math.exp`, you don't
examine the bodies of those functions. You just assume that they work
because the people who wrote the built-in functions were good
programmers.

The same is true when you call one of your own functions. For example,
in Section 6.4, we wrote a function called `is_divisible` that
determines whether one number is divisible by another. Once we have
convinced ourselves that this function is correct---by examining the
code and testing---we can use the function without looking at the body
again.

The same is true of recursive programs. When you get to the recursive
call, instead of following the flow of execution, you should assume that
the recursive call works (yields the correct result) and then ask
yourself, "Assuming that I can find the factorial of
`<I>`{=html}n`</I>`{=html}−1, can I compute the factorial of
`<I>`{=html}n`</I>`{=html}?" In this case, it is clear that you can, by
multiplying by `<I>`{=html}n`</I>`{=html}.

Of course, it\'s a bit strange to assume that the function works
correctly when you haven\'t finished writing it, but that\'s why it\'s
called a leap of faith!

## One more example

After `factorial`, the most common example of a recursively defined
mathematical function is `fibonacci`, which has the following
definition[^1]:

```{=html}
<TABLE CLASS="display dcenter">
```
```{=html}
<TR VALIGN="middle">
```
```{=html}
<TD CLASS="dcell">
```
```{=html}
<TABLE CELLSPACING=6 CELLPADDING=0>
```
```{=html}
<TR>
```
```{=html}
<TD ALIGN=right NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=center NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=left NOWRAP>
```
`<I>`{=html}fibonacci`</I>`{=html}(0) = 0

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
<TR>
```
```{=html}
<TD ALIGN=right NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=center NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=left NOWRAP>
```
`<I>`{=html}fibonacci`</I>`{=html}(1) = 1

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
<TR>
```
```{=html}
<TD ALIGN=right NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=center NOWRAP>
```

```{=html}
</TD>
```
```{=html}
<TD ALIGN=left NOWRAP>
```
`<I>`{=html}fibonacci`</I>`{=html}(`<I>`{=html}n`</I>`{=html}) = `<I>`{=html}fibonacci`</I>`{=html}(`<I>`{=html}n`</I>`{=html}−1) + `<I>`{=html}fibonacci`</I>`{=html}(`<I>`{=html}n`</I>`{=html}−2);

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
</TABLE>
```
```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
</TABLE>
```
Translated into Python, it looks like this:

``` python
def fibonacci (n):
    if n == 0:
        return 0
    elif  n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)
```

If you try to follow the flow of execution here, even for fairly small
values of `<I>`{=html}n`</I>`{=html}, your head explodes. But according
to the leap of faith, if you assume that the two recursive calls work
correctly, then it is clear that you get the right result by adding them
together.

## Checking types

What happens if we call `factorial` and give it 1.5 as an argument?

``` python
>>> factorial(1.5)
RuntimeError: Maximum recursion depth exceeded
```

It looks like an infinite recursion. But how can that be? There is a
base case---when `n == 0`. But if `n` is not an integer, we can *miss*
the base case and recurse forever.

In the first recursive call, the value of `n` is 0.5. In the next, it is
-0.5. From there, it gets smaller (more negative), but it will never be
0.

We have two choices. We can try to generalize the `factorial` function
to work with floating-point numbers, or we can make `factorial` check
the type of its argument. The first option is called the gamma
function[^2] and it's a little beyond the scope of this book. So we'll
go for the second.

We can use the built-in function `isinstance` to verify the type of the
argument. While we're at it, we can also make sure the argument is
positive:

``` python
def factorial (n):
    if not isinstance(n, int):
        print 'Factorial is only defined for integers.'
        return None
    elif n < 0:
        print 'Factorial is only defined for positive integers.'
        return None
    elif n == 0:
        return 1
    else:
        return n * factorial(n-1)
```

The first base case handles nonintegers; the second catches negative
integers. In both cases, the program prints an error message and returns
`None` to indicate that something went wrong:

``` python
>>> factorial('fred')
Factorial is only defined for integers.
None
>>> factorial(-2)
Factorial is only defined for positive integers.
None
```

If we get past both checks, then we know that `<I>`{=html}n`</I>`{=html}
is a positive integer, and we can prove that the recursion terminates.

This program demonstrates a pattern sometimes called a **guardian**. The
first two conditionals act as guardians, protecting the code that
follows from values that might cause an error. The guardians make it
possible to prove the correctness of the code.

## Debugging

Breaking a large program into smaller functions creates natural
checkpoints for debugging. If a function is not working, there are three
possibilities to consider:

-   There is something wrong with the arguments the function

is getting; a precondition is violated.

-   There is something wrong with the function; a postcondition

is violated.

-   There is something wrong with the return value or the

way it is being used.

To rule out the first possibility, you can add a `print` statement at
the beginning of the function and display the values of the parameters
(and maybe their types). Or you can write code that checks the
preconditions explicitly.

If the parameters look good, add a `print` statement before each
`return` statement that displays the return value. If possible, check
the result by hand. Consider calling the function with values that make
it easy to check the result (as in Section 6.2).

If the function seems to be working, look at the function call to make
sure the return value is being used correctly (or used at all!).

Adding print statements at the beginning and end of a function can help
make the flow of execution more visible. For example, here is a version
of `factorial` with print statements:

``` python
def factorial(n):
    space = ' ' * (4 * n)
    print space, 'factorial', n
    if n == 0:
        print space, 'returning 1'
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        print space, 'returning', result
        return result
```

`space` is a string of space characters that controls the indentation of
the output. Here is the result of `factorial(5)` :

``` python
                     factorial 5
                 factorial 4
             factorial 3
         factorial 2
     factorial 1
 factorial 0
 returning 1
     returning 1
         returning 2
             returning 6
                 returning 24
                     returning 120
```

If you are confused about the flow of execution, this kind of output can
be helpful. It takes some time to develop effective scaffolding, but a
little bit of scaffolding can save a lot of debugging.

## Glossary

```{=html}
<DL CLASS="description">
```
```{=html}
<DT CLASS="dt-description">
```
**temporary variable:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A variable used to store an intermediate value in a complex calculation.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**dead code:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
Part of a program that can never be executed, often because it appears
after a `return` statement.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**`None`:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A special value returned by functions that have no return statement or a
return statement without an argument.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**incremental development:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A program development plan intended to avoid debugging by adding and
testing only a small amount of code at a time.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**scaffolding:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
Code that is used during program development but is not part of the
final version.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**guardian:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A programming pattern that uses a conditional statement to check for and
handle circumstances that might cause an error.

```{=html}
</DD>
```
```{=html}
</DL>
```
## Exercises

### Exercise 4

*Draw a stack diagram for the following program. What does the program
print?*

``` python
def b(z):
    prod = a(z, z)
    print z, prod
    return prod

def a(x, y):
    x = x + 1
    return x * y

def c(x, y, z):
    sum = x + y + z
    pow = b(sum)**2
    return pow

x = 1
y = x + 1
print c(x, y+3, x+y)
```

### Exercise 5

*The Ackermann function,
**\'`<I>`{=html}A`</I>`{=html}(`<I>`{=html}m`</I>`{=html},
`<I>`{=html}n`</I>`{=html})**\' is defined[^3]:*

```{=html}
<TABLE CLASS="display dcenter">
```
```{=html}
<TR VALIGN="middle">
```
```{=html}
<TD CLASS="dcell">
```
\'\'

$A(M, N) =
\begin{cases}
n + 1 & \mbox{if }m = 0 \\
A(m-1, 1) & \mbox{if } m > 0 \mbox{ and } n = 0 \\
A(m-1, A(m, n-1)) & \mbox{if } m > 0 \mbox{ and } n > 0
\end{cases}$

Write a function named **\'`ack`**\' that evaluates Ackerman's function.
Use your function to evaluate **\'`ack(3, 4)`**\', which should be 125.
What happens for larger values of **\'`m`**\' and **\'`n`**\'?\'\'

### Exercise 6

*A palindrome is a word that is spelled the same backward and forward,
like "noon" and "redivider". Recursively, a word is a palindrome if the
first and last letters are the same and the middle is a palindrome.*

*The following are functions that take a string argument and return the
first, last, and middle letters:*

``` python
def first(word):
    return word[0]

def last(word):
    return word[-1]

def middle(word):
    return word[1:-1]
```

*We'll see how they work in Chapter **\'8**\'.*

-   \'\'Type these functions into a file named **\'`palindrome.py`**\'

and test them out. What happens if you call **\'`middle`**\' with a
string with two letters? One letter? What about the empty string, which
is written *`<CODE>`{=html}*\'*\'`</CODE>`{=html}* and contains no
letters?\'\'

-   *Write a function called`is_palindrome`*\'\' that takes

a string argument and returns **\'`True`**\' if it is a palindrome and
**\'`False`**\' otherwise. Remember that you can use the built-in
function **\'`len`**\' to check the length of a string.\'\'

### Exercise 7

A number, **\'`<I>`{=html}a`</I>`{=html}**\', is a power of
**\'`<I>`{=html}b`</I>`{=html}**\' if it is divisible by
**\'`<I>`{=html}b`</I>`{=html}**\' and
**\'`<I>`{=html}a`</I>`{=html}/`<I>`{=html}b`</I>`{=html}**\' is a power
of **\'`<I>`{=html}b`</I>`{=html}**\'. Write a function called
**`is_power`** that takes parameters **\'`a`**\' and **\'`b`**\' and
returns **\'`True`**\' if **\'`a`**\' is a power of **\'`b`**\'.

### Exercise 8

*The greatest common divisor (GCD) of **\'`<I>`{=html}a`</I>`{=html}**\'
and **\'`<I>`{=html}b`</I>`{=html}**\' is the largest number that
divides both of them with no remainder*[^4]*.*

*One way to find the GCD of two numbers is Euclid's algorithm, which is
based on the observation that if **\'`<I>`{=html}r`</I>`{=html}**\' is
the remainder when **\'`<I>`{=html}a`</I>`{=html}**\' is divided by
**\'`<I>`{=html}b`</I>`{=html}**\', then
**\'`<I>`{=html}gcd`</I>`{=html}(`<I>`{=html}a`</I>`{=html},
`<I>`{=html}b`</I>`{=html}) =
`<I>`{=html}gcd`</I>`{=html}(`<I>`{=html}b`</I>`{=html},
`<I>`{=html}r`</I>`{=html})**\'. As a base case, we can consider
**\'`<I>`{=html}gcd`</I>`{=html}(`<I>`{=html}a`</I>`{=html}, 0) =
`<I>`{=html}a`</I>`{=html}**\'.*

*Write a function called`gcd`that takes parameters **\'`a`**\' and
**\'`b`**\' and returns their greatest common divisor. If you need help,
see **\'`wikipedia.org/wiki/Euclidean_algorithm`**\'.*

## Notes

[^1]: See `wikipedia.org/wiki/Fibonacci_number`.

[^2]: See `wikipedia.org/wiki/Gamma_function`.

[^3]: See `wikipedia.org/wiki/Ackermann_function`

[^4]: This exercise is based on an example from Abelson and Sussman's
    *Structure and Interpretation of Computer Programs*.


# Think Python/Iteration

## Multiple assignment

As you may have discovered, it is legal to make more than one assignment
to the same variable. A new assignment makes an existing variable refer
to a new value (and stop referring to the old value).

``` python
bruce = 5
print bruce,
bruce = 7
print bruce
```

The output of this program is `5 7`, because the first time `bruce` is
printed, its value is 5, and the second time, its value is 7. The comma
at the end of the first `print` statement suppresses the newline, which
is why both outputs appear on the same line.

Here is what **multiple assignment** looks like in a state diagram:

```{=html}
<DIV CLASS="center">
```
`<IMG SRC="book010.png">`{=html}

```{=html}
</DIV>
```
With multiple assignment it is especially important to distinguish
between an assignment operation and a statement of equality. Because
Python uses the equal sign (`=`) for assignment, it is tempting to
interpret a statement like `a = b` as a statement of equality. It is
not!

First, equality is a symmetric relation and assignment is not. For
example, in mathematics, if `<I>`{=html}a`</I>`{=html} = 7 then 7 =
`<I>`{=html}a`</I>`{=html}. But in Python, the statement `a = 7` is
legal and `7 = a` is not.

Furthermore, in mathematics, a statement of equality is either true or
false, for all time. If `<I>`{=html}a`</I>`{=html} =
`<I>`{=html}b`</I>`{=html} now, then `<I>`{=html}a`</I>`{=html} will
always equal `<I>`{=html}b`</I>`{=html}. In Python, an assignment
statement can make two variables equal, but they don't have to stay that
way:

``` python
a = 5
b = a    # a and b are now equal
a = 3    # a and b are no longer equal
```

The third line changes the value of `a` but does not change the value of
`b`, so they are no longer equal.

Although multiple assignment is frequently helpful, you should use it
with caution. If the values of variables change frequently, it can make
the code difficult to read and debug.

## Updating variables

One of the most common forms of multiple assignment is an **update**,
where the new value of the variable depends on the old.

``` python
x = x+1
```

This means "get the current value of `x`, add one, and then update `x`
with the new value."

If you try to update a variable that doesn't exist, you get an error,
because Python evaluates the right side before it assigns a value to
`x`:

``` python
>>> x = x+1
NameError: name 'x' is not defined
```

Before you can update a variable, you have to **initialize** it, usually
with a simple assignment:

``` python
>>> x = 0
>>> x = x+1
```

Updating a variable by adding 1 is called an **increment**; subtracting
1 is called a **decrement**.

## The `while` statement

Computers are often used to automate repetitive tasks. Repeating
identical or similar tasks without making errors is something that
computers do well and people do poorly.

We have seen two programs, `countdown` and `print_n`, that use recursion
to perform repetition, which is also called **iteration**. Because
iteration is so common, Python provides several language features to
make it easier. One is the `for` statement we saw in Section 4.2. We'll
get back to that later.

Another is the `while` statement. Here is a version of `countdown` that
uses a `while` statement:

``` python
def countdown(n):
    while n > 0:
        print n
        n = n-1
    print 'Blastoff!'
```

You can almost read the `while` statement as if it were English. It
means, "While `n` is greater than 0, display the value of `n` and then
reduce the value of `n` by 1. When you get to 0, display the word
`Blastoff!`"

More formally, here is the flow of execution for a `while` statement:

-   Evaluate the condition, yielding `True` or `False`.
-   If the condition is false, exit the `while` statement and continue
    execution at the next statement.
-   If the condition is true, execute the body and then go back to step
    1.

This type of flow is called a **loop** because the third step loops back
around to the top.

The body of the loop should change the value of one or more variables so
that eventually the condition becomes false and the loop terminates.
Otherwise the loop will repeat forever, which is called an **infinite
loop**. An endless source of amusement for computer scientists is the
observation that the directions on shampoo, "Lather, rinse, repeat," are
an infinite loop.

In the case of `countdown`, we can prove that the loop terminates
because we know that the value of `n` is finite, and we can see that the
value of `n` gets smaller each time through the loop, so eventually we
have to get to 0. In other cases, it is not so easy to tell:

``` python
def sequence(n):
    while n != 1:
        print n,
        if n%2 == 0:        # n is even
            n = n/2
        else:               # n is odd
            n = n*3+1
```

The condition for this loop is `n != 1`, so the loop will continue until
`n` is `1`, which makes the condition false.

Each time through the loop, the program outputs the value of `n` and
then checks whether it is even or odd. If it is even, `n` is divided by
2. If it is odd, the value of `n` is replaced with `n*3+1`. For example,
if the argument passed to `sequence` is 3, the resulting sequence is 3,
10, 5, 16, 8, 4, 2, 1.

Since `n` sometimes increases and sometimes decreases, there is no
obvious proof that `n` will ever reach 1, or that the program
terminates. For some particular values of `n`, we can prove termination.
For example, if the starting value is a power of two, then the value of
`n` will be even each time through the loop until it reaches 1. The
previous example ends with such a sequence, starting with 16.

The hard question is whether we can prove that this program terminates
for *all positive values* of `n`. So far^1^, no one has been able to
prove it *or* disprove it!

```{=html}
<DIV CLASS="theorem">
```
**Exercise 1**  *Rewrite the function`print_n`from Section **\'5.8**\'
using iteration instead of recursion.*

```{=html}
</DIV>
```
## `break`

Sometimes you don't know it's time to end a loop until you get half way
through the body. In that case you can use the `break` statement to jump
out of the loop.

For example, suppose you want to take input from the user until they
type `done`. You could write:

``` python
while True:
    line = raw_input('> ')
    if line == 'done':
        break
    print line

print 'Done!'
```

The loop condition is `True`, which is always true, so the loop runs
until it hits the break statement.

Each time through, it prompts the user with an angle bracket. If the
user types `done`, the `break` statement exits the loop. Otherwise the
program echoes whatever the user types and goes back to the top of the
loop. Here's a sample run:

``` python
> not done
not done
> done
Done!
```

This way of writing `while` loops is common because you can check the
condition anywhere in the loop (not just at the top) and you can express
the stop condition affirmatively ("stop when this happens") rather than
negatively ("keep going until that happens.").

## Square roots

Loops are often used in programs that compute numerical results by
starting with an approximate answer and iteratively improving it.

For example, one way of computing square roots is Newton's method.
Suppose that you want to know the square root of
`<I>`{=html}a`</I>`{=html}. If you start with almost any estimate,
`<I>`{=html}x`</I>`{=html}, you can compute a better estimate with the
following formula:

```{=html}
<TABLE CLASS="display dcenter">
```
```{=html}
<TR VALIGN="middle">
```
```{=html}
<TD CLASS="dcell">
```
`<I>`{=html}y`</I>`{=html} =

```{=html}
</TD>
```
```{=html}
<TD CLASS="dcell">
```
```{=html}
<TABLE CLASS="display">
```
```{=html}
<TR>
```
```{=html}
<TD CLASS="dcell" ALIGN="center">
```
`<I>`{=html}x`</I>`{=html} + `<I>`{=html}a`</I>`{=html}/`<I>`{=html}x`</I>`{=html}

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
<TR>
```
```{=html}
<TD CLASS="hbar">
```
```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
<TR>
```
```{=html}
<TD CLASS="dcell" ALIGN="center">
```
2

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
</TABLE>
```
```{=html}
</TD>
```
```{=html}
<TD CLASS="dcell">
```

```{=html}
</TD>
```
```{=html}
</TR>
```
```{=html}
</TABLE>
```
For example, if `<I>`{=html}a`</I>`{=html} is 4 and
`<I>`{=html}x`</I>`{=html} is 3:

``` python
>>> a = 4.0
>>> x = 3.0
>>> y = (x + a/x) / 2
>>> print y
2.16666666667
```

Which is closer to the correct answer
(√`<SPAN style="text-decoration:overline">`{=html}4`</SPAN>`{=html} =
2). If we repeat the process with the new estimate, it gets even closer:

``` python
>>> x = y
>>> y = (x + a/x) / 2
>>> print y
2.00641025641
```

After a few more updates, the estimate is almost exact:

``` python
>>> x = y
>>> y = (x + a/x) / 2
>>> print y
2.00001024003
>>> x = y
>>> y = (x + a/x) / 2
>>> print y
2.00000000003
```

In general we don't know ahead of time how many steps it takes to get to
the right answer, but we know when we get there because the estimate
stops changing:

``` python
>>> x = y
>>> y = (x + a/x) / 2
>>> print y
2.0
>>> x = y
>>> y = (x + a/x) / 2
>>> print y
2.0
```

When `y == x`, we can stop. Here is a loop that starts with an initial
estimate, `x`, and improves it until it stops changing:

``` python
while True:
    print x
    y = (x + a/x) / 2
    if y == x:
        break
    x = y
```

For most values of `a` this works fine, but in general it is dangerous
to test `float` equality. Floating-point values are only approximately
right: most rational numbers, like 1/3, and irrational numbers, like
√`<SPAN style="text-decoration:overline">`{=html}2`</SPAN>`{=html},
can't be represented exactly with a `float`.

Rather than checking whether `x` and `y` are exactly equal, it is safer
to use the built-in function `abs` to compute the absolute value, or
magnitude, of the difference between them:

``` python
    if abs(y-x) < epsilon:
        break
```

Where `epsilon` has a value like `0.0000001` that determines how close
is close enough.

```{=html}
<DIV CLASS="theorem">
```
**Exercise 2**  ***\' Encapsulate this loop in a function called
**`square_root`** that takes**\'`a`**\' as a parameter, chooses a
reasonable value of**\'`x`**\', and returns an estimate of the square
root of**\'`a`.*

```{=html}
</DIV>
```
## Algorithms

Newton's method is an example of an **algorithm**: it is a mechanical
process for solving a category of problems (in this case, computing
square roots).

It is not easy to define an algorithm. It might help to start with
something that is not an algorithm. When you learned to multiply
single-digit numbers, you probably memorized the multiplication table.
In effect, you memorized 100 specific solutions. That kind of knowledge
is not algorithmic.

But if you were "lazy," you probably cheated by learning a few tricks.
For example, to find the product of `<I>`{=html}n`</I>`{=html} and 9,
you can write `<I>`{=html}n`</I>`{=html}−1 as the first digit and
10−`<I>`{=html}n`</I>`{=html} as the second digit. This trick is a
general solution for multiplying any single-digit number by 9. That's an
algorithm!

Similarly, the techniques you learned for addition with carrying,
subtraction with borrowing, and long division are all algorithms. One of
the characteristics of algorithms is that they do not require any
intelligence to carry out. They are mechanical processes in which each
step follows from the last according to a simple set of rules.

In my opinion, it is embarrassing that humans spend so much time in
school learning to execute algorithms that, quite literally, require no
intelligence.

On the other hand, the process of designing algorithms is interesting,
intellectually challenging, and a central part of what we call
programming.

Some of the things that people do naturally, without difficulty or
conscious thought, are the hardest to express algorithmically.
Understanding natural language is a good example. We all do it, but so
far no one has been able to explain *how* we do it, at least not in the
form of an algorithm.

## Debugging

As you start writing bigger programs, you might find yourself spending
more time debugging. More code means more chances to make an error and
more place for bugs to hide.

One way to cut your debugging time is "debugging by bisection." For
example, if there are 100 lines in your program and you check them one
at a time, it would take 100 steps.

Instead, try to break the problem in half. Look at the middle of the
program, or near it, for an intermediate value you can check. Add a
`print` statement (or something else that has a verifiable effect) and
run the program.

If the mid-point check is incorrect, the problem must be in the first
half of the program. If it is correct, the problem is in the second
half.

Every time you perform a check like this, you halve the number of lines
you have to search. After six steps (which is much less than 100), you
would be down to one or two lines of code, at least in theory.

In practice it is not always clear what the "middle of the program" is
and not always possible to check it. It doesn't make sense to count
lines and find the exact midpoint. Instead, think about places in the
program where there might be errors and places where it is easy to put a
check. Then choose a spot where you think the chances are about the same
that the bug is before or after the check.

## Glossary

```{=html}
<DL CLASS="description">
```
```{=html}
<DT CLASS="dt-description">
```
**multiple assignment:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
Making more than one assignment to the same variable during the
execution of a program.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**update:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An assignment where the new value of the variable depends on the old.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**initialize:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An assignment that gives an initial value to a variable that will be
updated.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**increment:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An update that increases the value of a variable (often by one).

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**decrement:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An update that decreases the value of a variable.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**iteration:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
Repeated execution of a set of statements using either a recursive
function call or a loop.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**infinite loop:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A loop in which the terminating condition is never satisfied.

```{=html}
</DD>
```
```{=html}
</DL>
```
## Exercises

### Exercise 3

*To test the square root algorithm in this chapter, you could compare it
with **\'`math.sqrt`**\'. Write a function named`test_square_root`that
prints a table like this:*

``` python
''1.0 1.0           1.0           0.0
2.0 1.41421356237 1.41421356237 2.22044604925e-16
3.0 1.73205080757 1.73205080757 0.0
4.0 2.0           2.0           0.0
5.0 2.2360679775  2.2360679775  0.0
6.0 2.44948974278 2.44948974278 0.0
7.0 2.64575131106 2.64575131106 0.0
8.0 2.82842712475 2.82842712475 4.4408920985e-16
9.0 3.0           3.0           0.0

''
```

\'\'The first column is a number, **\'`<I>`{=html}a`</I>`{=html}**\';
the second column is the square root of
**\'`<I>`{=html}a`</I>`{=html}**\' computed with the function from
Exercise **\'7.2**\'; the third column is the square root computed by
**\'`math.sqrt`**\'; the fourth column is the absolute value of the
difference between the two estimates.

### Exercise 4

*The built-in function **\'`eval`**\' takes a string and evaluates it
using the Python interpreter. For example:*

``` python
''>>> eval('1 + 2 * 3')
7
>>> import math
>>> eval('math.sqrt(5)')
2.2360679774997898
>>> eval('type(math.pi)')
<type 'float'>
''
```

*Write a function called`eval_loop`that iteratively prompts the user,
takes the resulting input and evaluates it using **\'`eval`**\', and
prints the result.*

*It should continue until the user enters***`done`***, and then return
the value of the last expression it evaluated.*

### Exercise 5

*The brilliant mathematician Srinivasa Ramanujan found an infinite
series*^*2*^ that can be used to generate a numerical approximation of
$\pi$:\'\'

$\frac{1}{\pi} = \frac{2 \sqrt{2}} {9801} \sum_{k=0}^{\infty}
\frac{ (4k)! (1103 + 26390k)}{(k!)^4 396^{4k}}$

*Write a function called`estimate_pi`that uses this formula to compute
and return an estimate of **\'π**\'. It should use a **\'`while`**\'
loop to compute terms of the summation until the last term is smaller
than **\'`1e-15`**\' (which is Python notation for **\'10*^*−15*^*). You
can check the result by comparing it to**\'`math.pi`.*

*You can see my solution at **\'`thinkpython.com/code/pi.py`**\'.*

```{=html}
</DIV>
```
```{=html}
<HR CLASS="footnoterule">
```
```{=html}
<DL CLASS="thefootnotes">
```
```{=html}
<DT CLASS="dt-thefootnotes">
```
1

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-thefootnotes">
```
See `wikipedia.org/wiki/Collatz_conjecture`.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-thefootnotes">
```
2

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-thefootnotes">
```
See `wikipedia.org/wiki/Pi`.

```{=html}
</DD>
```
```{=html}
</DL>
```


# Think Python/Strings

## A string is a sequence

A string is a **sequence** of characters. You can access the characters
one at a time with the bracket operator:

``` python
>>> fruit = 'banana'
>>> letter = fruit[1]
```

The second statement selects character number 1 from `fruit` and assigns
it to `letter`.

The expression in brackets is called an **index**. The index indicates
which character in the sequence you want (hence the name).

But you might not get what you expect:

``` python
>>> print letter
a
```

For most people, the first letter of `'banana'` is `b`, not `a`. But for
computer scientists, the index is an offset from the beginning of the
string, and the offset of the first letter is zero.

``` python
>>> letter = fruit[0]
>>> print letter
b
```

So `b` is the 0th letter ("zero-eth") of `'banana'`, `a` is the 1th
letter ("one-eth"), and `n` is the 2th ("two-eth") letter.

You can use any expression, including variables and operators, as an
index, but the value of the index has to be an integer. Otherwise you
get:

``` python
>>> letter = fruit[1.5]
TypeError: string indices must be integers
```

## `len`

`len` is a built-in function that returns the number of characters in a
string:

``` python
>>> fruit = 'banana'
>>> len(fruit)
6
```

To get the last letter of a string, you might be tempted to try
something like this:

    >>> length = len(fruit)
    >>> last = fruit[length]
    IndexError: string index out of range

The reason for the `IndexError` is that there is no letter in `’banana’`
with the index 6. Since we started counting at zero, the six letters are
numbered 0 to 5. To get the last character, you have to subtract 1 from
`length`:

``` python
>>> last = fruit[length-1]
>>> print last
a
```

Alternatively, you can use negative indices, which count backward from
the end of the string. The expression `fruit[-1]` yields the last
letter, `fruit[-2]` yields the second to last, and so on.

## Traversal with a `for` loop

A lot of computations involve processing a string one character at a
time. Often they start at the beginning, select each character in turn,
do something to it, and continue until the end. This pattern of
processing is called a **traversal**. One way to write a traversal is
with a `while` loop:

``` python
index = 0
while index < len(fruit):
    letter = fruit[index]
    print letter
    index = index + 1
```

This loop traverses the string and displays each letter on a line by
itself. The loop condition is `index < len(fruit)`, so when `index` is
equal to the length of the string, the condition is false, and the body
of the loop is not executed. The last character accessed is the one with
the index `len(fruit)-1`, which is the last character in the string.

```{=html}
<DIV CLASS="theorem">
```
**Exercise 1**  *Write a function that takes a string as an argument and
displays the letters backward, one per line.*

```{=html}
</DIV>
```
Another way to write a traversal is with a `for` loop:

``` python
for char in fruit:
    print char
```

Each time through the loop, the next character in the string is assigned
to the variable `char`. The loop continues until no characters are left.

The following example shows how to use concatenation (string addition)
and a `for` loop to generate an abecedarian series (that is, in
alphabetical order). In Robert McCloskey's book *Make Way for
Ducklings*, the names of the ducklings are Jack, Kack, Lack, Mack, Nack,
Ouack, Pack, and Quack. This loop outputs these names in order:

``` python
prefixes = 'JKLMNOPQ'
suffix = 'ack'

for letter in prefixes:
    print letter + suffix
```

The output is:

``` python
Jack
Kack
Lack
Mack
Nack
Oack
Pack
Qack
```

Of course, that's not quite right because "Ouack" and "Quack" are
misspelled.

### Exercise 2

Modify the program to fix this error.

## String slices

A segment of a string is called a **slice**. Selecting a slice is
similar to selecting a character:

``` python
>>> s = 'Monty Python'
>>> print s[0:5]
Monty
>>> print s[6:13]
Python
```

The operator `[n:m]` returns the part of the string from the "n-eth"
character to the "m-eth" character, including the first but excluding
the last. This behavior is counterintuitive, but it might help to
imagine the indices pointing *between* the characters, as in the
following diagram:

```{=html}
<DIV CLASS="center">
```
`<IMG SRC="book011.png">`{=html}

```{=html}
</DIV>
```
If you omit the first index (before the colon), the slice starts at the
beginning of the string. If you omit the second index, the slice goes to
the end of the string:

``` python
>>> fruit = 'banana'
>>> fruit[:3]
'ban'
>>> fruit[3:]
'ana'
```

If the first index is greater than or equal to the second the result is
an **empty string**, represented by two quotation marks:

``` python
>>> fruit = 'banana'
>>> fruit[3:3]
''
```

An empty string contains no characters and has length 0, but other than
that, it is the same as any other string.

### Exercise 3

Given that **\'`fruit`**\' is a string, what does **\'`fruit[:]`**\'
mean?\'\'

## Strings are immutable

It is tempting to use the `[]` operator on the left side of an
assignment, with the intention of changing a character in a string. For
example:

``` python
>>> greeting = 'Hello, world!'
>>> greeting[0] = 'J'
TypeError: object does not support item assignment
```

The "object" in this case is the string and the "item" is the character
you tried to assign. For now, an **object** is the same thing as a
value, but we will refine that definition later. An **item** is one of
the values in a sequence.

The reason for the error is that strings are **immutable**, which means
you can't change an existing string. The best you can do is create a new
string that is a variation on the original:

``` python
>>> greeting = 'Hello, world!'
>>> new_greeting = 'J' + greeting[1:]
>>> print new_greeting
Jello, world!
```

This example concatenates a new first letter onto a slice of `greeting`.
It has no effect on the original string.

## Searching

What does the following function do?

``` python
def find(word, letter):
    index = 0
    while index < len(word):
        if word[index] == letter:
            return index
        index = index + 1
    return -1
```

In a sense, `find` is the opposite of the `[]` operator. Instead of
taking an index and extracting the corresponding character, it takes a
character and finds the index where that character appears. If the
character is not found, the function returns `-1`.

This is the first example we have seen of a `return` statement inside a
loop. If `word[index] == letter`, the function breaks out of the loop
and returns immediately.

If the character doesn't appear in the string, the program exits the
loop normally and returns `-1`.

This pattern of computation---traversing a sequence and returning when
we find what we are looking for---is a called a **search**.

### Exercise 4

Modify **\'`find`**\' so that it has a third parameter, the index in
**\'`word`**\' where it should start looking.

## Looping and counting

The following program counts the number of times the letter `a` appears
in a string:

``` python
word = 'banana'
count = 0
for letter in word:
    if letter == 'a':
        count = count + 1
print count
```

This program demonstrates another pattern of computation called a
**counter**. The variable `count` is initialized to 0 and then
incremented each time an `a` is found. When the loop exits, `count`
contains the result---the total number of `a`'s.

```{=html}
<DIV CLASS="theorem">
```
**Exercise 5**   *Encapsulate this code in a function named
**\'`count`**\', and generalize it so that it accepts the string and the
letter as arguments.*

```{=html}
</DIV>
```
```{=html}
<DIV CLASS="theorem">
```
**Exercise 6**  *Rewrite this function so that instead of traversing the
string, it uses the three-parameter version of **\'`find`**\' from the
previous section.*

```{=html}
</DIV>
```
## `string` methods

A **method** is similar to a function---it takes arguments and returns a
value---but the syntax is different. For example, the method `upper`
takes a string and returns a new string with all uppercase letters:

Instead of the function syntax `upper(word)`, it uses the method syntax
`word.upper()`.

``` python
>>> word = 'banana'
>>> new_word = word.upper()
>>> print new_word
BANANA
```

This form of dot notation specifies the name of the method, `upper`, and
the name of the string to apply the method to, `word`. The empty
parentheses indicate that this method takes no argument.

A method call is called an **invocation**; in this case, we would say
that we are invoking `upper` on the `word`.

As it turns out, there is a string method named `find` that is
remarkably similar to the function we wrote:

``` python
>>> word = 'banana'
>>> index = word.find('a')
>>> print index
1
```

In this example, we invoke `find` on `word` and pass the letter we are
looking for as a parameter.

Actually, the `find` method is more general than our function; it can
find substrings, not just characters:

``` python
>>> word.find('na')
2
```

It can take as a second argument the index where it should start:

``` python
>>> word.find('na', 3)
4
```

And as a third argument the index where it should stop:

``` python
>>> name = 'bob'
>>> name.find('b', 1, 2)
-1
```

This search fails because `b` does not appear in the index range from
`1` to `2` (not including `2`).

```{=html}
<DIV CLASS="theorem">
```
**Exercise 7**  ***\' There is a string method called**\'`count`**\'
that is similar to the function in the previous exercise. Read the
documentation of this method and write an invocation that counts the
number of**\'`a`s in***`banana`***.*

```{=html}
</DIV>
```
## The `in` operator

The word `in` is a boolean operator that takes two strings and returns
`True` if the first appears as a substring in the second:

``` python
>>> 'a' in 'banana'
True
>>> 'seed' in 'banana'
False
```

For example, the following function prints all the letters from `word1`
that also appear in `word2`:

``` python
def in_both(word1, word2):
    for letter in word1:
        if letter in word2:
            print letter
```

With well-chosen variable names, Python sometimes reads like English.
You could read this loop, "for (each) letter in (the first) word, if
(the) letter (appears) in (the second) word, print (the) letter."

Here's what you get if you compare apples and oranges:

``` python
>>> in_both('apples', 'oranges')
a
e
s
```

## String comparison

The comparison operators work on strings. To see if two strings are
equal:

``` python
if word == 'banana':
    print  'All right, bananas.'
```

Other comparison operations are useful for putting words in alphabetical
order:

``` python
if word < 'banana':
    print 'Your word,' + word + ', comes before banana.'
elif word > 'banana':
    print 'Your word,' + word + ', comes after banana.'
else:
    print 'All right, bananas.'
```

Python does not handle uppercase and lowercase letters the same way that
people do. All the uppercase letters come before all the lowercase
letters, so:

``` python
Your word, Pineapple, comes before banana.
```

A common way to address this problem is to convert strings to a standard
format, such as all lowercase, before performing the comparison. Keep
that in mind in case you have to defend yourself against a man armed
with a Pineapple.

## Debugging

When you use indices to traverse the values in a sequence, it is tricky
to get the beginning and end of the traversal right. Here is a function
that is supposed to compare two words and return `True` if one of the
words is the reverse of the other, but it contains two errors:

``` python
def is_reverse(word1, word2):
    if len(word1) != len(word2):
        return False

    i = 0
    j = len(word2)

    while j > 0:
        if word1[i] != word2[j]:
            return False
        i = i+1
        j = j-1

    return True
```

The first `if` statement checks whether the words are the same length.
If not, we can return `False` immediately and then, for the rest of the
function, we can assume that the words are the same length. This is an
example of the guardian pattern in Section 6.8.

`i` and `j` are indices: `i` traverses `word1` forward while `j`
traverses `word2` backward. If we find two letters that don't match, we
can return `False` immediately. If we get through the whole loop and all
the letters match, we return `True`.

If we test this function with the words "pots" and "stop", we expect the
return value `True`, but we get an IndexError:

``` python
>>> is_reverse('pots', 'stop')
...
  File "reverse.py", line 15, in is_reverse
    if word1[i] != word2[j]:
IndexError: string index out of range
```

For debugging this kind of error, my first move is to print the values
of the indices immediately before the line where the error appears.

``` python
    while j > 0:
        print i, j        # print here

        if word1[i] != word2[j]:
            return False
        i = i+1
        j = j-1
```

Now when I run the program again, I get more information:

``` python
>>> is_reverse('pots', 'stop')
0 4
...
IndexError: string index out of range
```

The first time through the loop, the value of `j` is 4, which is out of
range for the string `'pots'`. The index of the last character is 3, so
the initial value for `j` should be `len(word2)-1`.

If I fix that error and run the program again, I get:

``` python
>>> is_reverse('pots', 'stop')
0 3
1 2
2 1
True
```

This time we get the right answer, but it looks like the loop only ran
three times, which is suspicious. To get a better idea of what is
happening, it is useful to draw a state diagram. During the first
iteration, the frame for `is_reverse` looks like this:

```{=html}
<DIV CLASS="center">
```
`<IMG SRC="book012.png">`{=html}

```{=html}
</DIV>
```
I took a little license by arranging the variables in the frame and
adding dotted lines to show that the values of `i` and `j` indicate
characters in `word1` and `word2`.

```{=html}
<DIV CLASS="theorem">
```
**Exercise 8**  ***\' Starting with this diagram, execute the program on
paper, changing the values of**\'`i`**\' and**\'`j`during each
iteration. Find and fix the second error in this function.*

```{=html}
</DIV>
```
## Glossary

```{=html}
<DL CLASS="description">
```
```{=html}
<DT CLASS="dt-description">
```
**object:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
Something a variable can refer to. For now, you can use "object" and
"value" interchangeably.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**sequence:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An ordered set; that is, a set of values where each value is identified
by an integer index.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**item:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
One of the values in a sequence.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**index:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
An integer value used to select an item in a sequence, such as a
character in a string.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**slice:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A part of a string specified by a range of indices.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**empty string:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A string with no characters and length 0, represented by two quotation
marks.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**immutable:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
The property of a sequence whose items cannot be assigned.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**traverse:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
To iterate through the items in a sequence, performing a similar
operation on each.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**search:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A pattern of traversal that stops when it finds what it is looking for.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**counter:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A variable used to count something, usually initialized to zero and then
incremented.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**method:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A function that is associated with an object and called using dot
notation.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**invocation:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A statement that calls a method.

```{=html}
</DD>
```
```{=html}
</DL>
```
## Exercises

### Exercise 9

A string slice can take a third index that specifies the "step size;"
that is, the number of spaces between successive characters. A step size
of 2 means every other character; 3 means every third, etc.

``` python
''>>> fruit = 'banana'
>>> fruit[0:5:2]
'bnn'
''
```

*A step size of -1 goes through the word backwards, so the
slice`[::-1]`generates a reversed string.*

*Use this idiom to write a one-line version of`is_palindrome`*\'\' from
Exercise **\'6.6**\'.

### Exercise 10

Read the documentation of the string methods at
**\'`docs.python.org/lib/string-methods.html`**\'. You might want to
experiment with some of them to make sure you understand how they work.
**\'`strip`**\' and **\'`replace`**\' are particularly useful.\'\'

*The documentation uses a syntax that might be confusing. For example,
in`find(sub[, start[, end]])`*\'\', the brackets indicate optional
arguments. So **\'`sub`**\' is required, but **\'`start`**\' is
optional, and if you include **\'`start`**\', then **\'`end`**\' is
optional.

### Exercise 11

The following functions are all *intended* to check whether a string
contains any lowercase letters, but at least some of them are wrong. For
each function, describe what the function actually does.\'\'

``` python
''def any_lowercase1(s):
    for c in s:
        if c.islower():
            return True
        else:
            return False

def any_lowercase2(s):
    for c in s:
        if 'c'.islower():
            return 'True'
        else:
            return 'False'

def any_lowercase3(s):
    for c in s:
        flag = c.islower()
    return flag

def any_lowercase4(s):
    flag = False
    for c in s:
        flag = flag or c.islower()
    return flag

def any_lowercase5(s):
    for c in s:
        if not c.islower():
            return False
    return True
''
```

### Exercise 12

ROT13 is a weak form of encryption that involves "rotating" each letter
in a word by 13 places[^1]. To rotate a letter means to shift it through
the alphabet, wrapping around to the beginning if necessary, so 'A'
shifted by 3 is 'D' and 'Z' shifted by 1 is 'A'.\'\'

*Write a function called`rotate_word`that takes a string and an integer
as parameters, and that returns a new string that contains the letters
from the original string "rotated" by the given amount.*

*For example, "cheer" rotated by 7 is "jolly" and "melon" rotated by -10
is "cubed".*

*You might want to use the built-in functions **\'`ord`**\', which
converts a character to a numeric code, and **\'`chr`**\', which
converts numeric codes to characters.*

\'\'Potentially offensive jokes on the Internet are sometimes encoded in
ROT13. If you are not easily offended, find and decode some of them.

## Notes

[^1]: See `<TT>`{=html}wikipedia.org/wiki/ROT13


# Think Python/Case study: word play

## Reading word lists

For the exercises in this chapter we need a list of English words. There
are lots of word lists available on the Web, but the one most suitable
for our purpose is one of the word lists collected and contributed to
the public domain by Grady Ward as part of the Moby lexicon project[^1].
It is a list of 113,809 official crosswords; that is, words that are
considered valid in crossword puzzles and other word games. In the Moby
collection, the filename is `113809of.fic`; I include a copy of this
file, with the simpler name `words.txt`, along with Swampy.

This file is in plain text, so you can open it with a text editor, but
you can also read it from Python. (You may need to move the file from
the swampy folder into the main python folder) The built-in function
`open` takes the name of the file as a parameter and returns a **file
object** you can use to read the file.

``` python
>>> fin = open('words.txt')
>>> print fin
<open file 'words.txt', mode 'r' at 0xb7f4b380>
```

`fin` is a common name for a file object used for input. Mode `'r'`
indicates that this file is open for reading (as opposed to `'w'` for
writing).

The file object provides several methods for reading, including
`readline`, which reads characters from the file until it gets to a
newline and returns the result as a string:

``` python
>>> fin.readline()
'aa\r\n'
```

The first word in this particular list is "aa," which is a kind of lava.
The sequence `\r\n` represents two whitespace characters, a carriage
return and a newline, that separate this word from the next.

The file object keeps track of where it is in the file, so if you call
`readline` again, you get the next word:

``` python
>>> fin.readline()
'aah\r\n'
```

The next word is "aah," which is a perfectly legitimate word, so stop
looking at me like that. Or, if it's the whitespace that's bothering
you, we can get rid of it with the string method `strip`:

``` python
>>> line = fin.readline()
>>> word = line.strip()
>>> print word
aahed
```

You can also use a file object as part of a `for` loop. This program
reads `words.txt` and prints each word, one per line:

``` python
fin = open('words.txt')
for line in fin:
    word = line.strip()
    print word
```

```{=html}
<DIV CLASS="theorem">
```
**Exercise 1**  *Write a program that reads **\'`words.txt`**\' and
prints only the words with more than 20 characters (not counting
whitespace).*

```{=html}
</DIV>
```
## Exercises

There are solutions to these exercises in the next section. You should
at least attempt each one before you read the solutions.

```{=html}
<DIV CLASS="theorem">
```
**Exercise 2**  *In 1939 Ernest Vincent Wright published a 50,000 word
novel called*Gadsby*that does not contain the letter "e." Since "e" is
the most common letter in English, that's not easy to do.* *In fact, it
is difficult to construct a solitary thought without using that most
common symbol. It is slow going at first, but with caution and hours of
training you can gradually gain facility.*

*All right, I'll stop now.*

*Write a function called`has_no_e`that returns **\'`True`**\' if the
given word doesn't have the letter "e" in it.*

*Modify your program from the previous section to print only the words
that have no "e" and compute the percentage of the words in the list
have no "e."*

```{=html}
</DIV>
```
```{=html}
<DIV CLASS="theorem">
```
**Exercise 3**  *Write a function named **\'`avoids`**\' that takes a
word and a string of forbidden letters, and that returns **\'`True`**\'
if the word doesn't use any of the forbidden letters.* *Modify your
program to prompt the user to enter a string of forbidden letters and
then print the number of words that don't contain any of them. Can you
find a combination of 5 forbidden letters that excludes the smallest
number of words?*

```{=html}
</DIV>
```
```{=html}
<DIV CLASS="theorem">
```
**Exercise 4**  *Write a function named`uses_only`that takes a word and
a string of letters, and that returns **\'`True`**\' if the word
contains only letters in the list. Can you make a sentence using only
the letters **\'`acefhlo`**\'? Other than "Hoe alfalfa?"*

```{=html}
</DIV>
```
```{=html}
<DIV CLASS="theorem">
```
**Exercise 5**  *Write a function named`uses_all`that takes a word and a
string of required letters, and that returns **\'`True`**\' if the word
uses all the required letters at least once. How many words are there
that use all the vowels **\'`aeiou`**\'? How about **\'`aeiouy`**\'?*

```{=html}
</DIV>
```
```{=html}
<DIV CLASS="theorem">
```
**Exercise 6**  *Write a function called`is_abecedarian`that returns
**\'`True`**\' if the letters in a word appear in alphabetical order
(double letters are ok). How many abecedarian words are there?*

```{=html}
</DIV>
```
## Search

All of the exercises in the previous section have something in common;
they can be solved with the search pattern we saw in Section 8.6. The
simplest example is:

``` python
def has_no_e(word):
    for letter in word:
        if letter == 'e':
            return False
    return A=break
```

The `for` loop traverses the characters in `word`. If we find the letter
"e", we can immediately return `False`; otherwise we have to go to the
next letter. If we exit the loop normally, that means we didn't find an
"e", so we return `True`.

You can write this function more concisely using the `in` operator, but
I started with this version because it demonstrates the logic of the
search pattern.

`avoids` is a more general version of `has_no_e` but it has the same
structure:

``` python
def avoids(word, forbidden):
    for letter in word:
        if letter in forbidden:
            return False
    return True
```

We can return `False` as soon as we find a forbidden letter; if we get
to the end of the loop, we return `True`.

`uses_only` is similar except that the sense of the condition is
reversed:

``` python
def uses_only(word, available):
    for letter in word:
        if letter not in available:
            return False
    return True
```

Instead of a list of forbidden words, we have a list of available words.
If we find a letter in `word` that is not in `available`, we can return
`False`.

`uses_all` is similar except that we reverse the role of the word and
the string of letters:

``` python
def uses_all(word, required):
    for letter in required:
        if letter not in word:
            return False
    return True
```

Instead of traversing the letters in `word`, the loop traverses the
required letters. If any of the required letters do not appear in the
word, we can return `False`.

If you were really thinking like a computer scientist, you would have
recognized that `uses_all` was an instance of a previously-solved
problem, and you would have written:

``` python
def uses_all(word, required):
    return uses_only(required, word)
```

This is an example of a program development method called **problem
recognition**, which means that you recognize the problem you are
working on as an instance of a previously-solved problem, and apply a
previously-developed solution.

## Looping with indices

I wrote the functions in the previous section with `for` loops because I
only needed the characters in the strings; I didn't have to do anything
with the indices.

For `is_abecedarian` we have to compare adjacent letters, which is a
little tricky with a `for` loop:

``` python
def is_abecedarian(word):
    previous = word[0]
    for c in word:
        if c < previous:
            return False
        previous = c
    return True
```

An alternative is to use recursion:

``` python
def is_abecedarian(word):
    if len(word) <= 1:
        return True
    if word[0] > word[1]:
        return False
    return is_abecedarian(word[1:])
```

Another option is to use a `while` loop:

``` python
def is_abecedarian(word):
    i = 0
    while i < len(word)-1:
        if word[i+1] < word[i]:
            return False
        i = i+1
    return True
```

The loop starts at `i=0` and ends when `i=len(word)-1`. Each time
through the loop, it compares the `<I>`{=html}i`</I>`{=html}th character
(which you can think of as the current character) to the
`<I>`{=html}i`</I>`{=html}+1th character (which you can think of as the
next).

If the next character is less than (alphabetically before) the current
one, then we have discovered a break in the abecedarian trend, and we
return `False`.

If we get to the end of the loop without finding a fault, then the word
passes the test. To convince yourself that the loop ends correctly,
consider an example like `'flossy'`. The length of the word is 6, so the
last time the loop runs is when `i` is 4, which is the index of the
second-to-last character. On the last iteration, it compares the
second-to-last character to the last, which is what we want.

Here is a version of `is_palindrome` (see Exercise 6.6) that uses two
indices; one starts at the beginning and goes up; the other starts at
the end and goes down.

``` python
def is_palindrome(word):
    i = 0
    j = len(word)-1

    while i<j:
        if word[i] != word[j]:
            return False
        i = i+i
        j = j-j

    return True
```

Or, if you noticed that this is an instance of a previously-solved
problem, you might have written:

``` python
def is_palindrome(word):
    return is_reverse(word, word)
```

Assuming you did Exercise 8.8.

## Debugging

Testing programs is hard. The functions in this chapter are relatively
easy to test because you can check the results by hand. Even so, it is
somewhere between difficult and impossible to choose a set of words that
test for all possible errors.

Taking `has_no_e` as an example, there are two obvious cases to check:
words that have an 'e' should return `False`; words that don't should
return `True`. You should have no trouble coming up with one of each.

Within each case, there are some less obvious subcases. Among the words
that have an "e," you should test words with an "e" at the beginning,
the end, and somewhere in the middle. You should test long words, short
words, and very short words, like the empty string. The empty string is
an example of a **special case**, which is one of the non-obvious cases
where errors often lurk.

In addition to the test cases you generate, you can also test your
program with a word list like `words.txt`. By scanning the output, you
might be able to catch errors, but be careful: you might catch one kind
of error (words that should not be included, but are) and not another
(words that should be included, but aren't).

In general, testing can help you find bugs, but it is not easy to
generate a good set of test cases, and even if you do, you can't be sure
your program is correct.

According to a legendary computer scientist:

> Program testing can be used to show the presence of bugs, but never to
> show their absence! --- Edsger W. Dijkstra

## Glossary

```{=html}
<DL CLASS="description">
```
```{=html}
<DT CLASS="dt-description">
```
**file object:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A value that represents an open file.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**problem recognition:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A way of solving a problem by expressing it as an instance of a
previously-solved problem.

```{=html}
</DD>
```
```{=html}
<DT CLASS="dt-description">
```
**special case:**

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
A test case that is atypical or non-obvious (and less likely to be
handled correctly).

```{=html}
</DD>
```
```{=html}
</DL>
```
## Exercises

### Exercise 7

This question is based on a Puzzler that was broadcast on the radio
program *Car Talk[^2]*:\'\'

> Give me a word with three consecutive double letters. I\'ll give you a
> couple of words that almost qualify, but don\'t. For example, the word
> committee, c-o-m-m-i-t-t-e-e. It would be great except for the 'i'
> that sneaks in there. Or Mississippi: M-i-s-s-i-s-s-i-p-p-i. If you
> could take out those i's it would work. But there is a word that has
> three consecutive pairs of letters and to the best of my knowledge
> this may be the only word. Of course there are probably 500 more but I
> can only think of one. What is the word?

*Write a program to find it. You can see my solution at
**\'`thinkpython.com/code/cartalk.py`**\'.*

### Exercise 8

Here's another *Car Talk Puzzler*[^3]\'\':

> *"I was driving on the highway the other day and I happened to notice
> my odometer. Like most odometers, it shows six digits, in whole miles
> only. So, if my car had 300,000 miles, for example, I'd see
> 3-0-0-0-0-0.* *"Now, what I saw that day was very interesting. I
> noticed that the last 4 digits were palindromic; that is, they read
> the same forward as backward. For example, 5-4-4-5 is a palindrome, so
> my odometer could have read 3-1-5-4-4-5.*
>
> *"One mile later, the last 5 numbers were palindromic. For example, it
> could have read 3-6-5-4-5-6. One mile after that, the middle 4 out of
> 6 numbers were palindromic. And you ready for this? One mile later,
> all 6 were palindromic!*
>
> *"The question is, what was on the odometer when I first looked?"*

*Write a Python program that tests all the six-digit numbers and prints
any numbers that satisfy these requirements. You can see my solution at
**\'`thinkpython.com/code/cartalk.py`**\'.*

### Exercise 9

Here's another *Car Talk* Puzzler you can solve with a search*[^4]*:

> *"Recently I had a visit with my mom and we realized that the two
> digits that make up my age when reversed resulted in her age. For
> example, if she's 73, I'm 37. We wondered how often this has happened
> over the years but we got sidetracked with other topics and we never
> came up with an answer.* *"When I got home I figured out that the
> digits of our ages have been reversible six times so far. I also
> figured out that if we're lucky it would happen again in a few years,
> and if we're really lucky it would happen one more time after that. In
> other words, it would have happened 8 times over all. So the question
> is, how old am I now?"*

*Write a Python program that searches for solutions to this Puzzler.
Hint: you might find the string method **\'`zfill`**\' useful.*

*You can see my solution at **\'`thinkpython.com/code/cartalk.py`**\'.*

## Notes

[^1]: `wikipedia.org/wiki/Moby_Project`

[^2]: `<TT>`{=html}www.cartalk.com/content/puzzler/transcripts/200725

[^3]: `www.cartalk.com/content/puzzler/transcripts/200803`

[^4]: `www.cartalk.com/content/puzzler/transcripts/200813`