diff --git a/python/calculators/_helpers/__init__.py b/python/calculators/_helpers/__init__.py new file mode 100644 index 0000000..91403f4 --- /dev/null +++ b/python/calculators/_helpers/__init__.py @@ -0,0 +1,3 @@ +from .intInput import intInput +from .floatInput import floatInput +from .listInput import listInput diff --git a/python/calculators/_helpers/floatInput.py b/python/calculators/_helpers/floatInput.py new file mode 100644 index 0000000..adf25c5 --- /dev/null +++ b/python/calculators/_helpers/floatInput.py @@ -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') diff --git a/python/calculators/_helpers/intInput.py b/python/calculators/_helpers/intInput.py new file mode 100644 index 0000000..b6fd0c3 --- /dev/null +++ b/python/calculators/_helpers/intInput.py @@ -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') diff --git a/python/calculators/_helpers/listInput.py b/python/calculators/_helpers/listInput.py new file mode 100644 index 0000000..9b00e6e --- /dev/null +++ b/python/calculators/_helpers/listInput.py @@ -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!') diff --git a/python/calculators/binomial distribution.py b/python/calculators/binomial distribution.py index 89a1260..c270974 100644 --- a/python/calculators/binomial distribution.py +++ b/python/calculators/binomial distribution.py @@ -1,5 +1,6 @@ import operator as op from functools import reduce +from _helpers import intInput, floatInput def nCr(n, r): r = min(r, n-r) @@ -9,6 +10,12 @@ def nCr(n, r): def binomial(x, n, 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) diff --git a/python/calculators/karatsuba algorithm.py b/python/calculators/karatsuba algorithm.py index 3ea4151..0e911a3 100644 --- a/python/calculators/karatsuba algorithm.py +++ b/python/calculators/karatsuba algorithm.py @@ -1,3 +1,5 @@ +from _helpers import intInput + def karatsuba(x, y): xLen = len(str(x)) yLen = len(str(y)) @@ -18,25 +20,16 @@ def karatsuba(x, y): # plug it into the formula return (z2 * 10 ** (n * 2)) + ((z1 - z2 - z0) * (10 ** n)) + z0 -# helper method to easily take in our inputs -def takeInput(text): - 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') +num1 = intInput('Please enter a number (:') +num2 = intInput('Please enter a number to multiply it by!') # Calculate the result and check if it is right res = num1 * num2 karatsubaRes = karatsuba(num1, num2) print() -print('Quadratic Result: ', res) -print('Karatsuba Result: ', karatsubaRes) +print('Quadratic Result:', res) +print('Karatsuba Result:', karatsubaRes) if res == karatsubaRes: print() diff --git a/python/calculators/pmcc.py b/python/calculators/pmcc.py index b42de72..0aabc83 100644 --- a/python/calculators/pmcc.py +++ b/python/calculators/pmcc.py @@ -1,9 +1,10 @@ from operator import mul import math +from _helpers import listInput def pmcc(x, 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) xy = list(map(mul, x, y)) @@ -16,4 +17,11 @@ def pmcc(x, y): return sxy / math.sqrt(sxx * syy) -print(pmcc([1,2,3,4,5], [5,3,8,7,12])) \ No newline at end of file +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) diff --git a/python/calculators/quadratic nth term.py b/python/calculators/quadratic nth term.py index 0ec75ac..a8d870b 100644 --- a/python/calculators/quadratic nth term.py +++ b/python/calculators/quadratic nth term.py @@ -1,4 +1,5 @@ import operator +from _helpers import listInput def diff(a): return list(map(operator.sub, a[1:], a[:-1])) @@ -9,7 +10,7 @@ def formatNumber(x): else: return x -sequence = [-0.5,1,4.5,10,17.5] +sequence = listInput('Please input a sequence of numbers') row1 = diff(sequence) row2 = diff(row1) diff --git a/python/calculators/readme.md b/python/calculators/readme.md index 92aa39f..bb7e68b 100644 --- a/python/calculators/readme.md +++ b/python/calculators/readme.md @@ -2,7 +2,7 @@ Some extra information on the more complex topics (: -## Karatsuba's Algorithm +## Karatsuba Algorithm ### Useful Links diff --git a/python/calculators/sqrt.py b/python/calculators/sqrt.py index 742b7fb..0756e50 100644 --- a/python/calculators/sqrt.py +++ b/python/calculators/sqrt.py @@ -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): a = 2 while abs((a - (x / a))) > 1: a = (a + (x / a)) / 2 return int(a) -print(sqrt(81)) +num = intInput('Please input a number! (:') +res = sqrt(num) + +print() +print('Square Root:', res) diff --git a/python/calculators/srcc.py b/python/calculators/srcc.py index 4d440e7..e974714 100644 --- a/python/calculators/srcc.py +++ b/python/calculators/srcc.py @@ -1,33 +1,32 @@ import numpy +from _helpers import listInput def rank(x): - y = x.sort() - print(y) - res = [] - for i in x: - res.append(y.index(x[i]) + 1) - return res + x = numpy.array(x) + return x.argsort().argsort() def spearman(a, b): if len(a) != len(b): - print('Both datasets must be the same length!') - return undefined + raise Exception('Both datasets must be the same length!') n = len(a) - # rank set a - aranks = sorted(range(n), reverse=True, key=a.__getitem__) - print(aranks) - - # rank set b + # calculate rankings + aranks = rank(a) branks = rank(b) # work out the difference between ranks d = aranks - branks - d = d**2 - d = numpy.sum(d) + d = numpy.sum(d ** 2) # 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])) \ No newline at end of file +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) diff --git a/python/calculators/stdev.py b/python/calculators/stdev.py index 86b8fb5..2f8859b 100644 --- a/python/calculators/stdev.py +++ b/python/calculators/stdev.py @@ -1,4 +1,5 @@ import math +from _helpers import listInput def sd(x): n = len(x) @@ -7,4 +8,9 @@ def sd(x): return math.sqrt(abs((sum(squared) / n) - (mean**2))) -print(sd([135,230,132,323])) \ No newline at end of file +nums = listInput('Please input a list of numbers') +res = sd(nums) + +print() +print('The list:', nums) +print('Standard Deviation:', res)