Skip to content

Added to maths and strings #1642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions maths/combinations.py
Original file line number Diff line number Diff line change
@@ -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()
60 changes: 60 additions & 0 deletions maths/gamma.py
Original file line number Diff line number Diff line change
@@ -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()
29 changes: 29 additions & 0 deletions maths/radians.py
Original file line number Diff line number Diff line change
@@ -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()
29 changes: 29 additions & 0 deletions strings/lower.py
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 33 additions & 0 deletions strings/split.py
Original file line number Diff line number Diff line change
@@ -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()
26 changes: 26 additions & 0 deletions strings/upper.py
Original file line number Diff line number Diff line change
@@ -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()