Skip to content

Commit a26ae00

Browse files
coleman2246cclauss
authored andcommitted
Added to maths and strings (TheAlgorithms#1642)
* Added to maths and strings * added changes suggest by cclauss
1 parent e849578 commit a26ae00

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

maths/combinations.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from math import factorial
2+
3+
4+
def combinations(n, k):
5+
"""
6+
>>> combinations(10,5)
7+
252
8+
>>> combinations(6,3)
9+
20
10+
>>> combinations(20,5)
11+
15504
12+
"""
13+
return int(factorial(n) / ((factorial(k)) * (factorial(n - k))))
14+
15+
16+
if __name__ == "__main__":
17+
from doctest import testmod
18+
19+
testmod()

maths/gamma.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import math
2+
from scipy.integrate import quad
3+
from numpy import inf
4+
5+
6+
def gamma(num: float) -> float:
7+
"""
8+
https://en.wikipedia.org/wiki/Gamma_function
9+
In mathematics, the gamma function is one commonly
10+
used extension of the factorial function to complex numbers.
11+
The gamma function is defined for all complex numbers except the non-positive integers
12+
13+
14+
>>> gamma(-1)
15+
Traceback (most recent call last):
16+
...
17+
ValueError: math domain error
18+
19+
20+
21+
>>> gamma(0)
22+
Traceback (most recent call last):
23+
...
24+
ValueError: math domain error
25+
26+
27+
>>> gamma(9)
28+
40320.0
29+
30+
>>> from math import gamma as math_gamma
31+
>>> all(gamma(i)/math_gamma(i) <= 1.000000001 and abs(gamma(i)/math_gamma(i)) > .99999999 for i in range(1, 50))
32+
True
33+
34+
35+
>>> from math import gamma as math_gamma
36+
>>> gamma(-1)/math_gamma(-1) <= 1.000000001
37+
Traceback (most recent call last):
38+
...
39+
ValueError: math domain error
40+
41+
42+
>>> from math import gamma as math_gamma
43+
>>> gamma(3.3) - math_gamma(3.3) <= 0.00000001
44+
True
45+
"""
46+
47+
if num <= 0:
48+
raise ValueError("math domain error")
49+
50+
return quad(integrand, 0, inf, args=(num))[0]
51+
52+
53+
def integrand(x: float, z: float) -> float:
54+
return math.pow(x, z - 1) * math.exp(-x)
55+
56+
57+
if __name__ == "__main__":
58+
from doctest import testmod
59+
60+
testmod()

maths/radians.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from math import pi
2+
3+
4+
def radians(degree: float) -> float:
5+
"""
6+
Coverts the given angle from degrees to radians
7+
https://en.wikipedia.org/wiki/Radian
8+
9+
>>> radians(180)
10+
3.141592653589793
11+
>>> radians(92)
12+
1.6057029118347832
13+
>>> radians(274)
14+
4.782202150464463
15+
>>> radians(109.82)
16+
1.9167205845401725
17+
18+
>>> from math import radians as math_radians
19+
>>> all(abs(radians(i)-math_radians(i)) <= 0.00000001 for i in range(-2, 361))
20+
True
21+
"""
22+
23+
return degree / (180 / pi)
24+
25+
26+
if __name__ == "__main__":
27+
from doctest import testmod
28+
29+
testmod()

strings/lower.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def lower(word: str) -> str:
2+
3+
"""
4+
Will convert the entire string to lowecase letters
5+
6+
>>> lower("wow")
7+
'wow'
8+
>>> lower("HellZo")
9+
'hellzo'
10+
>>> lower("WHAT")
11+
'what'
12+
13+
>>> lower("wh[]32")
14+
'wh[]32'
15+
>>> lower("whAT")
16+
'what'
17+
"""
18+
19+
# converting to ascii value int value and checking to see if char is a capital letter
20+
# if it is a capital letter it is getting shift by 32 which makes it a lower case letter
21+
return "".join(
22+
chr(ord(char) + 32) if 65 <= ord(char) <= 90 else char for char in word
23+
)
24+
25+
26+
if __name__ == "__main__":
27+
from doctest import testmod
28+
29+
testmod()

strings/split.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def split(string: str, seperator: str = " ") -> list:
2+
"""
3+
Will split the string up into all the values seperated by the seperator (defaults to spaces)
4+
5+
>>> split("apple#banana#cherry#orange",seperator='#')
6+
['apple', 'banana', 'cherry', 'orange']
7+
8+
>>> split("Hello there")
9+
['Hello', 'there']
10+
11+
>>> split("11/22/63",seperator = '/')
12+
['11', '22', '63']
13+
14+
>>> split("12:43:39",seperator = ":")
15+
['12', '43', '39']
16+
"""
17+
18+
split_words = []
19+
20+
last_index = 0
21+
for index, char in enumerate(string):
22+
if char == seperator:
23+
split_words.append(string[last_index:index])
24+
last_index = index + 1
25+
elif index + 1 == len(string):
26+
split_words.append(string[last_index : index + 1])
27+
return split_words
28+
29+
30+
if __name__ == "__main__":
31+
from doctest import testmod
32+
33+
testmod()

strings/upper.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def upper(word: str) -> str:
2+
"""
3+
Will convert the entire string to uppercase letters
4+
5+
>>> upper("wow")
6+
'WOW'
7+
>>> upper("Hello")
8+
'HELLO'
9+
>>> upper("WHAT")
10+
'WHAT'
11+
12+
>>> upper("wh[]32")
13+
'WH[]32'
14+
"""
15+
16+
# converting to ascii value int value and checking to see if char is a lower letter
17+
# if it is a capital letter it is getting shift by 32 which makes it a capital case letter
18+
return "".join(
19+
chr(ord(char) - 32) if 97 <= ord(char) <= 122 else char for char in word
20+
)
21+
22+
23+
if __name__ == "__main__":
24+
from doctest import testmod
25+
26+
testmod()

0 commit comments

Comments
 (0)