Merge branch 'master' into master

This commit is contained in:
Luke Weiler
2020-10-21 14:26:43 -04:00
committed by GitHub
8 changed files with 224 additions and 106 deletions

View File

@@ -95,4 +95,8 @@ from .radian_to_deg import *
from .differentiation import *
from .definite_integral import *
from .is_prime import *
from .bcd_to_decimal import *
from .complex_to_polar import *
from .set_operation import *
from .base_conversion import *
from .curvedSurfaceAreaCylinderFunc import *

View File

@@ -0,0 +1,50 @@
from .__init__ import *
# base from 2 to 36
alpha = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def fromBaseTenTo(n, toBase):
assert type(toBase) == int and toBase >= 2 and toBase <= 36, "toBase({}) must be >=2 and <=36"
# trivial cases
if toBase == 2:
return bin(n)[2:]
elif toBase == 8:
return oct(n)[2:]
elif toBase == 10:
return str(n)
elif toBase == 16:
return hex(n)[2:].upper()
res = alpha[n % toBase]
n = n//toBase
while n > 0:
res = alpha[n % toBase] + res
n = n//toBase
return res
# Useful to check answers, but not needed here
# def toBaseTen(n,fromBase):
# return int(n,fromBase)
def baseConversionFunc(maxNum=60000, maxBase=16):
assert type(
maxNum) == int and maxNum >= 100 and maxNum <= 65536, "maxNum({}) must be >=100 and <=65536".format(maxNum)
assert type(
maxBase) == int and maxBase >= 2 and maxBase <= 36, "maxBase({}) must be >= 2 and <=36".format(maxBase)
n = random.randint(40, maxNum)
dist = [10]*10+[2]*5+[16]*5+[i for i in range(2, maxBase+1)]
# set this way since converting to/from bases 2,10,16 are more common -- can be changed if needed.
bases = random.choices(dist, k=2)
while bases[0] == bases[1]:
bases = random.choices(dist, k=2)
problem = "Convert {} from base {} to base {}.".format(
fromBaseTenTo(n, bases[0]), bases[0], bases[1])
ans = fromBaseTenTo(n, bases[1])
return problem, ans
base_conversion = Generator("Base Conversion", 94, "Convert 152346 from base 8 to base 10.", "54502",
baseConversionFunc)

View File

@@ -0,0 +1,24 @@
from .__init__ import *
def BCDtoDecimalFunc(maxNumber=10000):
n = random.randint(1000, maxNumber)
binstring = ''
while True:
q, r = divmod(n, 10)
nibble = bin(r).replace('0b', "")
while len(nibble) < 4:
nibble = '0' + nibble
binstring = nibble + binstring
if q == 0:
break
else:
n = q
problem = "Integer of Binary Coded Decimal " + str(n) + " is = "
solution = int(binstring, 2)
return problem, solution
bcd_to_decimal = Generator("Binary Coded Decimal to Integer", 91,
"Integer of Binary Coded Decimal b is ", "n", BCDtoDecimalFunc)

View File

@@ -0,0 +1,18 @@
from .__init__ import *
def complexToPolarFunc(minRealImaginaryNum=-20, maxRealImaginaryNum=20):
num = complex(random.randint(minRealImaginaryNum, maxRealImaginaryNum),
random.randint(minRealImaginaryNum, maxRealImaginaryNum))
a = num.real
b = num.imag
r = round(math.hypot(a, b), 2)
theta = round(math.atan2(b, a), 2)
plr = str(r) + "exp(i" + str(theta) + ")"
problem = f"rexp(itheta) = "
solution = plr
return problem, solution
complex_to_polar = Generator("Complex To Polar Form", 92,
"rexp(itheta) = ", "plr", complexToPolarFunc)

View File

@@ -1,24 +1,17 @@
from .__init__ import *
def compoundInterestFunc(maxPrinciple=10000,
maxRate=10,
maxTime=10,
maxPeriod=10):
p = random.randint(100, maxPrinciple)
def compoundInterestFunc(maxPrinciple=10000, maxRate=10, maxTime=10):
p = random.randint(1000, maxPrinciple)
r = random.randint(1, maxRate)
t = random.randint(1, maxTime)
n = random.randint(1, maxPeriod)
A = p * ((1 + (r / (100 * n))**(n * t)))
problem = "Compound Interest for a principle amount of " + str(
p) + " dollars, " + str(
r) + "% rate of interest and for a time period of " + str(
t) + " compounded monthly is = "
solution = round(A, 2)
n = random.randint(1, maxTime)
a = p * (1 + r / 100)**n
problem = "Compound interest for a principle amount of " + \
str(p) + " dollars, " + str(r) + \
"% rate of interest and for a time period of " + str(n) + " year is = "
solution = round(a, 2)
return problem, solution
compound_interest = Generator(
"Compound Interest", 78,
"Compound interest for a principle amount of p dollars, r% rate of interest and for a time period of t years with n times compounded annually is = ",
"A dollars", compoundInterestFunc)
"Compound Interest", 78, "Compound interest for a principle amount of a dollars, b% rate of interest and for a time period of c years is = ", "d dollars", compoundInterestFunc)

View File

@@ -0,0 +1,24 @@
from .__init__ import *
def set_operation(minval=3, maxval=7, n_a=4, n_b=5):
number_variables_a = random.randint(minval, maxval)
number_variables_b = random.randint(minval, maxval)
a = []
b = []
for i in range(number_variables_a):
a.append(random.randint(1, 10))
for i in range(number_variables_b):
b.append(random.randint(1, 10))
a = set(a)
b = set(b)
problem = "Given the two sets a=" + \
str(a) + " ,b=" + str(b) + ".Find the Union,intersection,a-b,b-a and symmetric difference"
solution = "Union is " + str(a.union(b)) + ",Intersection is " + str(a.intersection(b)) + ", a-b is " + str(
a.difference(b)) + ",b-a is " + str(b.difference(a)) + ", Symmetric difference is " + str(a.symmetric_difference(b))
return problem, solution
set_operation = Generator("Union,Intersection,Difference of Two Sets", 93,
"Union,intersection,difference",
"aUb,a^b,a-b,b-a,", set_operation)