# Chapter 5 Exercise Set 2¶

1. Open a file named `ch05warmups.py` and make the following doctests pass:

```"""
>>> 13 in junk
True
>>> del junk[4]
>>> junk
[3, 7, 9, 10, 17, 21, 24, 27]
>>> del junk[a:b]
>>> junk
[3, 7, 27]
"""
```
```"""
>>> nlist[2][1]
0
>>> nlist[0][2]
17
>>> nlist[1][1]
5
"""
```
```"""
>>> mystring.split('??')
['this', 'and', 'that']
"""
```

Only a string that appears as the first line in a file is the docstring for that file, and only doctests in that string will run.

Add the first set of tests to a string at the beginning of the file, make them all pass, then add the second set of tests to the same docstring, make them all pass, and then add the last doctest and make it pass.

Note

We will study how this works in much greater detail in the Maps, files, and modules chapter.

## Doctest practice set 3¶

Put the next two exercises in a file named `ch05practice3.py`.

### `lots_of_letters`¶

Write a function named `lots_of_letters` that takes a word (string) as an argument and returns a new string with the first letter once, the second letter twice, the third letter three times, and so forth.

Your function should pass the following doctests:

```def lots_of_letters(word):
"""
>>> lots_of_letters('Lidia')
'Liidddiiiiaaaaa'
>>> lots_of_letters('Python')
'Pyyttthhhhooooonnnnnn'
>>> lots_of_letters('')
''
>>> lots_of_letters('1')
'1'
"""
```

What will happen if you pass this function a list or tuple instead of a string? Explain your answer.

### `seperate_by_type`¶

Write a function named `seperate_by_type` that takes a list of values of any type and returns a 3-tuple of lists containing the numbers, sequences, and other types from the argument list in the same order in which they originally occurred.

Your function should pass the following doctests:

```def seperate_by_type(list_of_stuff):
"""
>>> seperate_by_type([3, 'a', 4.2, None, (1, 2), 'b'])
([3, 4.2], ['a', (1, 2), 'b'], [None])
>>> seperate_by_type([1, 3, 'xyz', 42, (0, 2), 'qwerty', [1, 0], 12])
([1, 3, 42, 12], ['xyz', (0, 2), 'qwerty', [1, 0]], [])
>>> seperate_by_type([])
([], [], [])
"""
```

## `numberlists`¶

Create a new file named `numberlists.py` and add the following functions to it:

```def only_evens(numbers):
"""
>>> only_evens([1, 3, 4, 6, 7, 8])
[4, 6, 8]
>>> only_evens([2, 4, 6, 8, 10, 11, 0])
[2, 4, 6, 8, 10, 0]
>>> only_evens([1, 3, 5, 7, 9, 11])
[]
>>> only_evens([4, 0, -1, 2, 6, 7, -4])
[4, 0, 2, 6, -4]
>>> nums = [1, 2, 3, 4]
>>> only_evens(nums)
[2, 4]
>>> nums
[1, 2, 3, 4]
"""
```
```def only_odds(numbers):
"""
>>> only_odds([1, 3, 4, 6, 7, 8])
[1, 3, 7]
>>> only_odds([2, 4, 6, 8, 10, 11, 0])
[11]
>>> only_odds([1, 3, 5, 7, 9, 11])
[1, 3, 5, 7, 9, 11]
>>> only_odds([4, 0, -1, 2, 6, 7, -4])
[-1, 7]
>>> nums = [1, 2, 3, 4]
>>> only_odds(nums)
[1, 3]
>>> nums
[1, 2, 3, 4]
"""
```

Be sure these new functions pass the doctests.

## `multiples_of(num, numlist)`¶

Add a function `multiples_of(num, numlist)` to `numberlists.py` that takes an integer (`num`), and a list of integers (`numlist`) as arguments and returns a list of those integers in `numlist` that are multiples of `num`. Add your own doctests and use TDD to develope this function.

## `s.join(s.split(song)` vs. `s`¶

Given:

```import string

song = "The rain in Spain..."
```

Describe the relationship between `string.join(string.split(song))` and `song`. Are they the same for all strings? When would they be different?

## `replace(s, old, new)`¶

Write a function `replace(s, old, new)` that replaces all occurences of `old` with `new` in a string `s`.

```def replace(s, old, new):
"""
>>> replace('Mississippi', 'i', 'I')
'MIssIssIppI'
>>> s = 'I love spom!  Spom is my favorite food.  Spom, spom, yum!'
>>> replace(s, 'om', 'am')
'I love spam!  Spam is my favorite food.  Spam, spam, spam, yum!'
>>> replace(s, 'o', 'a')
'I lave spam!  Spam is my favarite faad.  Spam, spam, spam, yum!'
"""
```

Hint

Your solution should pass the doctests above. Hint: use `split` and `join` string methods.

## Alarm clock¶

```def will_ring(timenow, hourslater):
"""
"""
```

You look at the clock and it is exactly 2 pm. You set an alarm to go off in 51 hours. At what time does the alarm go off?

## Generalized alarm clock¶

Write a Python program to solve the general version of the above problem. Ask the user for the time now (in hours), and ask for the number of hours to wait. Your program should output what the time will be on the clock when the alarm goes off.

## Week days¶

Assume the days of the week are numbered 0, 1, 2, 3, 4, 5, 6 from Sunday to Saturday. Write a program which is given the day number, and it computes the day name (a string).

## Wonderful holiday¶

You go on a wonderful holiday leaving on day number 3 (a Wednesday). You return home after 137 sleeps. Write a general version of the program which asks for the starting day number, and the length of your stay, and it will tell you the name of day of the week on which you will return.

## Greatest Common Factor (GCF)¶

The greatest common factor of two positive integers, `m` and `n`, is the largest positive integer that that will divide both `m` and `n` evenly.

```def gcf(m, n):
"""
>>> gcf(10, 25)
5
>>> gcf(8, 12)
4
>>> gcf(5, 12)
1
>>> gcf(24, 12)
12
"""
```

Write a function body for `gcf` that will make the doctests pass.

Note

There is a well known recursive algorithm to solve this problem named after the Greek mathematician who described it in one of his books. Do a web search for this and see if you can use it to solve this problem.