Copyright By PowCoder代写 加微信 powcoder
name = “ABC” # Enter your name
user = “ABC” # Enter your BC username
print(“HW4 submission for {} ({})”.format(name,user))
Please rename the file so that it includes your BC username. For example, I would change HW4 to HW4-reuterj and upload HW4-reuterj.ipynb to Canvas.
Please run the cell below every time you re-open this notebook¶
import math
import random
def test(got, expected, out_flag=1):
if got == expected:
prefix = ‘ OK ‘
if out_flag==1:
print(“{} returned: {} expected: {}”.format(prefix, repr(got), repr(expected)))
prefix = ‘ X ‘
if out_flag==1:
print(“{} returned: {} expected: {}”.format(prefix, repr(got), repr(expected)))
Question 1¶
(2 points) Question 1. Write a function sphere(r) that calculates the volume and surface area of a sphere, where ‘r’ is the radius. The function should return a list where element 0 of the list is the volume and element 1 is the surface area. You may assume that the user will enter a radius that is an int or float.
$V = \frac{4}{3} \pi r^3$
$S = 4 \pi r^2$
Note: Because I asked you to ‘import math’ above, you have access to the constant ‘math.pi’.
# def sphere(r):
# your code goes here
Code to grade question 1¶
flag_q1 = 0
score_q1 = 0
while flag_q1 == 0:
test([‘A’],[‘A’],0)
print(‘Error: Load cell defining test()’)
print(‘Error: Please define sphere()’)
temp = sphere(5.5)
temp_s1 = str(temp[0])[0:8]
temp_s2 = str(temp[1])[0:8]
score_q1 = score_q1 + (temp_s1==’696.9099′ and temp_s2==’380.1327′)
score_q1 = score_q1 + (sphere(0)==[0.0,0.0])
flag_q1 = 1
print(‘\nYour score on Question 1 is {:3.1f} out of 2.0.’.format(score_q1))
Question 2¶
(2 points) You are considering buying a new car that costs \$35,000. Use a nested for loop to calculate (and print) the monthly car payments associated with three different loan lengths (48, 60, 72 months) and three different annual percentage rates (5%, 6%, 7%).
The output should be nine sentences of the form “When the APR is {} and the number of months is {}, the monthly payment is {}.”, and where {} are used by the .format() function.
Hint #1: You can create the lists yourself without using range().
Hint #2: You will need to use PV(Annuity) = (Per Period Payment) * (Annuity Factor). In this case, PV = $35,000
Hint #3: An APR of 6\% implies a monthly rate of 0.06/12.
Hint #4: If you use {:6.2f} instead of {}, your monthly payment will only display two decimal places.
# your code goes here
Question 3. Simulation¶
(4 points) 3a. Write the function threeinarow() that takes no input and returns either a zero or a one (plus the list described under Hint #2).
The function will randomly generate a list containing three elements, where each element is a number between 0 and 9, and where no number can appear more than once. (This is known as sampling without replacement.) For example, it might generate the list [8, 6, 7]. Sort the list from lowest to highest value. If the list consists of three consecutive integers, your function should return 1. Otherwise, it should return 0. For example, the list [6, 7, 8] should return 1 but the list [5, 7, 8] should return 0.
Hint #1: To prevent duplicate integers, you should create a list of integers from 0 to 9 and then use random.sample() to choose 3 elements.
Hint #2: To help us verify that your function works are intended, your function should return the integer 0/1 as its first output and it should return the sorted list of three integers as its second output. These should be two elements of a list.
Once the function is working correctly, please write a for loop (or list comprehension) that runs your function 20 times and prints both outputs as a tuple, with one tuple per line.
# def threeinarow():
# your for loop that calls your new function here
(2 points) Question 3b. We would like to use the function above to determine the probability of drawing three integers from the list [0, 1, …, 9] (without replacement) and ending up with three consecutive integers.
Write a list comprehension statement that runs your function threeinarow() a total of 10000 times and that stores element 0 (and only element 0) of your function’s output in the list ‘count’. The list ‘count’ should consist only of 1s and 0s.
Write three print statements. The first will print the fraction of times that the function threeinarow() returns 1 in the first 1000 elements. The second will print the fraction of times that the function threeinarow() returns 1 in the first 5000 elements. The third will print the fraction of times that the function threeinarow() returns 1 in all 10000 elements.
For example, the print statement could return
‘The fraction of times that I observe 3-in-a-row in 1000 attempts is 0.0400.’
# your code here
# count = []
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com