From a26ae00b2462490b9deb11f699efc3c886738e51 Mon Sep 17 00:00:00 2001 From: Cole Mollica <30614241+coleman2246@users.noreply.github.com> Date: Wed, 8 Jan 2020 08:18:17 -0500 Subject: [PATCH] Added to maths and strings (#1642) * Added to maths and strings * added changes suggest by cclauss --- maths/combinations.py | 19 ++++++++++++++ maths/gamma.py | 60 +++++++++++++++++++++++++++++++++++++++++++ maths/radians.py | 29 +++++++++++++++++++++ strings/lower.py | 29 +++++++++++++++++++++ strings/split.py | 33 ++++++++++++++++++++++++ strings/upper.py | 26 +++++++++++++++++++ 6 files changed, 196 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 000000000..fd98992e6 --- /dev/null +++ b/maths/combinations.py @@ -0,0 +1,19 @@ +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() diff --git a/maths/gamma.py b/maths/gamma.py new file mode 100644 index 000000000..ef5e7dae6 --- /dev/null +++ b/maths/gamma.py @@ -0,0 +1,60 @@ +import math +from scipy.integrate import quad +from numpy import inf + + +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(0) + Traceback (most recent call last): + ... + ValueError: math domain error + + + >>> gamma(9) + 40320.0 + + >>> 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() diff --git a/maths/radians.py b/maths/radians.py new file mode 100644 index 000000000..3788b3e8a --- /dev/null +++ b/maths/radians.py @@ -0,0 +1,29 @@ +from math import pi + + +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) + + +if __name__ == "__main__": + from doctest import testmod + + testmod() diff --git a/strings/lower.py b/strings/lower.py new file mode 100644 index 000000000..c3a6e598b --- /dev/null +++ b/strings/lower.py @@ -0,0 +1,29 @@ +def lower(word: str) -> str: + + """ + 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' + """ + + # 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() diff --git a/strings/split.py b/strings/split.py new file mode 100644 index 000000000..727250fe6 --- /dev/null +++ b/strings/split.py @@ -0,0 +1,33 @@ +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='#') + ['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() diff --git a/strings/upper.py b/strings/upper.py new file mode 100644 index 000000000..59b16096a --- /dev/null +++ b/strings/upper.py @@ -0,0 +1,26 @@ +def upper(word: str) -> str: + """ + Will convert the entire string to uppercase letters + + >>> upper("wow") + 'WOW' + >>> upper("Hello") + 'HELLO' + >>> upper("WHAT") + 'WHAT' + + >>> upper("wh[]32") + 'WH[]32' + """ + + # 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()