# Think Python/Debugging

Different kinds of errors can occur in a program, and it is useful to
distinguish among them in order to track them down more quickly:

-   Syntax errors are produced by Python when it is translating the
    source code into byte code. They usually indicate that there is
    something wrong with the syntax of the program. Example: Omitting
    the colon at the end of a `def` statement yields the somewhat
    redundant message `SyntaxError: invalid syntax`.
-   Runtime errors are produced by the interpreter if something goes
    wrong while the program is running. Most runtime error messages
    include information about where the error occurred and what
    functions were executing. Example: An infinite recursion eventually
    causes the runtime error "maximum recursion depth exceeded."
-   Semantic errors are problems with a program that runs without
    producing error messages but doesn't do the right thing. Example: An
    expression may not be evaluated in the order you expect, yielding an
    incorrect result.

The first step in debugging is to figure out which kind of error you are
dealing with. Although the following sections are organized by error
type, some techniques are applicable in more than one situation.

## Syntax errors

Syntax errors are usually easy to fix once you figure out what they are.
Unfortunately, the error messages are often not helpful. The most common
messages are `SyntaxError: invalid syntax` and
`SyntaxError: invalid token`, neither of which is very informative.

On the other hand, the message does tell you where in the program the
problem occurred. Actually, it tells you where Python noticed a problem,
which is not necessarily where the error is. Sometimes the error is
prior to the location of the error message, often on the preceding line.

If you are building the program incrementally, you should have a good
idea about where the error is. It will be in the last line you added.

If you are copying code from a book, start by comparing your code to the
book's code very carefully. Check every character. At the same time,
remember that the book might be wrong, so if you see something that
looks like a syntax error, it might be.

Here are some ways to avoid the most common syntax errors:

-   Make sure you are not using a Python keyword for a variable name.

```{=html}
<!-- -->
```
-   Check that you have a colon at the end of the header of every

compound statement, including `for`, `while`, `if`, and `def`
statements.

-   Make sure that any strings in the code have matching

quotation marks.

-   If you have multiline strings with triple quotes (single or double),
    make

sure you have terminated the string properly. An unterminated string may
cause an `invalid token` error at the end of your program, or it may
treat the following part of the program as a string until it comes to
the next string. In the second case, it might not produce an error
message at all!

-   An unclosed opening operator---`(`, `{`, or

`[`---makes Python continue with the next line as part of the current
statement. Generally, an error occurs almost immediately in the next
line.

-   Check for the classic `=` instead of `==` inside

a conditional.

-   Check the indentation to make sure it lines up the way it

is supposed to. Python can handle space and tabs, but if you mix them it
can cause problems. The best way to avoid this problem is to use a text
editor that knows about Python and generates consistent indentation.

If nothing works, move on to the next section\...

### I keep making changes and it makes no difference.

If the interpreter says there is an error and you don't see it, that
might be because you and the interpreter are not looking at the same
code. Check your programming environment to make sure that the program
you are editing is the one Python is trying to run.

If you are not sure, try putting an obvious and deliberate syntax error
at the beginning of the program. Now run it again. If the interpreter
doesn't find the new error, you are not running the new code.

There are a few likely culprits:

-   You edited the file and forgot to save the changes before running it
    again. Some programming environments do this for you, but some
    don't.
-   You changed the name of the file, but you are still running the old
    name.
-   Something in your development environment is configured incorrectly.
-   If you are writing a module and using `import`, make sure you don't
    give your module the same name as one of the standard Python
    modules.
-   If you are using `import` to read a module, remember that you have
    to restart the interpreter or use `reload` to read a modified file.
    If you import the module again, it doesn't do anything.

If you get stuck and you can't figure out what is going on, one approach
is to start again with a new program like "Hello, World!," and make sure
you can get a known program to run. Then gradually add the pieces of the
original program to the new one.

## Runtime errors

Once your program is syntactically correct, Python can compile it and at
least start running it. What could possibly go wrong?

### My program does absolutely nothing.

This problem is most common when your file consists of functions and
classes but does not actually invoke anything to start execution. This
may be intentional if you only plan to import this module to supply
classes and functions.

If it is not intentional, make sure that you are invoking a function to
start execution, or execute one from the interactive prompt. Also see
the "Flow of Execution" section below.

### My program hangs

If a program stops and seems to be doing nothing, it is "hanging." Often
that means that it is caught in an infinite loop or infinite recursion.

-   If there is a particular loop that you suspect is the problem, add a
    `print` statement immediately before the loop that says "entering
    the loop" and another immediately after that says "exiting the
    loop." Run the program. If you get the first message and not the
    second, you've got an infinite loop. Go to the "Infinite Loop"
    section below.
-   Most of the time, an infinite recursion will cause the program to
    run for a while and then produce a "RuntimeError: Maximum recursion
    depth exceeded" error. If that happens, go to the "Infinite
    Recursion" section below. If you are not getting this error but you
    suspect there is a problem with a recursive method or function, you
    can still use the techniques in the "Infinite Recursion" section.
-   If neither of those steps works, start testing other loops and other
    recursive functions and methods.
-   If that doesn't work, then it is possible that you don't understand
    the flow of execution in your program. Go to the "Flow of Execution"
    section below.

#### Infinite Loop

If you think you have an infinite loop and you think you know what loop
is causing the problem, add a `print` statement at the end of the loop
that prints the values of the variables in the condition and the value
of the condition.

For example:

``` python
while x > 0 and y < 0 :
    # do something to x
    # do something to y

    print  "x: ", x
    print  "y: ", y
    print  "condition: ", (x > 0 and y < 0)
```

Now when you run the program, you will see three lines of output for
each time through the loop. The last time through the loop, the
condition should be `false`. If the loop keeps going, you will be able
to see the values of `x` and `y`, and you might figure out why they are
not being updated correctly.

#### Infinite Recursion

Most of the time, an infinite recursion will cause the program to run
for a while and then produce a `Maximum recursion depth exceeded` error.

If you suspect that a function or method is causing an infinite
recursion, start by checking to make sure that there is a base case. In
other words, there should be some condition that will cause the function
or method to return without making a recursive invocation. If not, then
you need to rethink the algorithm and identify a base case.

If there is a base case but the program doesn't seem to be reaching it,
add a `print` statement at the beginning of the function or method that
prints the parameters. Now when you run the program, you will see a few
lines of output every time the function or method is invoked, and you
will see the parameters. If the parameters are not moving toward the
base case, you will get some ideas about why not.

#### Flow of Execution

If you are not sure how the flow of execution is moving through your
program, add `print` statements to the beginning of each function with a
message like "entering function `foo`," where `foo` is the name of the
function.

Now when you run the program, it will print a trace of each function as
it is invoked.

### When I run the program I get an exception.

If something goes wrong during runtime, Python prints a message that
includes the name of the exception, the line of the program where the
problem occurred, and a traceback.

The traceback identifies the function that is currently running, and
then the function that invoked it, and then the function that invoked
*that*, and so on. In other words, it traces the sequence of function
invocations that got you to where you are. It also includes the line
number in your file where each of these calls occurs.

The first step is to examine the place in the program where the error
occurred and see if you can figure out what happened. These are some of
the most common runtime errors:

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

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
You are trying to use a variable that doesn't exist in the current
environment. Remember that local variables are local. You cannot refer
to them from outside the function where they are defined.

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

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
There are several possible causes:

