# How to Think Like a Computer Scientist: Learning with Python 2nd Edition/Solutions

The following section contains answers to the exercises in the Book.

## Chapter 1

Exercise 1:
Exercise 2:
Exercise 3:
def main:
if __name__ == '__main__':

## Chapter 3

Solution 4 def cat_n_times(s, n):

### Chapter 4 Exercise 7
## Chapter 5
### CH 5 - Solution 9
## Chapter 6
### CH 6 - Solution 1

$ cat ch0601.py
print 'produces', '\n', 'this', '\n', 'output'
$ python ch0601.py
produces
this
output

### CH 6 - Solution 2

$ cat ch0602.py
def sqrt(n):
print sqrt(25)
$ python ch0602.py
7.25 5.34913793103 5.01139410653 5.00001295305 5.00000000002 5.0

### CH 6 - Solution 3

def print_multiples(n, high):
def print_mult_table(high):
if __name__ == "__main__":

### CH 6 - Solution 6 Part 1

What will num_digits(0) return?

### CH 6 - Solution 6 Part 2

Modify it to return 1 for this case.

### CH 6 - Solution 6 Part 3

Why does a call to num_digits(-24) result in an infinite loop (hint: -1/10 evaluates to -1)?
The loop will end when n = 0, and per above -1/10 == -1, causing an infinite loop.

### CH 6 - Solution 9
## Chapter 7
### CH 7 - Question 1

Modify:
so that Ouack and Quack are spelled correctly.

### CH 7 - Notes regarding Question 1

Note that for letter in prefixes: is a substitution of traversal, for example:
is substituted by:

### CH 7 - Question 2

Encapsulate:
in a function named count_letters, and generalize it so that it accepts the string and the letter as arguments.

### CH 7 - Solution 2

Tracing the program:

### CH 7 - Solution 4

Re: the versions of is_lower:
* this is the fastest!!!

### CH 7 - Question 5

Create a file named stringtools.py and put the following in it:
Add a function body to reverse to make the doctests pass.

### CH 7 - Solution 5

Now inorder to make the doctest pass we need to enclose our output in a string. Here we use a placeholder string: lastoutput = "".
Second, we remove redundant code:
per:
by using negative indices, such as:
to:

### CH 7 - Solution 10
## Chapter 8
### CH 8 - Solution 4

Not fully complete but...

## Chapter 9
### CH 9 - Solution 3

What happens if start is less than stop and step is less than 0?
The result will be an empty list.
Write a rule for the relationships among start, stop, and step.

### CH 9 - Solution 14

You can also do:

## Chapter 10
### CH 10 - Solution 1
#### CH 10 - Solution 1.2

isleap(year) - Return 1 for leap years, 0 for non-leap years.

### CH 10 - Solution 2
#### CH 10 - Solution 2.2

There are 35 functions in the math module.

#### CH 10 - Solution 2.3

The floor function finds the greatest integral value less than or equal to x. The ceil function finds the lowest integeral value greater than or equal to x.

#### CH 10 - Solution 2.5

The two data constants in the math module are: 'e' and 'pi'.

### CH 10 - Solution 3

deepcopy would have come handy in exercises you didn't have to solve regarding object reference, thus no answer is excpected here.

### CH 10 - Solution 6

Namespaces are one honking great idea -- let's do more of those!

### CH 10 - Solution 8

def matrix_mult(m1, m2):
return NewMtx

### CH 10 - Solution 16
## Chapter 11
### CH 11 - Solution 11
## Chapter 12
### CH 12 Solution 3

The word "alice" occurs 386 times (not including 12 occurences of "alice's")

### CH 12 Solution 4

The longest 'word' in the list is "bread-and-butter" with 16 characters. This can be found by adding the following code to the alice_words.py program:
Slightly altering this we can find the longest unhyphenated word, "disappointment", which has 14 characters.

### CH 12 Solution 12
## Chapter 13

* date:3-5-2015
* author:m.khodakarami@ut.ac.ir
* this program will reverse the lines of any given file
for line in reversed(open('test.txt').readlines):