Let all calculators take input
This commit is contained in:
parent
3bee84cd67
commit
fc3f6a9cfe
12 changed files with 92 additions and 38 deletions
3
python/calculators/_helpers/__init__.py
Normal file
3
python/calculators/_helpers/__init__.py
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
from .intInput import intInput
|
||||||
|
from .floatInput import floatInput
|
||||||
|
from .listInput import listInput
|
8
python/calculators/_helpers/floatInput.py
Normal file
8
python/calculators/_helpers/floatInput.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# helper method to easily take in float inputs
|
||||||
|
def floatInput(text):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
x = float(input(text + '\n'))
|
||||||
|
return x
|
||||||
|
except ValueError:
|
||||||
|
print('You must input a float integer!\n')
|
8
python/calculators/_helpers/intInput.py
Normal file
8
python/calculators/_helpers/intInput.py
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
# helper method to easily take in int inputs
|
||||||
|
def intInput(text):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
x = int(input(text + '\n'))
|
||||||
|
return x
|
||||||
|
except ValueError:
|
||||||
|
print('You must input an integer!\n')
|
10
python/calculators/_helpers/listInput.py
Normal file
10
python/calculators/_helpers/listInput.py
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
# helper method to easily take in float list inputs
|
||||||
|
def listInput(text, delimeter = ','):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
x = input('%s - the delimeter is %s (:\n' % (text, delimeter))
|
||||||
|
x = x.split(delimeter)
|
||||||
|
nums = [float(a) for a in x]
|
||||||
|
return nums
|
||||||
|
except ValueError:
|
||||||
|
print('All of the values must be floats!')
|
|
@ -1,5 +1,6 @@
|
||||||
import operator as op
|
import operator as op
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
from _helpers import intInput, floatInput
|
||||||
|
|
||||||
def nCr(n, r):
|
def nCr(n, r):
|
||||||
r = min(r, n-r)
|
r = min(r, n-r)
|
||||||
|
@ -9,6 +10,12 @@ def nCr(n, r):
|
||||||
|
|
||||||
def binomial(x, n, p):
|
def binomial(x, n, p):
|
||||||
q = 1 - p
|
q = 1 - p
|
||||||
return nCr(n, x) * (p**x) * (q**(n-x))
|
return nCr(n, x) * (p ** x) * (q ** (n-x))
|
||||||
|
|
||||||
print(binomial(5, 100, 0.1))
|
n = intInput('How many trials would you like to execute?')
|
||||||
|
x = intInput('How many times must the outcome happen in these trials?')
|
||||||
|
p = floatInput('What is the probability of success as a float?')
|
||||||
|
res = binomial(x, n, p)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print(res)
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
from _helpers import intInput
|
||||||
|
|
||||||
def karatsuba(x, y):
|
def karatsuba(x, y):
|
||||||
xLen = len(str(x))
|
xLen = len(str(x))
|
||||||
yLen = len(str(y))
|
yLen = len(str(y))
|
||||||
|
@ -18,25 +20,16 @@ def karatsuba(x, y):
|
||||||
# plug it into the formula
|
# plug it into the formula
|
||||||
return (z2 * 10 ** (n * 2)) + ((z1 - z2 - z0) * (10 ** n)) + z0
|
return (z2 * 10 ** (n * 2)) + ((z1 - z2 - z0) * (10 ** n)) + z0
|
||||||
|
|
||||||
# helper method to easily take in our inputs
|
num1 = intInput('Please enter a number (:')
|
||||||
def takeInput(text):
|
num2 = intInput('Please enter a number to multiply it by!')
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
x = int(input(text))
|
|
||||||
return x
|
|
||||||
except ValueError:
|
|
||||||
print('You must input an integer!\n')
|
|
||||||
|
|
||||||
num1 = takeInput('Please enter a number (:\n')
|
|
||||||
num2 = takeInput('Please enter a number to multiply it by!\n')
|
|
||||||
|
|
||||||
# Calculate the result and check if it is right
|
# Calculate the result and check if it is right
|
||||||
res = num1 * num2
|
res = num1 * num2
|
||||||
karatsubaRes = karatsuba(num1, num2)
|
karatsubaRes = karatsuba(num1, num2)
|
||||||
|
|
||||||
print()
|
print()
|
||||||
print('Quadratic Result: ', res)
|
print('Quadratic Result:', res)
|
||||||
print('Karatsuba Result: ', karatsubaRes)
|
print('Karatsuba Result:', karatsubaRes)
|
||||||
|
|
||||||
if res == karatsubaRes:
|
if res == karatsubaRes:
|
||||||
print()
|
print()
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from operator import mul
|
from operator import mul
|
||||||
import math
|
import math
|
||||||
|
from _helpers import listInput
|
||||||
|
|
||||||
def pmcc(x, y):
|
def pmcc(x, y):
|
||||||
if len(x) != len(y):
|
if len(x) != len(y):
|
||||||
raise Exception('List x must be of the same length as list y. List x is {0} values long, whereas list y is {1} values long.'.format(len(x), len(y)))
|
raise Exception('Both datasets must be the same length!')
|
||||||
|
|
||||||
n = len(x)
|
n = len(x)
|
||||||
xy = list(map(mul, x, y))
|
xy = list(map(mul, x, y))
|
||||||
|
@ -16,4 +17,11 @@ def pmcc(x, y):
|
||||||
|
|
||||||
return sxy / math.sqrt(sxx * syy)
|
return sxy / math.sqrt(sxx * syy)
|
||||||
|
|
||||||
print(pmcc([1,2,3,4,5], [5,3,8,7,12]))
|
a = listInput('Please input a list of numbers')
|
||||||
|
b = listInput('Please input a second list of numbers')
|
||||||
|
res = pmcc(a, b)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print('List A:', a)
|
||||||
|
print('List B:', b)
|
||||||
|
print('PMCC:', res)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import operator
|
import operator
|
||||||
|
from _helpers import listInput
|
||||||
|
|
||||||
def diff(a):
|
def diff(a):
|
||||||
return list(map(operator.sub, a[1:], a[:-1]))
|
return list(map(operator.sub, a[1:], a[:-1]))
|
||||||
|
@ -9,7 +10,7 @@ def formatNumber(x):
|
||||||
else:
|
else:
|
||||||
return x
|
return x
|
||||||
|
|
||||||
sequence = [-0.5,1,4.5,10,17.5]
|
sequence = listInput('Please input a sequence of numbers')
|
||||||
row1 = diff(sequence)
|
row1 = diff(sequence)
|
||||||
row2 = diff(row1)
|
row2 = diff(row1)
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Some extra information on the more complex topics (:
|
Some extra information on the more complex topics (:
|
||||||
|
|
||||||
## Karatsuba's Algorithm
|
## Karatsuba Algorithm
|
||||||
|
|
||||||
### Useful Links
|
### Useful Links
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,18 @@
|
||||||
|
"""
|
||||||
|
A square root calculator based on the Babylonian's approach!
|
||||||
|
Created as a part of my Royal Institute masterclass
|
||||||
|
"""
|
||||||
|
|
||||||
|
from _helpers import intInput
|
||||||
|
|
||||||
def sqrt(x):
|
def sqrt(x):
|
||||||
a = 2
|
a = 2
|
||||||
while abs((a - (x / a))) > 1:
|
while abs((a - (x / a))) > 1:
|
||||||
a = (a + (x / a)) / 2
|
a = (a + (x / a)) / 2
|
||||||
return int(a)
|
return int(a)
|
||||||
|
|
||||||
print(sqrt(81))
|
num = intInput('Please input a number! (:')
|
||||||
|
res = sqrt(num)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print('Square Root:', res)
|
||||||
|
|
|
@ -1,33 +1,32 @@
|
||||||
import numpy
|
import numpy
|
||||||
|
from _helpers import listInput
|
||||||
|
|
||||||
def rank(x):
|
def rank(x):
|
||||||
y = x.sort()
|
x = numpy.array(x)
|
||||||
print(y)
|
return x.argsort().argsort()
|
||||||
res = []
|
|
||||||
for i in x:
|
|
||||||
res.append(y.index(x[i]) + 1)
|
|
||||||
return res
|
|
||||||
|
|
||||||
def spearman(a, b):
|
def spearman(a, b):
|
||||||
if len(a) != len(b):
|
if len(a) != len(b):
|
||||||
print('Both datasets must be the same length!')
|
raise Exception('Both datasets must be the same length!')
|
||||||
return undefined
|
|
||||||
|
|
||||||
n = len(a)
|
n = len(a)
|
||||||
|
|
||||||
# rank set a
|
# calculate rankings
|
||||||
aranks = sorted(range(n), reverse=True, key=a.__getitem__)
|
aranks = rank(a)
|
||||||
print(aranks)
|
|
||||||
|
|
||||||
# rank set b
|
|
||||||
branks = rank(b)
|
branks = rank(b)
|
||||||
|
|
||||||
# work out the difference between ranks
|
# work out the difference between ranks
|
||||||
d = aranks - branks
|
d = aranks - branks
|
||||||
d = d**2
|
d = numpy.sum(d ** 2)
|
||||||
d = numpy.sum(d)
|
|
||||||
|
|
||||||
# plug the values into the formula
|
# plug the values into the formula
|
||||||
return 1 - ((6*d) / (n**3 - n))
|
return 1 - ((6 * d) / (n ** 3 - n))
|
||||||
|
|
||||||
print(spearman([1,2,3,5,4], [5,4,3,2,1]))
|
a = listInput('Please input a list of numbers')
|
||||||
|
b = listInput('Please input a second list of numbers')
|
||||||
|
res = spearman(a, b)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print('List A:', a)
|
||||||
|
print('List B:', b)
|
||||||
|
print('SRCC:', res)
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import math
|
import math
|
||||||
|
from _helpers import listInput
|
||||||
|
|
||||||
def sd(x):
|
def sd(x):
|
||||||
n = len(x)
|
n = len(x)
|
||||||
|
@ -7,4 +8,9 @@ def sd(x):
|
||||||
|
|
||||||
return math.sqrt(abs((sum(squared) / n) - (mean**2)))
|
return math.sqrt(abs((sum(squared) / n) - (mean**2)))
|
||||||
|
|
||||||
print(sd([135,230,132,323]))
|
nums = listInput('Please input a list of numbers')
|
||||||
|
res = sd(nums)
|
||||||
|
|
||||||
|
print()
|
||||||
|
print('The list:', nums)
|
||||||
|
print('Standard Deviation:', res)
|
||||||
|
|
Loading…
Reference in a new issue