180 KiB
180 KiB
In [1]:
# You may need to run this cell at the beginning of each new session
!pip install requests
# This will just take a few seconds
import requests
# Get the library from GitHub
url = 'https://raw.githubusercontent.com/edatfreecodecamp/python-math/main/math-code-test-c.py'
r = requests.get(url)
# Save the library in a local working directory
with open('math_code_test_c.py', 'w') as f:
f.write(r.text)
# Now you can import the library
import math_code_test_c as test
# This will tell you if the code works
test.step00()Requirement already satisfied: requests in /usr/local/lib/python3.8/dist-packages (2.31.0) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.8/dist-packages (from requests) (3.2.0) Requirement already satisfied: idna<4,>=2.5 in /usr/lib/python3/dist-packages (from requests) (2.8) Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/lib/python3/dist-packages (from requests) (1.25.8) Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3/dist-packages (from requests) (2019.11.28) [33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv[0m[33m [0m [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m A new release of pip is available: [0m[31;49m23.1.2[0m[39;49m -> [0m[32;49m23.2.1[0m [1m[[0m[34;49mnotice[0m[1;39;49m][0m[39;49m To update, run: [0m[32;49mpython3 -m pip install --upgrade pip[0m Code test passed Go on to the next step
In [4]:
import matplotlib.pyplot as plt
import numpy as np
xmin = -10
xmax = 10
ymin = - 10
ymax = 10
points = 2*(xmax-xmin)
x = np.linspace(xmin,xmax,points)
fig, ax = plt.subplots()
plt.axis([xmin,xmax,ymin,ymax]) # window size
plt.plot([xmin,xmax],[0,0],'b') # blue x axis
plt.plot([0,0],[ymin,ymax], 'b') # blue y axis
plt.title("y > 2x")
y1 = 2*x
plt.plot(x, y1)
# Only change code below this line
plt.fill_between(x, y1, ymax)
plt.show()
# Only change code above this line
import math_code_test_c as test
test.step01(In[-1].split('# Only change code above this line')[0])Code test passed Go on to the next step
In [7]:
import matplotlib.pyplot as plt
import numpy as np
xmin = -10
xmax = 10
ymin = - 10
ymax = 10
points = 2*(xmax-xmin)
x = np.linspace(xmin,xmax,points)
fig, ax = plt.subplots()
plt.axis([xmin,xmax,ymin,ymax]) # window size
plt.plot([xmin,xmax],[0,0],'b') # blue x axis
plt.plot([0,0],[ymin,ymax], 'b') # blue y axis
y1 = 2*x
# Only change the next line:
plt.plot(x, y1, 'r--')
plt.fill_between(x, y1, ymin)
plt.show()
# Only change code above this line
import math_code_test_c as test
test.step02(In[-1].split('# Only change code above this line')[0])Code test passed Go on to the next step
In [9]:
import matplotlib.pyplot as plt
import numpy as np
xmin = -10
xmax = 10
ymin = - 10
ymax = 10
points = 2*(xmax-xmin)
x = np.linspace(xmin,xmax,points)
fig, ax = plt.subplots()
plt.axis([xmin,xmax,ymin,ymax]) # window size
plt.plot([xmin,xmax],[0,0],'b') # blue x axis
plt.plot([0,0],[ymin,ymax], 'b') # blue y axis
# Only change the lines indicated below
# line 1
y1 = x+6
plt.plot(x, y1)
plt.fill_between(x, y1, 10, facecolor='blue') # change this line
# line 2
y2 = x+3
plt.plot(x, y2)
plt.fill_between(x, y2, y1, facecolor='green') # change this line
# line 3
y3 = x-1
plt.plot(x, y3)
plt.fill_between(x, y3, y2, facecolor='yellow') # change this line
# line 4
y4 = x-4
plt.plot(x, y4)
plt.fill_between(x, y4, y3, facecolor='red') # change this line
plt.show()
# Only change code above this line
import math_code_test_c as test
test.step03(In[-1].split('# Only change code above this line')[0])Code test passed Go on to the next step
In [10]:
from sympy import symbols, Eq
x = symbols('x')
eq1 = Eq(-2*x**3 , -16)
display(eq1)
# Just run this code
import math_code_test_c as test
test.step00()\displaystyle - 2 x^{3} = -16Code test passed Go on to the next step
In [12]:
from IPython.display import display, Math
from sympy import *
x,y = symbols('x y')
a = int(input("Enter coefficient A: "))
b = int(input("Enter coefficient B: "))
# continue this to prompt for variables c and d
c = int(input("Enter coefficient C: "))
d = int(input("Enter coefficient D: "))
# change the next line to display the full polynomial
y = a*x**3 + b*x**2 + c*x + d
print("Here is your equation:")
display(Math("y = " + latex(y)))
# Only change code above this line
import math_code_test_c as test
test.step05(In[-1].split('# Only change code above this line')[0])Enter coefficient A: 3 Enter coefficient B: 56 Enter coefficient C: 2 Enter coefficient D: 6
Here is your equation:
\displaystyle y = 3 x^{3} + 56 x^{2} + 2 x + 6Code test passed Go on to the next step
In [14]:
%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
# Change the next line:
def f(a, b, c):
plt.axis([-10,10,-10,10]) # window size
plt.plot([-10,10],[0,0],'k') # blue x axis
plt.plot([0,0],[-10,10], 'k') # blue y axis
x = np.linspace(-10, 10, 200)
plt.plot(x, a*x**2+b*x+c) # Change this line
plt.show()
# Change the next line:
interactive_plot = interactive(f, a=(-9, 9), b=(-9, 9), c=(-9, 9))
display(interactive_plot)
# Only change code above this line
import math_code_test_c as test
test.step06(In[-1].split('# Only change code above this line')[0])interactive(children=(IntSlider(value=0, description='a', max=9, min=-9), IntSlider(value=0, description='b', …
Code test passed Go on to the next step
In [16]:
%matplotlib inline
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
xmin = -10
xmax = 10
ymin = -100
ymax = 100
def f(a, b):
plt.axis([xmin,xmax,ymin,ymax]) # window size
plt.plot([xmin,xmax],[0,0],'k') # x axis
plt.plot([0,0],[ymin,ymax], 'k') # y axis
x = np.linspace(xmin, xmax, 1000)
plt.plot(x, a*b**x)
plt.show()
# Only change the next line:
interactive_plot = interactive(f, a=(-9, -1), b=(1, 9))
display(interactive_plot)
# Only change code above this line
import math_code_test_c as test
test.step07(In[-1].split('# Only change code above this line')[0])interactive(children=(IntSlider(value=-5, description='a', max=-1, min=-9), IntSlider(value=5, description='b'…
Code test passed Go on to the next step
In [17]:
p = float(input("Starting amount = "))
r = float(input("Enter the percentage rate, converted to a decimal: "))
t = float(input("How many years will this investment grow? "))
# Change the next line to calculate the annuity
a = p*(1+r)**t
print("The annuity is ", a)
# Only change code above this line
import math_code_test_c as test
test.step08(In[-1].split('# Only change code above this line')[0])Starting amount = 100 Enter the percentage rate, converted to a decimal: 0.06 How many years will this investment grow? 5
The annuity is 133.82255776000002 Code test passed Go on to the next step
In [18]:
p = float(input("Starting amount = "))
r = float(input("Enter the percentage rate, converted to a decimal: "))
t = float(input("How many years will this decrease continue? "))
# Change the next line to calculate the final amount
a = p*(1-r)**t
print("The final amount is ", a)
# Only change code above this line
import math_code_test_c as test
test.step09(In[-1].split('# Only change code above this line')[0])Starting amount = 2323 Enter the percentage rate, converted to a decimal: 0.12 How many years will this decrease continue? 27
The final amount is 73.63450265803228 Code test passed Go on to the next step
In [20]:
p = float(input("Starting amount: "))
r = float(input("Percentage rate, converted to a decimal: "))
t = float(input("Number of years this investment will grow: "))
n = int(input("Number of times compounded per year: "))
# Change the next line to calculate the annuity
annuity = p*(1+(r/n))**(n*t)
print("The annuity is ", annuity)
# Only change code above this line
import math_code_test_c as test
test.step10(In[-1].split('# Only change code above this line')[0])Starting amount: 100 Percentage rate, converted to a decimal: 0.12 Number of years this investment will grow: 4 Number of times compounded per year: 4
The annuity is 160.47064390987882 Code test passed Go on to the next step
In [22]:
import math
p = float(input("Principle: "))
r = float(input("Rate: "))
t = int(input("Time: "))
n = int(input("N: "))
# Only change the following three formulas:
a_annual = p*(1+r)**t
a_n_times = p*(1+r/n)**(n*t)
a_continuous = p*math.e**(r*t) # use math.e in this formula
# Only change the code above this line
print("Compounded annually, anuity = ", a_annual)
print("Compounded ", n, "times per year, anuity = ", a_n_times)
print("Compounded continuously, anuity = ", a_continuous)
# Only change code above this line
import math_code_test_c as test
test.step11(In[-1].split('# Only change code above this line')[0])Principle: 100 Rate: 0.08 Time: 5 N: 12
Compounded annually, anuity = 146.93280768000005 Compounded 12 times per year, anuity = 148.9845708301605 Compounded continuously, anuity = 149.18246976412703 Code test passed Go on to the next step
In [23]:
p = float(input("Starting amount: "))
r = float(input("Annual percentage rate: "))
t = int(input("Number of years: "))
monthly = float(input("Monthly contribution: "))
# Hint: keep updating this annuity variable in the loop
annuity = p
# Each iteration of the loop represents one month
for a in range(12*t):
annuity = annuity + monthly
# Change the next line to calculate the interest
interest = annuity * (r/12)
annuity = annuity + interest
# Keep this line:
print("Annuity = ", annuity)
# Only change code above this line
import math_code_test_c as test
test.step12(p,r,t,monthly,annuity)Starting amount: 100 Annual percentage rate: 0.08 Number of years: 5 Monthly contribution: 10
Annuity = 888.6515903655937 Code test passed Go on to the next step
In [33]:
p = float(input("Amount borrowed: "))
r = float(input("Annual percentage rate: "))
t = float(input("Number of years: "))
# Write your code here and change the pmt variable
common = (1 + (r/12))**(12*t)
pmt = p*((r/12)*common)/(common-1)
print("Monthly payment = $", pmt)
# Only change code above this line
import math_code_test_c as test
test.step13(p,r,t,pmt)Amount borrowed: 1000 Annual percentage rate: 0.12 Number of years: 5
Monthly payment = $ 22.24444768490176 Code test passed Go on to the next step
In [34]:
import math
base = float(input("base: "))
result = float(input("result: "))
# Just change the next line:
exp = math.log(result, base)
print("exponent = ", exp)
# Only change code above this line
import math_code_test_c as test
test.step14(In[-1].split('# Only change code above this line')[0])base: 2 result: 24
exponent = 4.584962500721157 Code test passed Go on to the next step
In [35]:
import math
r = float(input("Enter the annual rate as a decimal: \n"))
t = math.log(2)/r
print("Your money will double in ", t, " years")
# Just run this code
import math_code_test_c as test
test.step00()Enter the annual rate as a decimal: 0.05
Your money will double in 13.862943611198904 years Code test passed Go on to the next step
In [37]:
import math
n = input('Enter a number with several digits or several decimal places: ')
n = float(n)
# Run the code then change the next line:
exp = math.log(n, 10)
# This avoids a weird Python quirk:
if n==1000:
exp = 3;
print("exponent = ", exp)
# Only change code above this line
import math_code_test_c as test
test.step16(In[-1].split('# Only change code above this line')[0])Enter a number with several digits or several decimal places: 1232.43524
exponent = 3.090764107949619 Code test passed Go on to the next step
In [39]:
a = 156000000000
b = 0.000000000413
# Change the code below this line
a1 = 1.56
a2 = 11
b1 = 4.13
b2 = -10
print(a, " = ", a1, "* 10^", a2)
print(b, " = ", b1, "* 10^", b2)
# Only change code above this line
import math_code_test_c as test
test.step17(b1,b2)156000000000 = 1.56 * 10^ 11 4.13e-10 = 4.13 * 10^ -10 Code test passed Go on to the next step
In [47]:
import math
a = .00000000000234
b = 12300000000000
# Use these three lines as a model
x1 = math.floor(math.log(a,10))
n1 = round(a*10**(-x1),2)
print("a = ", n1, "* 10^", x1)
# Change the next two lines to match the model
x2 = math.floor(math.log(b, 10))
n2 = round(b*10**(-x2), 2)
print("b = ", n2, "* 10^", x2)
# Only change code above this line
import math_code_test_c as test
test.step18(In[-1].split('# Only change code above this line')[0])a = 2.34 * 10^ -12 b = 1.23 * 10^ 13 Code test passed Go on to the next step
In [48]:
import math
a = float(input('Enter a number to convert to scientific notation: '))
# write your code here
n = math.floor(math.log(a, 10))
x = round(a/10**n, 2)
print(f"a = {x}*10^{n}")
# Only change code above this line
import math_code_test_c as test
test.step19(In[-1].split('# Only change code above this line')[0])Enter a number to convert to scientific notation: 0.005
a = 5.0*10^-3 You should use x = math.floor(math.log(a,10)) in your code You should use n = round(a*10**(-x),2) in your code
In [50]:
import matplotlib.pyplot as plt
import numpy as np
import math
xmin = -10
xmax = 10
ymin = -10
ymax = 10
plt.axis([xmin,xmax,ymin,ymax]) # window size
plt.plot([xmin,xmax],[0,0],'k') # x axis
plt.plot([0,0],[ymin,ymax], 'k') # y axis
# Same x values for two lines
x1 = np.linspace(xmin, xmax, 1000)
# Blue line for y = e^x
plt.plot(x1, 2**x1, 'b') # Change this line
# Red line for y = x
plt.plot(x1, x1, 'r')
# Different x values for y = log(x) because x > 0
x2 = np.linspace(.001, xmax, 500)
# Green line for y = log(x)
plt.plot(x2, np.log2(x2), 'g') # Change this line
plt.show()
# Only change code above this line
import math_code_test_c as test
test.step20(In[-1].split('# Only change code above this line')[0])Code test passed Go on to the next step
In [58]:
import math
decimal = float(input("Enter the hydrogen concentration as a decimal number: "))
# Write your code here
h = math.ceil(-math.log(decimal, 10))
print("pH = ", h)
# Only change code above this line
import math_code_test_c as test
test.step21(In[-1].split('# Only change code above this line')[0])Enter the hydrogen concentration as a decimal number: 0.007
pH = 3 Code test passed
In [65]:
# Write your code here
def monthly_mortage_payment():
print("Provide following information to calculate monthly mortgage payment:")
p = float(input("Loan amount: "))
r = float(input("Rate of inerest (in decimal): "))
t = float(input("Loan duration (in years): "))
common = (1+ (r/12))**(t*12)
monthly_payment = p * (((r/12)*common)/(common-1))
print("Monthly payment will be:", monthly_payment)
def retirement_corpus():
print("Provide following information to estimate your retirement savings:")
s = float(input("Current savings: "))
r = float(input("Estimated rate of return on investments (in decimal): "))/12
m = float(input("Monthly savings: "))
t = float(input("Number of years till retirement: "))
for i in range(int(t*12)):
s += m
s = s + (s*r)
print("Estimated retirement savings: ", s)
# This step does not have a testProvide following information to estimate your retirement savings:
Current savings: 0 Estimated rate of return on investments (in decimal): 0.05 Monthly savings: 1300 Number of years till retirement: 40
Estimated retirement savings: 1992092.1458609407
In [69]:
# Write your code here
def f(a, b, c):
xlo = -100
xhi = 100
ylo = -100
yhi = 100
plt.clf()
fig = plt.subplot()
plt.axis([xlo, xhi, ylo, yhi])
plt.plot([xlo, xhi], [0, 0], "black")
plt.plot([0, 0], [ylo, yhi], "black")
x = np.linspace(xlo, xhi, (xhi-xlo)*5)
y = a*x**2 + b*x + c
plt.plot(x, y)
plt.grid()
plt.show()
interactive_graph = interactive(f, a=(-10, 10, 0.1), b=(-10, 10, 0.1), c=(-10, 10, 0.1))
interactive_graph
# This step does not have a testOut [69]:
interactive(children=(FloatSlider(value=0.0, description='a', max=10.0, min=-10.0), FloatSlider(value=0.0, des…
In [72]:
# Write your code here
def time_to_double():
r = float(input("Give rate of interest/return (in decimal) to find time to double: "))
t = round(math.log(2)/r, 2)
print(f"It will take {t} years for amount to double.")
# This step does not have a testGive rate of interest/return (in decimal) to find time to double: 0.05
It will take 13.86 years for amount to double.
In [76]:
# Write your code here
def time_to_double():
print("Provide following information to determine needed rate of grwoth for given amount to reach desired amount in given time (years): ")
p = float(input("Current amount: "))
a = float(input("Desired future amount: "))
t = float(input("Number of years: "))
r = round(math.log(a/p)/t, 2)
print(f"{r*100}% rate of growth is needed for {p} to reach {a} in {t} years.")
rate_of_grwoth()Provide following information to determine needed rate of grwoth for given amount to reach desired amount in given time (years):
Current amount: 100 Desired future amount: 200 Number of years: 13.8
5.0% rate of growth is needed for 100.0 to reach 200.0 in 13.8 years.
In [80]:
options = [
(monthly_mortage_payment, "Mortgage payment - given principle, rate, time"),
(retirement_corpus, "Retirement account balance at time of retirement"),
(rate_of_grwoth, "Rate of growth - given starting value, time, and ending value"),
(time_to_double, "Time required for money to double - given the raten")
]
print("What would you like to do?")
[print(o[1]) for o in options]
selected = int(input())
if selected >= 1 and selected <= len(options):
options[selected-1][0]()What would you like to do? Mortgage payment - given principle, rate, time Retirement account balance at time of retirement Rate of growth - given starting value, time, and ending value Time required for money to double - given the raten
1
Provide following information to calculate monthly mortgage payment:
Loan amount: 1000000 Rate of inerest (in decimal): 0.06 Loan duration (in years): 30
Monthly payment will be: 5995.505251527569
In [ ]: