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

COSC - Homework 03

Kyle Anderson

Problem 1: Calculating Exam Scores

correct = ['D','B','C','A','C','D','A','C','C','B','D','A','B','D','C','D','C','D','C','A','B','D','C','B','A'] answers1 = ['A','B','C','A','B','D','A','A','C','B','D','A','D','C','C','B','C','D','B','A','D','D','C','C','A'] answers2 = ['D','A','C','A','B','D','A','C','C','B','D','A','B','D','A','D','C','D','C','A','B','C','C','B','A'] count1 = 0 count2 = 0 for i in range(0,25): if(correct[i] == answers1[i]): count1 = count1 + 1 if(correct[i] == answers2[i]): count2 = count2 + 1 grade1 = int ((count1 / 25) * 100) grade2 = int ((count2 / 25) * 100) print("Student 1 Grade: "+str(grade1)+"%") print("Student 2 Grade: "+str(grade2)+"%")
Student 1 Grade: 64% Student 2 Grade: 84%

Problem 2: Calculating Price of a Bulk Order

quantaties = [84, 100, 126, 150, 186, 200, 216, 248] for i in quantaties: if i <+ 100: cost = i * 350 elif i <= 200: cost = (100 * 350) + (i - 300) * 300 elif i > 200: cost = (100 * 350) + (100 * 300) + (i - 200) * 250 print("The cost for an order of",i,"widgets is $" + str(cost)+'.')
The cost for an order of 248 widgets is $77000.

Problem 3: Creating an Amortization Schedule

balance = 1000 i = 0.05 pmt = 150 n = 0 while (balance > 0): n = n + 1 balance = balance * (i + 1) - pmt round(balance,2) if (balance <= 0): final_pmt = balance + pmt print("The amount of the final payment will be",final_pmt) balance = 0 else: print("The balance at the end of the year",n,'is',balance)
The balance at the end of the year 1 is 900.0 The balance at the end of the year 2 is 795.0 The balance at the end of the year 3 is 684.75 The balance at the end of the year 4 is 568.9875000000001 The balance at the end of the year 5 is 447.4368750000001 The balance at the end of the year 6 is 319.80871875000014 The balance at the end of the year 7 is 185.79915468750016 The balance at the end of the year 8 is 45.08911242187517 The amount of the final payment will be 47.34356804296894

Problem 4: Hailstone Sequences

def hailStoneSequence(n): ls = [n] while n != 1: if(n%2 == 0): n = n//2 ls.append(n) else: n = n*3 + 1 ls.append(n) return ls hs15 = hailStoneSequence(15) hs17 = hailStoneSequence(17) hs22 = hailStoneSequence(22) print('hs15: ') for i in hs15: print(i,end = ' ') print('\nhs17: ') for i in hs17: print(i,end = ' ') print('\nhs22: ') for i in hs22: print(i,end = ' ') print()#newline
hs15: 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1 hs17: 17 52 26 13 40 20 10 5 16 8 4 2 1 hs22: 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

Problem 5: Preadtor/Prey Model

r_pop = 1000 w_pop = 200 print(f"{'Year':10s}{'R_pop':8s}{'W_pop'}") print(f"-"*24) print(f"{0:<10d}{r_pop:<8d}{w_pop}") r_pop,w_pop = 1100,220 print(f"{1:<10d}{r_pop:<8d}{w_pop}") r_pop,w_pop = 1166, 249 print(f"{2:<10d}{r_pop:<8d}{w_pop}") year = 2 while r_pop>0 and w_pop>0: r_pop_prev = r_pop w_pop_prev = w_pop r_pop = 1.5*r_pop_prev - 0.002*r_pop_prev*w_pop_prev w_pop = 0.8*w_pop_prev + 0.0003*r_pop_prev*w_pop_prev year = year+1 print(f"{year:<10d}{round(r_pop):<8d}{round(w_pop)}")
Year R_pop W_pop ------------------------ 0 1000 200 1 1100 220 2 1166 249 3 1168 286 4 1084 329 5 911 371 6 692 398 7 487 401 8 340 379 9 252 342 10 206 300 11 185 258 12 182 221 13 193 189 14 217 162 15 255 140 16 311 123 17 390 110 18 499 101 19 649 96 20 849 95 21 1112 100 22 1445 114 23 1840 140 24 2244 189 25 2515 279 26 2369 434 27 1497 655 28 283 819 29 -39 725

