Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 138
Image: ubuntu2004
Kernel: Python 3 (system-wide)

Exam 1

  1. a)

def minCost(m, n): total=m*11+n*25 if total<800: return(bool(total<800)) print("Enter number of children", '\n') c=int(input()) print("Enter number of adults", '\n') a=int(input()) if (c+a>50): print("Number of people is too many for a 50 seat theater", '\n') elif (minCost(c, a)): print("Buying individual tickets is cheaper", '\n') else: print("A flat fee of $800 is cheaper", '\n')
Enter number of children
Enter number of adults Number of people is too many for a 50 seat theater
print("Enter a positive integer", '\n') z=int(input()) total=0 for i in range(1, z): total=total+(1/i) print(total)
Enter a positive integer
2.283333333333333
  1. a)

  1. b)

[x for x in range(1, 4644) if (4644%x==0 and x%3==0)]
[3, 6, 9, 12, 18, 27, 36, 54, 108, 129, 258, 387, 516, 774, 1161, 1548, 2322]
  1. a)

def savings(principal, rate, months, contributions): for y in range(months): principal = (principal*(1+rate/12) + contributions) return principal print("Account total after 15 years: $", round(savings(1100, 0.034, 180, 0), 3))
Account total after 15 years: $ 1830.5
  1. b)

c=0 while savings(1100, 0.034, 180, c)<3000: c+=1 print("Minimum monthly deposit to ensure $3000 after 15 years: $", c)
Minimum monthly deposit to ensure $3000 after 15 years: $ 5
  1. a)

for w in range(0,16):
  1. b)

def stairs(s): if s==1: return 1 if s==2: return 2 if s==5: return 5 else: return stairs(s-1)+stairs(s-2)+stairs(s-5) stairs(15)
--------------------------------------------------------------------------- RecursionError Traceback (most recent call last) <ipython-input-88-8b79b06b28c9> in <module> 9 return stairs(s-1)+stairs(s-2)+stairs(s-5) 10 ---> 11 stairs(15) <ipython-input-88-8b79b06b28c9> in stairs(s) 7 return 5 8 else: ----> 9 return stairs(s-1)+stairs(s-2)+stairs(s-5) 10 11 stairs(15) ... last 1 frames repeated, from the frame below ... <ipython-input-88-8b79b06b28c9> in stairs(s) 7 return 5 8 else: ----> 9 return stairs(s-1)+stairs(s-2)+stairs(s-5) 10 11 stairs(15) RecursionError: maximum recursion depth exceeded in comparison
  1. a)

import numpy as np import matplotlib.pyplot as plt x = np.linspace(-5, 5, 500) y=x**3-3*x+7 plt.plot(x,y)
[<matplotlib.lines.Line2D at 0x7f77c9bf6fa0>]
Image in a Jupyter notebook
  1. b)

def p(t): return t**3-3*t+7 def bisection(b, d, f, err): g=(b+d)/2 while g-b > err: if p(g)*p(b)>0: b = g else: d = g g=(b+d)/2 return g bisection()
p(-2)
5
p(0.01)
6.970001
bisection(-2, 0.01, p, 0.01)
0.0021484375000000006