-   You are trying to use a value improperly. Example: indexing a
    string, list, or tuple with something other than an integer.
-   There is a mismatch between the items in a format string and the
    items passed for conversion. This can happen if either the number of
    items does not match or an invalid conversion is called for.
-   You are passing the wrong number of arguments to a function or
    method. For methods, look at the method definition and check that
    the first parameter is `self`. Then look at the method invocation;
    make sure you are invoking the method on an object with the right
    type and providing the other arguments correctly.

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

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
You are trying to access an element of a dictionary using a key that the
dictionary does not contain.

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

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
You are trying to access an attribute or method that does not exist.
Check the spelling! You can use `dir` to list the attributes that do
exist. If an AttributeError indicates that an object has `NoneType`,
that means that it is `None`. One common cause is forgetting to return a
value from a function; if you get to the end of a function without
hitting a `return` statement, it returns `None`. Another common cause is
using the result from a list method, like `sort`, that returns `None`.

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

```{=html}
</DT>
```
```{=html}
<DD CLASS="dd-description">
```
The index you are using to access a list, string, or tuple is greater
than its length minus one. Immediately before the site of the error, add
a `print` statement to display the value of the index and the length of
the array. Is the array the right size? Is the index the right value?

```{=html}
</DD>
```
```{=html}
</DL>
```
The Python debugger (`pdb`) is useful for tracking down Exceptions
because it allows you to examine the state of the program immediately
before the error. You can read about `pdb` at
`docs.python.org/lib/module-pdb.html`.

### I added so many `print` statements I get inundated with output

One of the problems with using `print` statements for debugging is that
you can end up buried in output. There are two ways to proceed: simplify
the output or simplify the program.

To simplify the output, you can remove or comment out `print` statements
that aren't helping, or combine them, or format the output so it is
easier to understand.

To simplify the program, there are several things you can do. First,
scale down the problem the program is working on. For example, if you
are searching a list, search a *small* list. If the program takes input
from the user, give it the simplest input that causes the problem.

Second, clean up the program. Remove dead code and reorganize the
program to make it as easy to read as possible. For example, if you
suspect that the problem is in a deeply nested part of the program, try
rewriting that part with simpler structure. If you suspect a large
function, try splitting it into smaller functions and testing them
separately.

Often the process of finding the minimal test case leads you to the bug.
If you find that a program works in one situation but not in another,
that gives you a clue about what is going on.

Similarly, rewriting a piece of code can help you find subtle bugs. If
you make a change that you think doesn't affect the program, and it
does, that can tip you off.

## Semantic errors

In some ways, semantic errors are the hardest to debug, because the
interpreter provides no information about what is wrong. Only you know
what the program is supposed to do.

The first step is to make a connection between the program text and the
behavior you are seeing. You need a hypothesis about what the program is
actually doing. One of the things that makes that hard is that computers
run so fast.

You will often wish that you could slow the program down to human speed,
and with some debuggers you can. But the time it takes to insert a few
well-placed `print` statements is often short compared to setting up the
debugger, inserting and removing breakpoints, and "stepping" the program
to where the error is occurring.

### My program doesn't work.

You should ask yourself these questions:

-   Is there something the program was supposed to do but which doesn't
    seem to be happening? Find the section of the code that performs
    that function and make sure it is executing when you think it
    should.
-   Is something happening that shouldn't? Find code in your program
    that performs that function and see if it is executing when it
    shouldn't.

```{=html}
<!-- -->
```
-   Is a section of code producing an effect that is not what you
    expected? Make sure that you understand the code in question,
    especially if it involves invocations to functions or methods in
    other Python modules. Read the documentation for the functions you
    invoke. Try them out by writing simple test cases and checking the
    results.

In order to program, you need to have a mental model of how programs
work. If you write a program that doesn't do what you expect, very often
the problem is not in the program; it's in your mental model.

The best way to correct your mental model is to break the program into
its components (usually the functions and methods) and test each
component independently. Once you find the discrepancy between your
model and reality, you can solve the problem.

Of course, you should be building and testing components as you develop
the program. If you encounter a problem, there should be only a small
amount of new code that is not known to be correct.

### I've got a big hairy expression and it doesn't do what I expect.

Writing complex expressions is fine as long as they are readable, but
they can be hard to debug. It is often a good idea to break a complex
expression into a series of assignments to temporary variables.

For example:

``` python
self.hands[i].addCard(self.hands[self.findNeighbor(i)].popCard())
```

This can be rewritten as:

``` python
neighbor = self.findNeighbor(i)
pickedCard = self.hands[neighbor].popCard()
self.hands[i].addCard(pickedCard)
```

The explicit version is easier to read because the variable names
provide additional documentation, and it is easier to debug because you
can check the types of the intermediate variables and display their
values.

Another problem that can occur with big expressions is that the order of
evaluation may not be what you expect. For example, if you are
translating the expression `<I>`{=html}x`</I>`{=html}/2 π into Python,
you might write:

``` python
y = x / 2 * math.pi
```

That is not correct because multiplication and division have the same
precedence and are evaluated from left to right. So this expression
computes `<I>`{=html}x`</I>`{=html} π / 2.

A good way to debug expressions is to add parentheses to make the order
of evaluation explicit:

``` python
y = x / (2 * math.pi)
```

Whenever you are not sure of the order of evaluation, use parentheses.
Not only will the program be correct (in the sense of doing what you
intended), it will also be more readable for other people who haven't
memorized the rules of precedence.

### I've got a function or method that doesn't return what I expect

If you have a `return` statement with a complex expression, you don't
have a chance to print the `return` value before returning. Again, you
can use a temporary variable. For example, instead of:

``` python
return self.hands[i].removeMatches()
```

you could write:

``` python
count = self.hands[i].removeMatches()
return count
```

Now you have the opportunity to display the value of `count` before
returning.

### I\'m really, really stuck and I need help.

First, try getting away from the computer for a few minutes. Computers
emit waves that affect the brain, causing these symptoms:

-   Frustration and rage.

```{=html}
<!-- -->
```
-   Superstitious beliefs ("the computer hates me") and

magical thinking ("the program only works when I wear my hat backward").

-   Random walk programming (the attempt to program by writing

every possible program and choosing the one that does the right thing).

If you find yourself suffering from any of these symptoms, get up and go
for a walk. When you are calm, think about the program. What is it
doing? What are some possible causes of that behavior? When was the last
time you had a working program, and what did you do next?

Sometimes it just takes time to find a bug. I often find bugs when I am
away from the computer and let my mind wander. Some of the best places
to find bugs are trains, showers, and in bed, just before you fall
asleep.

### No, I really need help.

It happens. Even the best programmers occasionally get stuck. Sometimes
you work on a program so long that you can't see the error. A fresh pair
of eyes is just the thing.

Before you bring someone else in, make sure you are prepared. Your
program should be as simple as possible, and you should be working on
the smallest input that causes the error. You should have `print`
statements in the appropriate places (and the output they produce should
be comprehensible). You should understand the problem well enough to
describe it concisely.

When you bring someone in to help, be sure to give them the information
they need:

-   If there is an error message, what is it and what part of the
    program does it indicate?
-   What was the last thing you did before this error occurred? What
    were the last lines of code that you wrote, or what is the new test
    case that fails?
-   What have you tried so far, and what have you learned?