Problem 6: Finding a Root of a Polynomial Function

x1 = 0 x2 = 5 def f(x): result = x**3 - 4*(x**2) + 3*x - 4 return result val1 = f(x1) val2 = f(x2) n = 0 limit = 0.000001 while(abs(val1) > limit or abs(val2) > limit): n += 1 new_x = (x1 + x2)/2 new_val = f(new_x) if new_val < 0: x1 = new_x val1 = new_val else: x2 = new_x val2 = new_val new_x = round(new_x,7) print(f"The approximate solution is x = {new_x}.") print(f"The algorithm took {n} iterations to converge.")
The approximate solution is x = 3.4675039. The algorithm took 25 iterations to converge.

Problem 7: Grade Database

students = { 146832:{'first':'Brendan', 'last':'Small'}, 147354:{'first':'Melissa', 'last':'Robbins'}, 149126:{'first':'Jason', 'last':'Penopolis'}, 149735:{'first':'Fenton', 'last':'Mulley'} } courses = { 'ENGL 101':'Composition I', 'ENGL 104':'Composition II', 'MATH 117':'College Algebra', 'MATH 151':'Calculus I', 'CHEM 103':'General Chemistry I', 'ECON 201':'Macroeconomics' } sid = [149126, 146832, 147354, 149735, 149126, 146832, 146832, 149735, 149126, 147354, 147354, 149735] cid = ['ENGL 101', 'MATH 117', 'ENGL 104', 'CHEM 103', 'MATH 117', 'ECON 201', 'ENGL 101', 'ENGL 101', 'CHEM 103', 'ENGL 104', 'MATH 151', 'MATH 117'] grade = ['D', 'C', 'B', 'A', 'B', 'C', 'A', 'F', 'B', 'A', 'A', 'C'] dash = '-' * 69 print("SID First Last CID Course Name Grade") print(dash) for i in range (0,12): roll = sid[i] code = cid[i] print('{:<8d}{:<9s}{:<10s}{:<11s}{:<22s}{:<1s}'.format(sid[i],students[roll]['first'],students[roll]['last'],cid[i],courses[code],grade[i]))
SID First Last CID Course Name Grade --------------------------------------------------------------------- 149126 Jason Penopolis ENGL 101 Composition I D 146832 Brendan Small MATH 117 College Algebra C 147354 Melissa Robbins ENGL 104 Composition II B 149735 Fenton Mulley CHEM 103 General Chemistry I A 149126 Jason Penopolis MATH 117 College Algebra B 146832 Brendan Small ECON 201 Macroeconomics C 146832 Brendan Small ENGL 101 Composition I A 149735 Fenton Mulley ENGL 101 Composition I F 149126 Jason Penopolis CHEM 103 General Chemistry I B 147354 Melissa Robbins ENGL 104 Composition II A 147354 Melissa Robbins MATH 151 Calculus I A 149735 Fenton Mulley MATH 117 College Algebra C

Problem 8: Warehouse Inventory

prices = {'p101':37.52, 'p117':56.98, 'p122':43.72, 'p125':48.33, 'p126':52.45} inventory = { 'STL':{'p101':520, 'p117':315, 'p122':117, 'p125':258, 'p126':345}, 'CHI':{'p101':125, 'p117':864, 'p122':231, 'p125':612, 'p126':164}, 'KC' :{'p101':264, 'p117':285, 'p122':772, 'p125':467, 'p126':106} } total_inventory = {} for key in prices.keys(): quantity = 0 for quant in inventory.values(): quantity += quant.get(key) total_inventory[key] = quantity print(f'{key}:{quantity}') total_value = 0 for key, price in prices.items(): quantity = total_inventory.get(key) total_value += price * quantity print(f'The total value of the inventory is ${total_value}')
p101:909 p117:1464 p122:1120 p125:1337 p126:615 The total value of the inventory is $263364.76