## 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 nth element is defined as Fn = Fn-1 + Fn-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
"""

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
"""

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
"""

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):
"""
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
"""

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]

"""
``````

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]
"""

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):

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.

4. 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.
6. 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