#
Homework 2

*Due at 11:59:59 pm on 09/14/2023.*

## Instructions

Download hw02.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 your file to Gradescope. You only need to upload the `.py`

files.

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

## Questions

### Question 1: Fibonacci

The Fibonacci sequence is a famous sequence in mathematics. The first
element in the sequence is 0 and the second element is 1. The *n*th
element is defined as *F _{n} = F_{n-1} +
F_{n-2}*.

Implement the `fib`

function, which takes an integer `n`

and returns
the `n`

th Fibonacci number. Use a `while`

loop in your solution.

```
def fib(n):
"""Returns the nth Fibonacci number.
>>> fib(0)
0
>>> fib(1)
1
>>> fib(2)
1
>>> fib(3)
2
>>> fib(4)
3
>>> fib(5)
5
>>> fib(6)
8
>>> fib(100)
354224848179261915075
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q fib`

### 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: Contains N

Write a function that takes in a list and a number, and returns whether or not the list contains the value n.

```
def has_n(lst, n):
""" Returns whether or not a list contains the value n.
>>> has_n([1, 2, 2], 2)
True
>>> has_n([0, 1, 2], 3)
False
>>> has_n([], 5)
False
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q has_n`

### Question 4: Total Price

Implement the function `total_price`

, which takes in a list of prices of individual products and needs to find the total price. Unfortunately, any product that is priced greater than or equal to $20 has a 50 percent tax, so include that in the final price.

Try to do this in one line!

Cast your final answer to an **integer** to avoid floating point precision errors. For example, if `x`

contains your final answer, `return int(x)`

!

```
def total_price(prices):
"""
Finds the total price of all products in prices including a
50% tax on products with a price greater than or equal to 20.
>>> total_price([5, 20, 30, 7])
87
>>> total_price([8, 4, 3])
15
>>> total_price([10, 100, 4])
164
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q total_price`

### Question 5: arange

Implement the function `arange`

, which behaves just like np.arange(start, end, step) from Data 8. You only need to support positive values for step.

```
def arange(start, end, step=1):
"""
arange behaves just like np.arange(start, end, step).
You only need to support positive values for step.
>>> arange(1, 3)
[1, 2]
>>> arange(0, 25, 2)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
>>> arange(999, 1231, 34)
[999, 1033, 1067, 1101, 1135, 1169, 1203]
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q arange`

### Question 6: Reverse (iteratively)

Write a function `reverse_iter_for`

that takes a list and returns a new
list that is the reverse of the original using a `for`

loop. You should not
need any indexing notation.

```
def reverse_iter_for(lst):
"""Returns the reverse of the given list.
>>> reverse_iter_for([1, 2, 3, 4])
[4, 3, 2, 1]
"""
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q reverse_iter_for`

Complete the function `reverse_iter_while`

that behaves identically to
`reverse_iter_for`

but is implemented as using a `while`

loop.
You may use indexing or slicing notation. Do not use `lst[::-1]`

!

```
def reverse_iter_while(lst):
"""Returns the reverse of the given list.
>>> reverse_iter_while([1, 2, 3, 4])
[4, 3, 2, 1]
"""
rev_lst = []
i = 0
while i < len(lst):
"*** YOUR CODE HERE ***"
```

Use OK to test your code:

`python3 ok -q reverse_iter_while`

**Final Task**: Submitting the Assignment

Now that you have completed your first C88C assignment, it's time to turn it in. You can follow these next steps to submit your work and get points.

### Submit with Gradescope

Log in with

**School Credentials**using your CalNet ID to Gradescope. You’ll be taken to your**Dashboard**as soon as you log in.- On your
**Dashboard**, select the course C88C. You should have already been added to Gradescope. If this is not the case, please make a private Ed post. This will take you to the list of assignments in the course that you’re able to submit. On this list, you will see the status of the assignment, the release date, and the due date. - Click on the assignment Homework 2 to open it.
When the dialog box appears, click on the gray area that says

**Drag & Drop**. This will open your file finder and you should select your code file`hw02.py`

that you edited for this assignment.Once you’ve chosen your file select the

**Upload**button. When your upload is successful, you’ll see a confirmation message on your screen and you’ll receive an email.Next, wait a few minutes for the autograder to grade your code file. Your final score will appear at the right and your output should be the same as the one you tested locally. You can check the code that you submitted at the top right where there is a tab labeled

**Code**. If there are any errors, you can edit your`hw02.py`

code and click**Resubmit**at the bottom of your screen to resubmit your code file. Assignments can be resubmitted as many times as you’d like before the deadline

🎉🎉 Congratulations on finishing Homework 2!