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.
Copyright By PowCoder代写 加微信 powcoder
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.
Readings: This homework relies on following references:
Section 1.5
Section 2.3.1-2.3.5
To submit: run ok with the –submit option:
python3 ok –submit
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 F = F + F .
Implement the fib function, which takes an integer n and returns the n th Fibonacci number. Use a while loop in your
def fib(n):
“””Returns the nth Fibonacci number.
>>> fib(0)
>>> fib(1)
>>> fib(2)
>>> fib(3)
>>> fib(4)
>>> fib(5)
>>> fib(6)
>>> 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])
>>> nonzero([0, 1, 2])
>>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6])
“*** 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)
>>> has_n([0, 1, 2], 3)
>>> has_n([], 5)
“*** 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])
>>> total_price([8, 4, 3])
>>> total_price([10, 100, 4])
“*** 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)
>>> 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 = []
while i < len(lst):
"*** YOUR CODE HERE ***"
Use OK to test your code:
python3 ok -q reverse_iter_while
Make sure to submit this assignment by running:
python3 ok --submit
Homework 2
Due at 11:59:59 pm on 02/09/2023.
Question 1: Fibonacci
Question 2: Nonzero
Question 3: Contains N
Question 4: Total Price
Question 5: arange
Question 6: Reverse (iteratively)
C88C Calendar Resources Google Calendar Syllabus Ed Staff OH Queue Python Tutor Recordings
https://c88c.org/sp23/hw/hw02/hw02.zip
https://c88c.org/sp23/
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com