When you find the bug, take a second to think about what you could have
done to find it faster. Next time you see something similar, you will be
able to find the bug more quickly.

Remember, the goal is not just to make the program work. The goal is to
learn how to make the program work.


# Think Python/Answers

## Chapter 1

See below for Chapter 1 exercises.

### Exercise 1.2

1.) How many seconds are there in 42 minutes 42 seconds?

\>\>\> (42 \* 60) + 42

**2562 seconds**

2.) How many miles are there in 10 kilometers? Hint there are 1.61
kilometers in a mile.

\>\>\> 10/1.61

**6.21 miles**

3.) If you run a 10 kilometer race in 42 minutes 42 seconds, what is
your average time per mile? What is your average speed in miles per
hour? (Hint: there are about 1.61 kilometers in a mile.)

\>\>\> 10 / 1.61 \# convert kilometers to miles

6.211180124223602

\>\>\> (42 \* 60) + 42 \# convert time to seconds

2562

\>\>\> 2562 / 6.211180124223602 \# what is your average time (seconds)
per mile

412.482

\>\>\> 412.482 / 60 \# what is your average time (minutes) per mile

**6.874700000000001**

\>\>\> 60 / 6.874700000000001 \# miles per hour

**8.727653570337614**

How about another way

\>\>\> 10 / 42.7 \# avg kilometers per minute

0.23419203747072598

\>\>\> 0.23419203747072598 \* 60 \# kilometers per hour

14.05152224824356

\>\>\> 14.05152224824356 / 1.61 \# convert to M.P.H

8.727653570337614

or a one-liner

\>\>\> (10 / 1.61) / (42.7 / 60) \# (distance in miles) / (time in
hours)

8.727653570337614 \# miles/hour

## Chapter 2

### Exercise 2.1

If you type an integer with a leading zero, you might get a confusing
error:

``` python
>>> zipcode = 02492
                  ^
SyntaxError: invalid token
```

Other number seem to work, but the results are bizarre:

``` python
>>> zipcode = 02132
>>> print zipcode
1114
```

So python is assuming you want to convert an octal number to a decimal
number. In the base 8 numbering system where valid numbers are **0, 1,
2, 3, 4, 5, 6 and 7**.

``` python
Base  8: 00 01 02 03 04 05 06 07 10 11 12 13 14 15 16 17 20 21 22 23 24
Base 10: 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20
```

Every 8 numbers we increment the left hand columns. This means that the
right most column is the number of \'ones\'. The one to the left of that
is a tally of the number of \'eights\', the one next to that is a tally
of a full column of \'eight\' times the \'eight column\' - 64. The one
next to that is 64\*8 - 512 and so on. For more information read Base
Eight math.

That is why **zipcode = 02492** is invalid as the digit 9 is not a valid
octal number. We can do the conversion manually as follows:

``` python
>>> print 02132
1114
>>> (2*512)+(1*64)+(3*8)+(2*1)
1114
>>>
```

### Exercise 2.2

The volume of a sphere with radius **r** is *4/3* π *r*^*3*^\'\'. What
is the volume of a sphere with radius 5?

``` python
>>> pi = 3.1415926535897932
>>> r = 5
>>> 4/3*pi*r**3 # This is the wrong answer
392.69908169872411
>>> r = 5.0 # Radius can be a float here as well, but is not _necessary_.
>>> 4.0/3.0*pi*r**3 # Using floats give the correct answer
523.5987755982989
>>>
```

Suppose the cover price of a book is \$24.95, but bookstores get a 40%
discount. Shipping costs \$3 for the first copy and 75 cents for each
additional copy. What is the total wholesale cost for 60 copies?\'\'

``` python
 $24.95  Cost
  $9.98  Discount per book
 $14.97  Cost per book after discount
  60     Total number of books
$898.20  Total cost not inc delivery

  $3.00  First book delivery
  59     Remaining books
  $0.75  Delivery cost for extra books
 $44.25  Total cost for extra books
 $47.25  Total Delivery cost

$945.45  Total Bill

This answer is wrong because 40.0/100.0 return wrong value 0.40000000000000002 for more info see IEEE 754 (Standard for Floating-Point Arithmetic)
>>> (24.95-24.95*40.0/100.0)*60+3+0.75*(60-1)
945.44999999999993
>>> 24.95*0.6*60+0.75*(60-1)+3
945.45

You can use the decimal module to maintain precision.
from decimal import Decimal
..
...
def wholesale_cost(args):

    d = 1 - args.get('discount')/100
    purchase = Decimal(args.get('cost') * d * 60)
    return purchase + Decimal(args.get('delivery'))

args =  {'cost': 24.95, 'discount': 40, 'delivery': 3.00+0.75*59}
```

Another solution using functions as well as input prompts:

``` python
# Total wholesale book cost calculator
cover_price = 24.95

number_of_books = int(input("How many books do you want to order at wholesale? "))

def ship_cost (number_of_books):
    if number_of_books == 1:
        return (number_of_books * 3) # Cost of shipping one book is $3
    else:
        return (3 + (number_of_books - 1) * 0.75) # Each additional copy of the book is $0.75 to ship

def discounted_price(number_of_books):
    return(cover_price - (cover_price * .4)) # There is a 40% discount on wholesale book sales

def wholesale_cost(number_of_books):
    return ((discounted_price(number_of_books) * number_of_books) + ship_cost(number_of_books))

print("The cost of buying and shipping", number_of_books, "books is $",round(wholesale_cost(number_of_books), 2))
```

If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per
mile), then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace
again, what time do I get home for breakfast?

Answer: **7:30 am**

How I did it:

``` python
>>> start = (6*60+52)*60
>>> easy = (8*60+15)*2
>>> fast = (7*60+12)*3
>>> finish_hour = (start + easy + fast)/(60*60.0)
>>> finish_floored = (start + easy + fast)//(60*60)  #int() function can also be used to get integer value, but it hasn't taught yet.
>>> finish_minute  = (finish_hour - finish_floored)*60
>>> print ('Finish time was %d:%d' % (finish_hour,finish_minute))
Finish time was 7:30

*** ANOTHER WAY ***
start_time_hr = 6 + 52 / 60.0
easy_pace_hr = (8 + 15 / 60.0 ) / 60.0
tempo_pace_hr = (7 + 12 / 60.0) / 60.0
running_time_hr = 2 * easy_pace_hr + 3 * tempo_pace_hr
breakfast_hr = start_time_hr + running_time_hr
breakfast_min = (breakfast_hr-int(breakfast_hr))*60
breakfast_sec= (breakfast_min-int(breakfast_min))*60

print ('breakfast_hr', int(breakfast_hr) )
print ('breakfast_min', int (breakfast_min) )
print ('breakfast_sec', int (breakfast_sec) )
>>>
```

## Chapter 3

### Exercise 3.1

