mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-30 22:23:42 +00:00
Added to maths and strings (#1642)
* Added to maths and strings * added changes suggest by cclauss
This commit is contained in:
parent
e849578e59
commit
a26ae00b24
19
maths/combinations.py
Normal file
19
maths/combinations.py
Normal file
|
@ -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
maths/gamma.py
Normal file
60
maths/gamma.py
Normal file
|
@ -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
maths/radians.py
Normal file
29
maths/radians.py
Normal file
|
@ -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
strings/lower.py
Normal file
29
strings/lower.py
Normal file
|
@ -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
strings/split.py
Normal file
33
strings/split.py
Normal file
|
@ -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
strings/upper.py
Normal file
26
strings/upper.py
Normal file
|
@ -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()
|
Loading…
Reference in New Issue
Block a user