Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
566 views
ubuntu2004
Kernel: Python 3 (system-wide)

COSC130 - Homework 01

Kyle Anderson

Problem 1: Arithmetic and Geometric Means

x0 = 11 x1 = 13 x2 = 17 x3 = 19 total = x0+x1+x2+x3 product = x0*x1*x2*x3 arith_mean = round(total/4,2); geom_mean = round(product**(1/float(4)),2) print("Sum: ", total) print("Product: ", product) print("Arithmetic Mean: ",arith_mean) print("Geometric Mean: ", geom_mean)
Sum: 60 Product: 46189 Arithmetic Mean: 15.0 Geometric Mean: 14.66

Problem 2: Calculating Bill

ss_cost = 3.95*2 hb_cost = 8.95*2 ds_cost = 2.50*2 subtotal = ss_cost + hb_cost + ds_cost tax = round(subtotal * 0.0475,2) tip = round(subtotal * 0.15,2) total_charge = subtotal + tax + tip print(f"2 Side Salads:\t${ss_cost:5.2f}") print(f"2 Hamburgers:\t${hb_cost:5.2f}") print(f"2 Diet Sodas:\t${ds_cost:5.2f}") print("----------------------") print(f"Subtotal:\t${subtotal:5.2f}") print(f"Tax (4.75%):\t${tax:5.2f}") print(f"Tip (15%):\t${tip:5.2f}") print("----------------------") print(f"Total Charge:\t${total_charge:5.2f}")
2 Side Salads: $ 7.90 2 Hamburgers: $17.90 2 Diet Sodas: $ 5.00 ---------------------- Subtotal: $30.80 Tax (4.75%): $ 1.46 Tip (15%): $ 4.62 ---------------------- Total Charge: $36.88

Problem 3: Volume of a Sphere

def findVolume(r): pi = 3.14519 #pi v = (4/3)*pi*r*r*r return v r1, r2, r3 = 4.6, 7.2, 9.7 ans = findVolume(r1) print("The volume of a sphere with radius",r1,"is equal to ",round(ans,3)) ans = findVolume(r2) print("The volume of a sphere with radius",r2,"is equal to ",round(ans,3)) ans = findVolume(r3) print("The volume of a sphere with radius",r3,"is equal to ",round(ans,3))
The volume of a sphere with radius 4.6 is equal to 408.187 The volume of a sphere with radius 7.2 is equal to 1565.248 The volume of a sphere with radius 9.7 is equal to 3827.373

Problem 4: Simple Interest

p = 210 i = 0.09 t = 10/12 A = p * (1 + i * t) print("Amount repaid: ${:.2f}".format(A))
Amount repaid: $225.75

Problem 5: Compound Interest

P = 400 i = 0.05 t = 3 A = p * (1 + i * t) print("Amount repaid: ${:.2f}".format(A))
Amount repaid: $241.50

Problem 6: Annuity

PMT = 2000 i = 0.04 n = 18 k = (1 + i) **n j = (k - 1) / i A = PMT * j txt="Balance after 18 years: ${G:.2f}" print(txt.format(G = A))
Balance after 18 years: $51290.83

Problem 7: Probability

prob_A = 0.6 prob_B = 0.3 prob_A_and_B = prob_A * prob_B prob_A_or_B = (prob_A + prob_B - prob_A_and_B) print("Probability that both people are late: ", prob_A_and_B) print("Probability that at least one person is late: ", prob_A_or_B)
Probability that both people are late: 0.18 Probability that at least one person is late: 0.72

Problem 8: Constucting an Output Message

first = " first" last = " last" sid = " 1234" msg = f"First Name:{first}\nLast Name:{last}\nStudent ID:{sid}" print(msg)
First Name: first Last Name: last Student ID: 1234
print(len(msg)) print(len(msg)-3)
53 50