Python provides a built-in function called *len* that returns the length
of a string, so the value of len(\'allen\') is 5. Write a function named
right_justify that takes a string named *s* as a parameter and prints
the string with enough leading spaces so that the last letter of the
string is in column 70 of the display.

``` python
>>> def right_justify(s):
     print (' '*(70-len(s))+s)

>>> right_justify('allen')
                                                                 allen
>>>
```

Alternate Solution Using concatenation and repetition

``` python

def right_justify(s):
    total_length = 70
    current_length = len(s)
    current_string = s
    while current_length < total_length:
       current_string = " " + current_string
       current_length = len(current_string)
    print(current_string)

OUTPUT

>>> right_justify("monty")
                                                                 monty
```

### Exercise 3.3

You can see my solution at <http://thinkpython.com/code/grid.py>.

``` python
"""
Solution to Exercise 3.5 on page 27 of Think Python
Allen B. Downey, Version 1.1.24+Kart [Python 3.2]

"""

# here is a mostly-straightforward solution to the
# two-by-two version of the grid.

def do_twice(f):
    f()
    f()

def do_four(f):
    do_twice(f)
    do_twice(f)

def print_beam():
    print('+ - - - -', end='')

def print_post():
    print('|        ', end='')

def print_beams():
    do_twice(print_beam)
    print('+')

def print_posts():
    do_twice(print_post)
    print('|')

def print_row():
    print_beams()
    do_twice(print_posts)

def print_grid():
    do_twice(print_row)
    print_beams()

print_grid()
 ____________

# another solution

def do_twice(f):
    f()
    f()

def do_four(f):
    do_twice(f)
    do_twice(f)

def print_column():
    print '+----+----+'

def print_row():
    print '|    |    |'

def print_rows():
    do_four(print_row)

def do_block():
    print_column()
    print_rows()

def print_block():
    do_twice(do_block)
    print_column()

print_block()

# nathan moses-gonzales
_________

# straight-forward solution to 4x4 grid

def do_twice(f):
    f()
    f()

def do_four(f): # not needed for 2x2 grid
    do_twice(f)
    do_twice(f)

def print_beam():
    print('+----', end='')

def print_post():
    print('|    ', end='')

def print_beams():
    do_twice(print_beam)
    print('+')

def print_posts():
    do_twice(print_post)
    print('|')

def print_row():
    print_beams()
    do_twice(print_posts)

def print_grid2x2():
    do_twice(print_row)
    print_beams()

def print_beam4():
    do_four(print_beam)
    print('+')

def print_post4():
    do_four(print_post)
    print('|')

def print_row4():
    print_beam4()
    do_twice(print_post4)

def print_grid4x4():
    do_four(print_row4)
    print_beam4()

print_grid4x4()
-----------------------

# here is a less-straightforward solution to the
# four-by-four grid

def one_four_one(f, g, h):
    f()
    do_four(g)
    h()

def print_plus():
    print '+',

def print_dash():
    print '-',

def print_bar():
    print '|',

def print_space():
    print ' ',

def print_end():
    print

def nothing():
    "do nothing"

def print1beam():
    one_four_one(nothing, print_dash, print_plus)

def print1post():
    one_four_one(nothing, print_space, print_bar)

def print4beams():
    one_four_one(print_plus, print1beam, print_end)

def print4posts():
    one_four_one(print_bar, print1post, print_end)

def print_row():
    one_four_one(nothing, print4posts, print4beams)

def print_grid():
    one_four_one(print4beams, print_row, nothing)

print_grid()

comment = """
After writing a draft of the 4x4 grid, I noticed that many of the
functions had the same structure: they would do something, do
something else four times, and then do something else once.

So I wrote one_four_one, which takes three functions as arguments; it
calls the first one once, then uses do_four to call the second one
four times, then calls the third.

Then I rewrote print1beam, print1post, print4beams, print4posts,
print_row and print_grid using one_four_one.

Programming is an exploratory process.  Writing a draft of a program
often gives you insight into the problem, which might lead you to
rewrite the code to reflect the structure of the solution.

--- Allen
"""

print comment

# another solution
def beam():
    plus = "+"
    minus = "-"*4
    print(plus, minus, plus,minus, plus, minus, plus, minus, plus)

def straight():
    straight = "|"
    space = " "*4
    print(straight, space, straight, space, straight, space, straight, space,
          straight, space)

def quad_straight():
    straight()
    straight()
    straight()
    straight()

def twice():
    beam()
    quad_straight()
    beam()
    quad_straight()

def quad():
    twice()
    twice()
    beam()

quad()
-- :)
------------------

# Without functions.
print("+ - - - - " * 2 + "+")
print("|\t\t  | \t\t|\n" * 3 + "|\t\t  | \t\t|")
print("+ - - - - " * 2 + "+")
print("|\t\t  | \t\t|\n " *3 + "|\t\t  | \t\t|")
print("+ - - - - " * 2 + "+")

------------------

Why not using the first solution and adapt it to the number of rows

def do_twice(f):
    f()
    f()

def do_four(f):
    do_twice(f)
    do_twice(f)

def print_column():
    print '+----+----+----+----+'

def print_row():
    print '|    |    |    |    |'

def print_rows():
    do_four(print_row)

def do_block():
    print_column()
    print_rows()

def print_block():
    do_twice(do_block)
    # print_column()
    do_twice(do_block)
    print_column()

print_block()

-----------------------
# mteodor

def draw_line(bar, middle = ' ', repeat = 2, length = 2):
  """ Draw a single line like this:
  [ (B M*repeat)*length B]
  """
  for k in range(length):
    print("%s %s " % (bar, middle*repeat), end='')
  print(bar)

def draw_grid(length = 2, height = 2, width = 2):
  """ Draw a grid like this:
  + -- + -- +
  |    |    |
  |    |    |
  + -- + -- +
  |    |    |
  |    |    |
  + -- + -- +
  where:
  * length x height are the table size
  * width is the size of a cell/column
  """
  for i in range(height):
    draw_line('+', '-', width, length)
    for j in range(length):
      draw_line('|', ' ', width, length)
  draw_line('+', '-', width, length)

draw_grid(4, 4, 3)

--------------------------
#kipp

# auto adjust size of columns

size=4

def beam():
    print(" + - - - -"*size, "+")

def post():
    print(" |        "*size, "|")

def repeat(func):
    func()
    func()
    func()

# manual adjust size for rows
# this is 4

beam()
repeat(post)
beam()
repeat(post)
beam()
repeat(post)
beam()
repeat(post)
beam()
```

## Chapter 4

### 4.3 Exercise 1

``` python
from TurtleWorld import *

world = TurtleWorld()
bob = Turtle()

def square(t):
    for i in range(4):
        fd(t, 100)
        lt(t)

square(bob)
wait_for_user()
```

### 4.3 Exercise 2

``` python
from TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print(bob)

def square(t, length):
    t = Turtle()
    for i in range(4):
        fd(t, length)
        lt(t)

square(bob, 200)
wait_for_user()
```

### 4.3 Exercise 3

``` python
from swampy.TurtleWorld import *

world = TurtleWorld()
bob = Turtle()
print(bob)

def polygon(t, length, n):

    for i in range(n):
        fd(t, length)
        lt(t, 360 / n)

polygon(bob, 50, 8)
wait_for_user()
```

## Chapter 5

### Exercise 5.2

``` python
def countdown(a):         # A typical countdown function
    if a < 0:
        print("Blastoff")
    elif a > 0:
        print(a)
        countdown(a - 1)

def call_function(n,a):    # The countdown function is called "n" number of times. Any other function can be used instead of countdown function.
    for i in range(n):
        countdown(a)

call_function(3, 10)
```

### Exercise 5.3

``` python
def is_triangle(a, b, c):
       if a <= b+c:
            if b <= a+c:
                 if c <= a+b:
                    return 'yes'
                 else:
                    return 'no'
            else:
                 return 'no'
       else:
            return 'no'

is_triangle(1, 12, 1)
'no'
```

### Exercise 5.3

``` python
def is_triangle(a, b, c):
       if a<=b+c and b<=a+c and c<=a+b:
           return 'yes'
       else:
           return 'no'

is_triangle(1, 12, 3)
'no'
```

## Chapter 9

### Exercise 9.1

``` python

fin = open('words.txt')
for line in fin:
    word = line.strip()
    if len(word) > 20:
        print (word)
```

### Exercise 9.2

``` python

fin = open('words.txt')

def has_no_e(word):
    for char in word:
        if char in 'Ee':
            return False
    return True

count = 0
for line in fin:
    word = line.strip()
    if has_no_e(word):
        count += 1
        print (word)

percent = (count / 113809.0) * 100

print (str(percent)) + "% of the words don't have an 'e'."
```

### Exercise 9.3

``` python

fin = open('words.txt')

def avoids(word,letter):
    for char in word:
        if char in letter:
            return False
    return True

letter = raw_input('What letters to exclude? ')
count = 0
for line in fin:
    word = line.strip()
    if avoids(word, letter):
        count += 1
        print word

percent = (count / 113809.0) * 100

print str(percent) + "% of the words don't have " + letter + '.'
```

### Exercise 9.4

``` python
def uses_only(word, letters):
    """returns true if word is made only out of letters  else flase"""
    for letter in word:
        if letter not in letters:
            return False
    return True
```

## Chapter 10

### Exercise 10.1

Write a function called nested_sum that takes a nested list of integers
and add up the elements from all of the nested lists.

``` python
def nested_sum(nestedList):
        '''
        nestedList: list composed of nested lists containing int.
        Returns the sum of all the int in the nested list
        '''
        newList = []
        #Helper function to flatten the list
        def flatlist(nestedList):
                '''
                Returns a flat list
                '''
                for i in range(len(nestedList)):
                        if type(nestedList[i]) == int:
                                newList.append(nestedList[i])
                        else:
                                flatlist(nestedList[i])
                return newList

        flatlist(nestedList)
        print sum(newList)

nested_sum(nestedList)
```

### Exercise 10.2

Write a function named \"capitalize_nested\" that takes a nested list of
strings and returns a new nested list with all strings capitalized.

``` python
>>> def capitalize_nested(l):
        def capitalize(s):
            return s.capitalize()
        for n, i in enumerate(l):
            if type(i) is list:
                l[n] = capitalize_nested(l[n])
            elif type(i) is str:
                l[n] = capitalize(i)
        return l
```

### Exercise 10.3

Write a function that takes a list of numbers and returns the cumulative
sum.

``` python
>>> def cumulative(l):
    cumulative_sum = 0
    new_list = []
    for i in l:
        cumulative_sum += i
        new_list.append(cumulative_sum)
    return new_list
```

### Exercise 10.4

Write a function called middle that takes a list and returns a new list
that contains all but the first and last elements.

``` python
>>> def middle(x):
    res = []
    i = 1
    while i <= len(x)-2:
        res.append(x[i])
        i += 1
    return res
```

This can also be done simply with a slice.

``` python
>>> def middle(x):
        return x[1:-1]
```

### Exercise 10.5

Write a function called chop that takes a list and modifies it, removing
the first and last elements, and returns None.

``` python
>>> def chop(x):
        del x[:1]
        del x[-1:]
```

## Chapter 11

### Exercise 11.1

Write a function that reads the words in words.txt and stores them as
keys in a dictionary. It doesn't matter what the values are. Then you
can use the in operator as a fast way to check whether a string is in
the dictionary.

``` python
fin = open('words.txt')
englishdict = dict()

def create_diction():
    counter = 0
    dictionairy = dict()
    for line in fin:
        word = line.strip()
        dictionairy[word] = counter
        counter += 1
    return dictionairy
```

### Exercise 11.2

``` python
def invert_dict(s):
    i={}
    for key in s:
        v=s[key]
        i.setdefault(v, []).append(key)
    return i
```

Edit: This was not the exercise I found in my edition of \'Think
Python\', so I\'ve added my answer in case anyone else is curious: Use
get to write histogram more concisely. You should be able to eliminate
the if statement.

``` python
def histogram(s):
    d = dict()
    for c in s:
        d[c] = d.get(c,0)+1
    return d
```

### Exercise 11.3

Dictionaries have a method called keys that returns the keys of the
dictionary, in no particular order, as a list. Modify print_hist to
print the keys and their values in alphabetical order.

``` python
v = {'p' : 1, 'a' : 1, 'r' : 2, 'o' : 1, 't' : 1}
def print_hist(h):
    d = []
    d += sorted(h.keys())
    for c in d:
        print(c, h[c])
```

OR

``` python
v = {'p' : 1, 'a' : 1, 'r' : 2, 'o' : 1, 't' : 1}
def print_hist(h):
    for c in sorted(h.keys()):
        print c, h[c]
```

### Exercise 11.4

Modify reverse_lookup so that it builds and returns a list of all keys
that map to v, or an empty list if there are none.

``` python
def reverse_lookup(d,v):
    l = list()
    for c in d:
        if d[c] == v:
            l.append(c)
    return l
```

## Chapter 12

### Exercise 12.1

``` python
numbers = (1,2,3)
def sumall(numbers):
    x = 0
    for i in numbers:
        x = x + i
    print x
sumall(numbers)
```

or

``` python
def sumall(*t):
    x = 0
    for i in range(len(t)):
        x += t[i]
    return x
```

or

``` python
def sumall(*args):
    t = list(args)
    return sum(t)
```

or

``` python
def sumall(*args):
    return sum(args)
```

### Exercise 12.2

``` python
import random

def sort_by_length(words):
    t = []
    for word in words:
        t.append((len(word),word))
    t.sort(reverse=True)
    res = []
    for length, word in t:
        res.append(word)
    i=0
    final = []
    while i <= len(res)-2:
        if len(res[i]) == len(res[i+1]):
            y_list = [res[i], res[i+1]]
            random.shuffle(y_list)
            final = final + y_list
            i += 2
        else:
            final.append(res[i])
            i += 1
    if i == len(res)-1:
        final.append(res[i])
    return final
```

or

``` python
from random import shuffle

def sort_by_length(words):
    r = []
    d = dict()
    for word in words:
        d.setdefault(len(word), []).append(word)
    for key in sorted(d, reverse=True):
        if len(d[key]) > 1:
            shuffle(d[key])
        r.extend(d[key])
    return r
```

### Exercise 12.3

``` python
import string

def most_frequent(s):
    d = dict()
    inv = dict()
    for char in s:
        if char in string.ascii_letters:
            letter = char.lower()
            d[letter] = d.get(letter, 0) + 1

    for letter, freq in d.items():
        inv.setdefault(freq, []).append(letter)

    for freq in sorted(inv, reverse=True):
        print('{:.2%}:'.format(freq/(sum(list(inv)*len(inv[freq])))), ', '.join(inv[freq]))
```

## Chapter 13

### Exercise 13.7

``` python
from string import punctuation, whitespace, digits
from random import randint
from bisect import bisect_left

def process_file(filename):
    h = dict()
    fp = open(filename)
    for line in fp:
        process_line(line, h)
    return h

def process_line(line, h):
    line = line.replace('-', ' ')
    for word in line.split():
        word = word.strip(punctuation + whitespace + digits)
        word = word.lower()
        if word != '':
            h[word] = h.get(word, 0) + 1

hist = process_file('emma.txt')

def cum_sum(list_of_numbers):
    cum_list = []
    for i, elem in enumerate(list_of_numbers):
        if i == 0:
            cum_list.append(elem)
        else:
            cum_list.append(cum_list[i-1] + elem)
    return cum_list

def random_word(h):
    word_list = list(h.keys())
    num_list = []
    for word in word_list:
        num_list.append(h[word])
    cum_list = cum_sum(num_list)
    i = randint(1, cum_list[-1])
    pos = bisect_left(cum_list, i)
    return word_list[pos]

print(random_word(hist))
```

## Chapter 14

### Exercise 14.3

``` python
import shelve

def dict_of_signatures_and_words(filename='words.txt'):
    d = dict()
    for line in open(filename):
        word = line.lower().strip()
        signature = ''.join(sorted(word))
        d.setdefault(signature, []).append(word)
    return d

def db_of_anagrams(filename='anagrams', d=dict_of_signatures_and_words()):
    db = shelve.open(filename)
    for key, values in d.items():
        if len(values)>1:
            for index, value in enumerate(values):
                db[value]=values[:index]+values[index+1:]
    db.close()

def print_contents_of_db(filename='anagrams'):
    db = shelve.open(filename, flag='r')
    for key in sorted(db):
        print(key.rjust(12), '\t<==>\t', ', '.join(db[key]))
    db.close()

db_of_anagrams()
print_contents_of_db()
```

### Exercise 14.5

``` python
# Replace urllib.request with urllib if you use Python 2.
# I would love to see a more elegant solution for this exercise, possibly by someone who understands html.

import urllib.request

def check(zip_code):
    if zip_code == 'done':
        return False

    else:
                if len(zip_code) != 5:
              print('\nThe zip code must have five digits!')
        return True

def get_html(zip_code):
    gibberish = urllib.request.urlopen('http://www.uszip.com/zip/' + zip_code)
    less_gib = gibberish.read().decode('utf-8')
    return less_gib

def extract_truth(code, key, delimiter):
    pos = code.find(key) + len(key)
    nearly_true = code[pos:pos+40]
    truth = nearly_true.split(delimiter)[0]
    return truth

while True:
    zip_code = input('Please type a zip code (5 digits) or "done" if want to stop:\n')

    if not check(zip_code):
                 break

    code = get_html(zip_code)

    invalid_key = '(0 results)'
    if invalid_key in code:
        print('\nNot a valid zip code.')
        continue

    name_key = '<title>'
    name_del = ' zip'
    name = extract_truth(code, name_key, name_del)

    pop_key = 'Total population</dt><dd>'
    pop_del = '<'
    pop = extract_truth(code, pop_key, pop_del)

    if not 1 < len(pop) < 9:
        pop = 'not available'

    print('\n' + name)
    print('Population:', pop, '\n')
```

## Chapter 15

### Exercise 15.1

``` python
import math

class Point(object):
    """represents a point in 2-D space"""

def distance(p1, p2):
    distance = math.sqrt((p2.x - p1.x)**2 + (p2.y - p1.y)**2)
    return distance

p1 = Point()
p2 = Point()

p1.x = 3
p1.y = 2
p2.x = 4
p2.y = 3

print(distance(p1, p2))
```

### Exercise 15.3

``` python

import copy

def move_rectangle(rec, dx, dy):
    """ does a deep copy and return the newRec with changes applied """
    newRec = copy.deepcopy(rec)
    newRec.corner.x += dx
    newRec.corner.y += dy

    return newRec
```

## Chapter 16

### Exercise 16.1

``` python
def print_time(t):
    print '%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second)
```

or

``` python
# Solution for Python3
# More on string formatting: http://docs.python.org/py3k/library/string.html#formatspec

def print_time(t):
    # 0 is a fill character, 2 defines the width
    print('{}:{:02}:{:02}'.format(t.hour, t.minute, t.second))
```

### Exercise 16.2

``` python
def is_after(t1, t2):
    return (t1.hour, t1.minute, t1.second) > (t2.hour, t2.minute, t2.second)
```

### Exercise 16.3

``` python
# Comment not by the author: This will give a wrong result, if (time.second + seconds % 60) > 60

def increment(time, seconds):

    n = seconds/60
    time.second += seconds - 60.0*n
    time.minute += n

    m = time.minute/60
    time.minute -= m*60
    time.hour += m
```

or

``` python
# Solution for Python3
# Replace '//' by '/' for Python2

def increment(time, seconds):
    time.second += seconds
    time.minute += time.second//60
    time.hour += time.minute//60

    time.second %= 60
    time.minute %= 60
    time.hour %= 24
```

``` python
# A different way of going about it

def increment(time, seconds):
    # Converts total to seconds, then back to a readable format
    time.second = time.hour*3600 + time.minute*60 + time.second + seconds
    (time.minute, time.second) = divmod(time_in_seconds, 60)
    (time.hour, time.minute) = divmod(time.minute, 60)
```

### Exercise 16.4

``` python
# Solution for Python3
# Replace '//' by '/' for Python2

from copy import deepcopy

def increment(time, seconds):
    r = deepcopy(time)

    r.second += seconds
    r.minute += r.second//60
    r.hour += r.minute//60

    r.second %= 60
    r.minute %= 60
    r.hour %= 24

    return r
```

### Exercise 16.5

``` python
class Time(object):
    """represents the time of day.
        attributes: hour, minute, second"""

time = Time()
time.hour = 11
time.minute = 59
time.second = 30

def time_to_int(time):
    minutes = time.hour * 60 + time.minute
    seconds = minutes * 60 + time.second
    return seconds

def int_to_time(seconds):
    time = Time()
    minutes, time.second = divmod(seconds, 60)
    time.hour, time.minute = divmod(minutes, 60)
    return time

def increment(time, addtime):
    seconds = time_to_int(time)
    return int_to_time(seconds + addtime)

def print_time (x):
    print 'The time is %.2d : %.2d : %.2d' % (x.hour, x.minute, x.second)
print_time (time)

newtime = increment (time, 70)

print_time (newtime)
```

### Exercise 16.6

``` python
def time_to_int(time):
    minutes = time.hour * 60 + time.minute
    seconds = minutes * 60 + time.second
    return seconds

def int_to_time(seconds):
    time = Time()
    minutes, time.second = divmod(seconds, 60)
    time.hour, time.minute = divmod(minutes, 60)
    return time

def mul_time(time, factor):
    seconds = time_to_int(time)
    seconds *= factor
    seconds = int(seconds)
    return int_to_time(seconds)

def average_pace(time, distance):
    return mul_time(time, 1/distance)
```

### Exercise 16.7

Write a class definition for a Date object that has attributes *day*,
*month* and *year*. Write a function called *increment_date* that takes
a Date object, *date*, and an integer, *n*, and returns a new Date
object that represents the day *n* days after *date*. Hint: "Thirty days
hath September\..." Challenge: does your function deal with leap years
correctly? See *wikipedia.org/wiki/Leap_year*.

``` python
class Date(object):
    """represents a date.
    attributes: day, month, year"""

def print_date(date):
    # German date format

    print('{}.{}.{}'.format(date.day, date.month, date.year))

def is_leap_year(year):
        # http://en.wikipedia.org/wiki/Leap_year#Algorithm

    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            return False
        return True
    return False

def month_list(year):
    if is_leap_year(year):
        return [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

def days_of_year(year):
    if is_leap_year(year):
        return 366
    return 365

def date_to_int(date):
    days = 0
    for year in range(1, date.year):
        days += days_of_year(year)

    month_days = month_list(date.year)
    for month in range(1, date.month):
        days += month_days[month - 1]

    days += date.day - 1
    return days

def int_to_date(days):
    date = Date()

    date.year = 1
    next_days = 365
    while days >= next_days:
        date.year += 1
        days -= next_days
        next_days = days_of_year(date.year)

    date.month = 1
    next_days = 31
    month_days = month_list(date.year)
    while days >= next_days:
        date.month += 1
        days -= next_days
        next_days = month_days[date.month - 1]

    date.day = days + 1
    return date

def increment_date(date, n):
    days = date_to_int(date)
    return int_to_date(days + n)

d1 = Date()
d1.day, d1.month, d1.year = 8, 3, 2012
print_date(d1)

d2 = increment_date(d1, 7)
print_date(d2)
```

### Exercise 16.8

1\. Use the *datetime* module to write a program that gets the current
date and prints the day of the week.

``` python
from datetime import date

def current_weekday():
    i = date.today()
    print i.strftime('%A')

current_weekday()
```

2\. Write a program that takes a birthday as input and prints the user's
age and the number of days, hours, minutes and seconds until their next
birthday.

``` python
# Python3 solution. Replace "input" by "raw_input" for Python2.
from datetime import datetime

def time_until_birthday():
    dob_input = input(('Please enter the date of your birth in '
                       'the format "mm/dd/yyyy": '))
    dob = datetime.strptime(dob_input, '%m/%d/%Y')
    now = datetime.now()
    if now > datetime(now.year, dob.month, dob.day):
        age = now.year - dob.year
        next_year = True
    else:
        age = now.year - dob.year - 1
        next_year = False
    time_to_birthday = datetime(now.year + next_year,
                                dob.month, dob.day) - now
    days = time_to_birthday.days
    hours, remainder = divmod(time_to_birthday.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    print("\nYou are {} years old.".format(age))
    print(("You have {0} days, {1} hours, {2} minutes and {3} "
           "seconds left until your next birthday.").format(
               days, hours, minutes, seconds))

time_until_birthday()
```

## Chapter 17

### Exercise 17.8

2\.

``` python
from visual import scene, sphere

scene.range = (256, 256, 256)
scene.center = (128, 128, 128)

t = range(0, 256, 51)

for x in t:
    for y in t:
        for z in t:
            pos = x, y, z
            color = (x/255., y/255., z/255.)
            sphere(pos=pos, radius=10, color=color)
```

3\. Download <http://thinkpython.com/code/color_list.py> and use the
function *read_colors* to generate a list of the available colors on
your system, their names and RGB values. For each named color draw a
sphere in the position that corresponds to its RGB values.

``` python
# As there currently (2013-04-12) is no function read_colors in color_list.py
# I use a workaround and simply import the variable COLORS from color_list.py.
# I then use the function all_colors() on COLORS to get a list of the colors.

from color_list import COLORS
from visual import scene, sphere

def all_colors(colors_string=COLORS):
    """Extract a list of unique RGB-tuples from COLORS.
    The tuples look like (r, g, b), where r, g and b are each integers in
    [0, 255].
    """

    # split the string into lines and remove irrelevant lines
    lines = colors_string.split('\n')[2:-2]

    # split the individual lines and remove the names
    numbers_only = [line.split()[:3] for line in lines]

    # turn strings into ints and rgb-lists into tuples
    rgb_tuples = [tuple([int(s) for s in lst]) for lst in numbers_only]

    # return a list of unique tuples
    return list(set(rgb_tuples))

def make_spheres(color_tuples=all_colors()):
    scene.range = (256, 256, 256)
    scene.center = (128, 128, 128)
    for (r, g, b) in color_tuples:
        sphere(pos=(r, g, b), radius=7, color=(r/255., g/255., b/255.))

if __name__ == '__main__':
    make_spheres()
```

## Chapter 3.5

### calculator

``` python
#recursion or recursive
print "\n   INDEX\n""\n C=1 for addition\n""\n  C=2 for substraction\n""\n
C=3 for multiplication\n""\n    C=4 for division\n""\n  C=5 for to find modulus\n""\n   C=6 to find factorial\n"
C=input("Enter your choice here: ")
def add(x,y):
    c=x+y
    print x,"+",y,"=",c
def sub(x,y):
    c=x-y
    print x,"-",y,"=",c
def mul(x,y):
    c=x*y
    print x,"*",y,"=",c
def div(x,y):
    c=x/y
    print x,"/",y,"=",c
def mod(x,y):
    c=x%y
    print x,"%",y,"=",c
if C==6:
    def f(n):
        if n==1:
            print n
            return n
        else:
            print n,"*",
            return n*f(n-1)
    n=input("enter your no here: ")
    print f(n)
if C==1:
    a=input("Enter your first no here: ")
    b=input("Enter your second no here: ")
    add(a,b)
elif C==2:
    a=input("Enter your first no here: ")
    b=input("Enter your second no here: ")
    sub(a,b)
elif C==3:
    a=input("Enter your first no here: ")
    b=input("Enter your second no here: ")
    mul(a,b)
elif C==4:
    a=input("Enter your first no here: ")
    b=input("Enter your second no here: ")
    div(a,b)
elif C==5:
    a=input("Enter your first no here: ")
    b=input("Enter your second no here: ")
    mod(a,b)
```

### palindrome

``` python
def first(word):
    return word[0]
def last(word):return word[-1]
def middle(word):
    return word[1:-1]
def palindrome(word):
    if first(word)==last(word):
        word = middle(word)
        n=len(word)
        if n<2:
            print "palindrome"
        else:
            return palindrome(word)
    else:
        print "not palindrome"

word=raw_input("Enter the  string:")
palindrome(word)
```

### sum of all digits

``` python
def sum_of_n_numbers(number):
    if(number==0):
        return 0
    else:
        return number + sum_of_n_numbers(number-1)
num = raw_input("Enter a number:")
num=int(num)
sum = sum_of_n_numbers(num)
print sum
###another answer in case of while loops
def sum_of_Digits(number):
    sum=0
    while number>0:
        digit=number%10
        sum=sum+digit
        number=number/10
    return sum
num=raw_input("enter the number")
num=int(num)
sum_of_digits=sum_of_Digits(num)
print sum_of_digits
```

### Exercise 18.5

``` python
class Card(object):

    suit_names = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    rank_names = [None, 'Ace', '2', '3', '4', '5', '6', '7',
                    '8', '9', '10', 'Jack', 'Queen', 'King']

    def __init__(self, suit = 0, rank = 2):
        self.suit = suit
        self.rank = rank

    def __str__(self):
        return '%s of %s' % (Card.rank_names[self.rank],
                            Card.suit_names[self.suit])

    def __cmp__(self, other):
        c1 = (self.suit, self.rank)
        c2 = (other.suit, other.rank)
        return cmp(c1, c2)

    def is_valid(self):
        return self.rank > 0

class Deck(object):

    def __init__(self, label = 'Deck'):
        self.label = label
        self.cards = []
        for i in range(4):
            for k in range(1, 14):
                card = Card(i, k)
                self.cards.append(card)

    def __str__(self):
        res = []
        for card in self.cards:
            res.append(str(card))
        print self.label
        return '\n'.join(res)

    def deal_card(self):
        return self.cards.pop(0)

    def add_card(self, card):
        self.cards.append(card)

    def shuffle(self):
        import random
        random.shuffle(self.cards)

    def sort(self):
        self.cards.sort()

    def move_cards(self, other, num):
        for i in range(num):
            other.add_card(self.deal_card())

    def deal_hands(self, num_hands, num_cards):
        if num_hands*num_cards > 52:
            return 'Not enough cards.'

        l = []

        for i in range(1, num_hands + 1):
            hand_i = Hand('Hand %d' % i)
            self.move_cards(hand_i, num_cards)
            l.append(hand_i)

        return l

class Hand(Deck):

    def __init__(self, label = ''):
        self.cards = []
        self.label = label

# 18-6, 1-4:
class PokerHand(Hand):

    def suit_hist(self):
        self.suits = {}
        for card in self.cards:
            self.suits[card.suit] = self.suits.get(card.suit, 0) + 1
        return self.suits

    def rank_hist(self):
        self.ranks = {}
        for card in self.cards:
            self.ranks[card.rank] = self.ranks.get(card.rank, 0) + 1
        return self.ranks

    def P(self):
        self.rank_hist()
        for val in self.ranks.values():
            if val >= 2:
                return True
        return False

    def TP(self):
        self.rank_hist()
        count = 0
        for val in self.ranks.values():
            if val == 4:
                return True
            elif val >= 2 and val < 4:
                count += 1
        return count >= 2

    def TOAK(self):
        self.rank_hist()
        for val in self.ranks.values():
            if val >= 3:
                return True
        return False

    def STRseq(self):
        seq = []
        l = STRlist()
        self.rank_hist()
        h = self.ranks.keys()
        h.sort()
        if len(h) < 5:
            return []

        # Accounts for high Aces:
        if 1 in h:
            h.append(1)

        for i in range(5, len(h)+1):
            if h[i-5:i] in l:
                seq.append(h[i-5:i])
        return seq

    def STR(self):
        seq = self.STRseq()
        return seq != []

    def FL(self):
        self.suit_hist()
        for val in self.suits.values():
            if val >= 5:
                return True
        return False

    def FH(self):
        d = self.rank_hist()
        keys = d.keys()

        for key in keys:
            if d[key] >= 3:
                keys.remove(key)
                for key in keys:
                    if d[key] >= 2:
                        return True
        return False

    def FOAK(self):
        self.rank_hist()
        for val in self.ranks.values():
            if val >= 4:
                return True
        return False

    def SFL(self):
        seq = self.STRseq()
        if seq == []:
            return False
        for list in seq:
            list_suits = []
            for index in list:
                for card in self.cards:
                    if card.rank == index:
                        list_suits.append(card.suit)
            list_hist = histogram(list_suits)
            for key in list_hist.keys():
                if list_hist[key] >= 5:
                    return True
        return False

    def classify(self):
        self.scores = []
        hands = ['Pair', 'Two-Pair',
        'Three of a Kind', 'Straight',
        'Flush', 'Full House',
        'Four of a Kind', 'Straight Flush']
        if self.P():
            self.scores.append(1)
        if self.TP():
            self.scores.append(2)
        if self.TOAK():
            self.scores.append(3)
        if self.STR():
            self.scores.append(4)
        if self.FL():
            self.scores.append(5)
        if self.FH():
            self.scores.append(6)
        if self.FOAK():
            self.scores.append(7)
        if self.SFL():
            self.scores.append(8)
        if self.scores != []:
            return hands[max(self.scores)-1]

def STRlist():
    s = []
    for i in range(0,9):
        s.append(range(1,14)[i:i+5])
    s.append([10,11,12,13,1])
    return s

def histogram(l):
    d = dict()
    for k in range(len(l)):
        d[l[k]] = 1 + d.get(l[k],0)
    return d

# 18-6, 5:
def p(config = '', trials = 10000, n = 1):
    """Estimates probability that the
    nth dealt hand will be config. A hand
    consists of seven cards."""

    successes = 0

    for i in range(1, trials + 1):
        deck = Deck('Deck %d' % i)
        deck.shuffle()

        box = Hand()
        deck.move_cards(box, (n-1)*7)

        hand = PokerHand('Poker Hand %d' % i)
        deck.move_cards(hand, 7)
        if hand.classify() == config:
            successes += 1

    return 1.0*successes/trials

#Iterate until first desired config.:
if __name__ == '__main__':

    c = 1

    while True:
        deck = Deck()
        deck.shuffle()
        hand = PokerHand('Poker Hand %d' % c)
        deck.move_cards(hand, 5)
        print hand
        print hand.SFL()
        if hand.SFL():
            print hand.STRseq()
            break
        print ''
        c += 1

Code by Victor Alvarez
```

## Appendix B

### Exercise B.1

Read the Wikipedia page on Big-Oh notation and answer the following
questions:

1\. *What is the order of growth of n³ + n²? What about 1000000n³ + n²?
What about n³ + 1000000n²?*

:   A: All are O(n³)

2\. *What is the order of growth of (n² + n) · (n + 1)? Before you start
multiplying, remember that you only need the leading term.*

:   A: O(n³)

3\. *If f is in O(g), for some unspecified function g, what can we say
about af + b, where a and b are constants?*

:   A: a and b are also O(g) since O-notation uses the leading
    coefficient. Constants are O(1), so they are irrelevant in this
    case.

4\. \'\'If f1 and f2 are in O(g), what can we say about f1 + f2?

:   A: Assuming the author means that g in this problem is separate from
    g the last problem, then O(g) remains unchanged.

5\. *If f1 is in O(g) and f2 is in O(h), what can we say about f1 + f2?*

:   A: f1 + f2 may or may not match the higher order of growth of either
    O(g) and O(h). If f1 or f2 are the leading coefficients, then f1 +
    f2 will have the same order of growth as the the largest order of
    growth of either O(g) or O(h). If that is not the case, then any
    information about order of growth of f1 + f2 is unknown to us. i.e.
    It doesn\'t matter that they are being added.

6\. *If f1 is in O(g) and f2 is O(h), what can we say about f1 · f2?*

:   A: This is different. If f1 and f2 are multiplied (and assuming they
    each contain an n in them) then O(f1 · f2) has to be O(n) or larger.
    It could be larger if f1 is n³, for example, but it must at least be
    O(n).

### Exercise B.3

Write a function called *bisection* that takes a sorted list and a
target value and returns the index of the value in the list, if it's
there, or *None* if it's not.

``` python
from bisect import bisect_left

def bisection(sorted_list, item):
    i = bisect_left(sorted_list, item)
    if i < len(sorted_list) and sorted_list[i] == item:
        return i
    else:
        return None

if __name__ == '__main__':
    a = [1, 2, 3]
    print(bisection(a, 2))  # expect 1
    b = [1, 3]
    print(bisection(b, 2))  # expect None
    c = [1, 2]
    print(bisection(c, 3))  # expect None
```