From 21da0d459bae600c023bdb7827a8a23cec53463f Mon Sep 17 00:00:00 2001 From: cole Date: Tue, 24 Dec 2019 01:12:45 -0500 Subject: [PATCH 1/6] Added to maths and strings --- maths/combinations.py | 18 ++++++++++++++++++ maths/gamma.py | 32 ++++++++++++++++++++++++++++++++ maths/radians.py | 24 ++++++++++++++++++++++++ strings/lower.py | 37 +++++++++++++++++++++++++++++++++++++ strings/split.py | 32 ++++++++++++++++++++++++++++++++ strings/upper.py | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 175 insertions(+) create mode 100644 maths/combinations.py create mode 100644 maths/gamma.py create mode 100644 maths/radians.py create mode 100644 strings/lower.py create mode 100644 strings/split.py create mode 100644 strings/upper.py diff --git a/maths/combinations.py b/maths/combinations.py new file mode 100644 index 000000000000..dd1e83c3f5be --- /dev/null +++ b/maths/combinations.py @@ -0,0 +1,18 @@ +from math import factorial + + +def combinations(n,k): + """ + >>> combinations(10,5) + 252 + >>> combinations(6,3) + 20 + >>> combinations(20,5) + 15504 + """ + return int(factorial(n) / ((factorial(k)) * (factorial(n-k)))) + +if __name__ == "__main__": + from doctest import testmod + testmod() + \ No newline at end of file diff --git a/maths/gamma.py b/maths/gamma.py new file mode 100644 index 000000000000..f5208b376b40 --- /dev/null +++ b/maths/gamma.py @@ -0,0 +1,32 @@ +from math import factorial + +def gamma(num): + ''' + >>> gamma(1) + 1 + + >>> gamma(0) + Traceback (most recent call last): + ... + ValueError: math domain error + + >>> gamma(10) + 362880 + + >>> gamma(-199) + Traceback (most recent call last): + ... + ValueError: math domain error + ''' + + + if num < 1: + raise ValueError(" math domain error") + else: + return factorial(num-1) + + +if __name__ == "__main__": + from doctest import testmod + testmod() + \ No newline at end of file diff --git a/maths/radians.py b/maths/radians.py new file mode 100644 index 000000000000..67227b9c87fe --- /dev/null +++ b/maths/radians.py @@ -0,0 +1,24 @@ +from math import pi +''' + +Coverts the given angle from degrees to radians +https://en.wikipedia.org/wiki/Radian +''' +def radians(degree): + + """ + >>> radians(180) + 3.141592653589793 + >>> radians(92) + 1.6057029118347832 + >>> radians(274) + 4.782202150464463 + """ + + return degree / (180/pi) + + +if __name__ == "__main__": + from doctest import testmod + testmod() + \ No newline at end of file diff --git a/strings/lower.py b/strings/lower.py new file mode 100644 index 000000000000..4ebcc8a47244 --- /dev/null +++ b/strings/lower.py @@ -0,0 +1,37 @@ + + +def lower(word): + + ''' + Will convert the entire string to lowecase letters + + >>> lower("wow") + 'wow' + >>> lower("HellZo") + 'hellzo' + >>> lower("WHAT") + 'what' + + >>> lower("wh[]32") + 'wh[]32' + >>> lower("whAT") + 'what' + ''' + lower_string = "" + + for char in word: + # converting to ascii value int value and checking to see if char is a capital letter + # if it is a capital letter it is getting shift by 32 which makes it a lower case letter + if ord(char) >= 65 and ord(char) <= 90: + lower_string += chr(ord(char) + 32) + else: + lower_string += char + + return lower_string + + + +if __name__ == "__main__": + from doctest import testmod + testmod() + \ No newline at end of file diff --git a/strings/split.py b/strings/split.py new file mode 100644 index 000000000000..067be374ba79 --- /dev/null +++ b/strings/split.py @@ -0,0 +1,32 @@ + +def split(string,seperator = " "): + ''' + Will split the string up into all the values seperated by the seperator (defaults to spaces) + + >>> split("apple#banana#cherry#orange",seperator='#') + ['apple', 'banana', 'cherry', 'orange'] + + >>> split("Hello there") + ['Hello', 'there'] + + >>> split("11/22/63",seperator = '/') + ['11', '22', '63'] + + >>> split("12:43:39",seperator = ":") + ['12', '43', '39'] + ''' + + split_words = [] + + last_index = 0 + for index,char in enumerate(string): + if char == seperator: + split_words.append(string[last_index:index]) + last_index = index+1 + elif index+1 == len(string): + split_words.append(string[last_index:index+1]) + return split_words + +if __name__ == "__main__": + from doctest import testmod + testmod() \ No newline at end of file diff --git a/strings/upper.py b/strings/upper.py new file mode 100644 index 000000000000..5b78af76b175 --- /dev/null +++ b/strings/upper.py @@ -0,0 +1,32 @@ +def upper(word): + ''' + Will convert the entire string to uppercase letters + + >>> upper("wow") + 'WOW' + >>> upper("Hello") + 'HELLO' + >>> upper("WHAT") + 'WHAT' + + >>> upper("wh[]32") + 'WH[]32' + ''' + upper_string = "" + + for char in word: + # converting to ascii value int value and checking to see if char is a lower letter + # if it is a capital letter it is getting shift by 32 which makes it a capital case letter + if ord(char) >= 97 and ord(char) <= 122: + upper_string += chr(ord(char) -32) + else: + upper_string += char + + return upper_string + + + +if __name__ == "__main__": + from doctest import testmod + testmod() + \ No newline at end of file From 6ad389e0ddf4406e6f3cadc5b93cd3f908b52aa0 Mon Sep 17 00:00:00 2001 From: cole Date: Wed, 25 Dec 2019 00:16:05 -0500 Subject: [PATCH 2/6] added changes suggest by cclauss --- maths/combinations.py | 7 +++-- maths/gamma.py | 64 +++++++++++++++++++++++++++++++------------ maths/radians.py | 19 ++++++++----- strings/lower.py | 26 ++++++------------ strings/split.py | 23 ++++++++-------- strings/upper.py | 24 ++++++---------- 6 files changed, 92 insertions(+), 71 deletions(-) diff --git a/maths/combinations.py b/maths/combinations.py index dd1e83c3f5be..fd98992e6c16 100644 --- a/maths/combinations.py +++ b/maths/combinations.py @@ -1,7 +1,7 @@ from math import factorial -def combinations(n,k): +def combinations(n, k): """ >>> combinations(10,5) 252 @@ -10,9 +10,10 @@ def combinations(n,k): >>> combinations(20,5) 15504 """ - return int(factorial(n) / ((factorial(k)) * (factorial(n-k)))) + return int(factorial(n) / ((factorial(k)) * (factorial(n - k)))) + if __name__ == "__main__": from doctest import testmod + testmod() - \ No newline at end of file diff --git a/maths/gamma.py b/maths/gamma.py index f5208b376b40..ef5e7dae6187 100644 --- a/maths/gamma.py +++ b/maths/gamma.py @@ -1,32 +1,60 @@ -from math import factorial +import math +from scipy.integrate import quad +from numpy import inf -def gamma(num): - ''' - >>> gamma(1) - 1 - >>> gamma(0) +def gamma(num: float) -> float: + """ + https://en.wikipedia.org/wiki/Gamma_function + In mathematics, the gamma function is one commonly + used extension of the factorial function to complex numbers. + The gamma function is defined for all complex numbers except the non-positive integers + + + >>> gamma(-1) Traceback (most recent call last): ... - ValueError: math domain error - - >>> gamma(10) - 362880 + ValueError: math domain error + - >>> gamma(-199) + + >>> gamma(0) Traceback (most recent call last): ... - ValueError: math domain error - ''' + ValueError: math domain error + + >>> gamma(9) + 40320.0 - if num < 1: - raise ValueError(" math domain error") - else: - return factorial(num-1) + >>> from math import gamma as math_gamma + >>> all(gamma(i)/math_gamma(i) <= 1.000000001 and abs(gamma(i)/math_gamma(i)) > .99999999 for i in range(1, 50)) + True + + + >>> from math import gamma as math_gamma + >>> gamma(-1)/math_gamma(-1) <= 1.000000001 + Traceback (most recent call last): + ... + ValueError: math domain error + + + >>> from math import gamma as math_gamma + >>> gamma(3.3) - math_gamma(3.3) <= 0.00000001 + True + """ + + if num <= 0: + raise ValueError("math domain error") + + return quad(integrand, 0, inf, args=(num))[0] + + +def integrand(x: float, z: float) -> float: + return math.pow(x, z - 1) * math.exp(-x) if __name__ == "__main__": from doctest import testmod + testmod() - \ No newline at end of file diff --git a/maths/radians.py b/maths/radians.py index 67227b9c87fe..3788b3e8a3a0 100644 --- a/maths/radians.py +++ b/maths/radians.py @@ -1,24 +1,29 @@ from math import pi -''' -Coverts the given angle from degrees to radians -https://en.wikipedia.org/wiki/Radian -''' -def radians(degree): +def radians(degree: float) -> float: """ + Coverts the given angle from degrees to radians + https://en.wikipedia.org/wiki/Radian + >>> radians(180) 3.141592653589793 >>> radians(92) 1.6057029118347832 >>> radians(274) 4.782202150464463 + >>> radians(109.82) + 1.9167205845401725 + + >>> from math import radians as math_radians + >>> all(abs(radians(i)-math_radians(i)) <= 0.00000001 for i in range(-2, 361)) + True """ - return degree / (180/pi) + return degree / (180 / pi) if __name__ == "__main__": from doctest import testmod + testmod() - \ No newline at end of file diff --git a/strings/lower.py b/strings/lower.py index 4ebcc8a47244..c3a6e598b9ea 100644 --- a/strings/lower.py +++ b/strings/lower.py @@ -1,8 +1,6 @@ +def lower(word: str) -> str: - -def lower(word): - - ''' + """ Will convert the entire string to lowecase letters >>> lower("wow") @@ -16,22 +14,16 @@ def lower(word): 'wh[]32' >>> lower("whAT") 'what' - ''' - lower_string = "" - - for char in word: - # converting to ascii value int value and checking to see if char is a capital letter - # if it is a capital letter it is getting shift by 32 which makes it a lower case letter - if ord(char) >= 65 and ord(char) <= 90: - lower_string += chr(ord(char) + 32) - else: - lower_string += char - - return lower_string + """ + # converting to ascii value int value and checking to see if char is a capital letter + # if it is a capital letter it is getting shift by 32 which makes it a lower case letter + return "".join( + chr(ord(char) + 32) if 65 <= ord(char) <= 90 else char for char in word + ) if __name__ == "__main__": from doctest import testmod + testmod() - \ No newline at end of file diff --git a/strings/split.py b/strings/split.py index 067be374ba79..727250fe6e9f 100644 --- a/strings/split.py +++ b/strings/split.py @@ -1,6 +1,5 @@ - -def split(string,seperator = " "): - ''' +def split(string: str, seperator: str = " ") -> list: + """ Will split the string up into all the values seperated by the seperator (defaults to spaces) >>> split("apple#banana#cherry#orange",seperator='#') @@ -14,19 +13,21 @@ def split(string,seperator = " "): >>> split("12:43:39",seperator = ":") ['12', '43', '39'] - ''' - + """ + split_words = [] - last_index = 0 - for index,char in enumerate(string): + last_index = 0 + for index, char in enumerate(string): if char == seperator: split_words.append(string[last_index:index]) - last_index = index+1 - elif index+1 == len(string): - split_words.append(string[last_index:index+1]) + last_index = index + 1 + elif index + 1 == len(string): + split_words.append(string[last_index : index + 1]) return split_words + if __name__ == "__main__": from doctest import testmod - testmod() \ No newline at end of file + + testmod() diff --git a/strings/upper.py b/strings/upper.py index 5b78af76b175..59b16096af0b 100644 --- a/strings/upper.py +++ b/strings/upper.py @@ -1,5 +1,5 @@ -def upper(word): - ''' +def upper(word: str) -> str: + """ Will convert the entire string to uppercase letters >>> upper("wow") @@ -11,22 +11,16 @@ def upper(word): >>> upper("wh[]32") 'WH[]32' - ''' - upper_string = "" - - for char in word: - # converting to ascii value int value and checking to see if char is a lower letter - # if it is a capital letter it is getting shift by 32 which makes it a capital case letter - if ord(char) >= 97 and ord(char) <= 122: - upper_string += chr(ord(char) -32) - else: - upper_string += char - - return upper_string + """ + # converting to ascii value int value and checking to see if char is a lower letter + # if it is a capital letter it is getting shift by 32 which makes it a capital case letter + return "".join( + chr(ord(char) - 32) if 97 <= ord(char) <= 122 else char for char in word + ) if __name__ == "__main__": from doctest import testmod + testmod() - \ No newline at end of file From b4935f0170e7d5168b15e7ce9b3e95cf6be16594 Mon Sep 17 00:00:00 2001 From: cole Date: Tue, 14 Jan 2020 22:14:28 -0500 Subject: [PATCH 3/6] added square root function --- maths/square_root.py | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 maths/square_root.py diff --git a/maths/square_root.py b/maths/square_root.py new file mode 100644 index 000000000000..1489e659edb2 --- /dev/null +++ b/maths/square_root.py @@ -0,0 +1,61 @@ +import math + + +def fx(x: float, a: float) -> float: + return math.pow(x, 2) - a + + +def fx_derivative(x: float) -> float: + return 2 * x + + +def get_initial_point(a: float) -> float: + start = 2 + + while start <= a: + start = math.pow(start, 2) + + return start + + +def square_root_iterative(a, max_iter=9999, tolerance=0.00000000000001): + """ + Sqaure root is aproximated using Newtons method. + https://en.wikipedia.org/wiki/Newton%27s_method + + >>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001 for i in range(0, 500)) + True + + >>> square_root_iterative(-1) + Traceback (most recent call last): + ... + ValueError: math domain error + + >>> square_root_iterative(4) + 2.0 + + >>> square_root_iterative(3.2) + 1.788854381999832 + + >>> square_root_iterative(140) + 11.832159566199232 + """ + + if a < 0: + raise ValueError("math domain error") + + value = get_initial_point(a) + + for i in range(max_iter): + prev_value = value + value = value - fx(value, a) / fx_derivative(value) + if abs(prev_value - value) < tolerance: + return value + + return value + + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 1b9ef40405a2e5836b7db50a87e4609a89f67411 Mon Sep 17 00:00:00 2001 From: cole Date: Tue, 14 Jan 2020 22:21:41 -0500 Subject: [PATCH 4/6] Fixed type hinting --- maths/square_root.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/square_root.py b/maths/square_root.py index 1489e659edb2..f4dc6f3ae9e6 100644 --- a/maths/square_root.py +++ b/maths/square_root.py @@ -18,7 +18,9 @@ def get_initial_point(a: float) -> float: return start -def square_root_iterative(a, max_iter=9999, tolerance=0.00000000000001): +def square_root_iterative( + a: float, max_iter: int = 9999, tolerance: float = 0.00000000000001 +) -> float: """ Sqaure root is aproximated using Newtons method. https://en.wikipedia.org/wiki/Newton%27s_method From bcc02eff8fcae6730be3eb5ae24bcd7f60b94777 Mon Sep 17 00:00:00 2001 From: cole Date: Tue, 14 Jan 2020 22:34:30 -0500 Subject: [PATCH 5/6] fixed type error --- maths/square_root.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/square_root.py b/maths/square_root.py index f4dc6f3ae9e6..9831a3a8fe53 100644 --- a/maths/square_root.py +++ b/maths/square_root.py @@ -9,7 +9,7 @@ def fx_derivative(x: float) -> float: return 2 * x -def get_initial_point(a: float) -> float: +def get_initial_point(a: float) -> int: start = 2 while start <= a: From b472c0ab4516f62cd2627be86d6450f393def418 Mon Sep 17 00:00:00 2001 From: cole Date: Tue, 14 Jan 2020 22:42:30 -0500 Subject: [PATCH 6/6] Fixed another type error --- maths/square_root.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/square_root.py b/maths/square_root.py index 9831a3a8fe53..46e791ab5662 100644 --- a/maths/square_root.py +++ b/maths/square_root.py @@ -9,8 +9,8 @@ def fx_derivative(x: float) -> float: return 2 * x -def get_initial_point(a: float) -> int: - start = 2 +def get_initial_point(a: float) -> float: + start = 2.0 while start <= a: start = math.pow(start, 2)