# Homework 3

*Due by 9:00pm on Wednesday, 2/20/2019*

## Instructions

Download hw03.zip. Inside the archive, you will find starter files for the questions in this homework, along with a copy of the OK autograder.

**Submission:** When you are done, submit with `python3 ok --submit`

. You may submit more than once before the deadline; only the final submission will be scored. Check that you have successfully submitted your code on okpy.org. See this article for more instructions on okpy and submitting assignments.

**Readings:** This homework relies on following references:

## Questions

### Question 1: Falling Factorial

Let's write a function `falling`

, which is a "falling" factorial
that takes two arguments, `n`

and `k`

, and returns the product of `k`

consecutive numbers, starting from `n`

and working downwards.

If `k`

is larger than n, only multiply up to n consecutive numbers!

```
def falling(n, k):
"""Compute the falling factorial of n to depth k.
>>> falling(6, 3) # 6 * 5 * 4
120
>>> falling(4, 0)
1
>>> falling(4, 3) # 4 * 3 * 2
24
>>> falling(4, 1) # 4
4
>>> falling(4, 10) # 4 * 3 * 2 * 1 # Only n times!!
24
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q falling`

### Question 2: Nonzero

Write a function that takes in a list and returns the first nonzero entry.

```
def nonzero(lst):
""" Returns the first nonzero element of a list
>>> nonzero([1, 2, 3])
1
>>> nonzero([0, 1, 2])
1
>>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6])
5
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q nonzero`

### Question 3: Hailstone

Complete this question using iteration!

Douglas Hofstadter's Pulitzer-prize-winning book, *Gödel, Escher,
Bach*, poses the following mathematical puzzle:

- Pick a positive integer
`n`

as the start. - If
`n`

is even, divide it by 2. - If
`n`

is odd, multiply it by 3 and add 1. - Continue this process until
`n`

is 1.

The sequence of values of `n`

is often called a Hailstone sequence,
because hailstones also travel up and down in the atmosphere before
falling to earth. Write a function that takes a single argument with
formal parameter name `n`

, prints out the hailstone sequence starting
at `n`

, and returns the number of steps in the sequence:

```
def hailstone(n):
"""Print the hailstone sequence starting at n and return its
length.
>>> a = hailstone(10)
10
5
16
8
4
2
1
>>> a
7
"""
"*** YOUR CODE HERE ***"
```

Hailstone sequences can get quite long! Try 27. What's the longest you can find?

Use OK to test your code:

`python3 ok -q hailstone`

### Question 4: Classify the elements

Complete the function `odd_even`

that classifies an number as either `'odd'`

or `'even'`

and the function `classify`

that takes in a list and applies `odd_even`

to all elements in the list.

```
def odd_even(x):
"""Classify a number as odd or even.
>>> odd_even(4)
'even'
>>> odd_even(3)
'odd'
"""
"*** YOUR CODE HERE ***"
def classify(s):
"""
Classify all the elements of a sequence as odd or even
>>> classify([0, 1, 2, 4])
['even', 'odd', 'even', 'even']
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q odd_even`

Use OK to test your code:

`python3 ok -q classify`

### Question 5: Decode

Implement a function `decode`

, which takes in a list of pairs of numbers and returns a list of lists of decoded values.

- The list contains pairs of the form
`[sex, age]`

- Sex is an int that is either 0 or 1 and age is an int between 0 and 10
- Return a list of strings where the Sex gets replaced by "Male" for 0 and "Female" for 1, and age gets replaced by "0-9", "10-19", ..., "90-99", "100+"

See the doctests for examples.

One other thing: your answer to the `decode`

function can only be *one line long*. You should make use of list comprehensions and use the helper function!

```
def decode_helper(pair):
"""
Optional helper function! Could be useful to turn something like [0, 0] to 'Male 0-9'
"""
"*** YOUR CODE HERE ***"
return ''
def decode(list_of_sex_age_pairs):
"""
>>> decode([[0, 0], [1, 1], [1, 10]])
['Male 0-9', 'Female 10-19', 'Female 100+']
>>> decode([[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 5], [1, 6], [1, 7], [1, 8], [1, 9], [1, 10]])
['Male 0-9', 'Male 10-19', 'Male 20-29', 'Male 30-39', 'Male 40-49', 'Female 50-59', 'Female 60-69', 'Female 70-79', 'Female 80-89', 'Female 90-99', 'Female 100+']
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q decode`