-
-
Notifications
You must be signed in to change notification settings - Fork 47.1k
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.