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 1 commit
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
18 changes: 18 additions & 0 deletions maths/combinations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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()

32 changes: 32 additions & 0 deletions maths/gamma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from math import factorial

def gamma(num):
'''
>>> gamma(1)
1

>>> gamma(0)
Traceback (most recent call last):
...
ValueError: math domain error

>>> gamma(10)
362880

>>> gamma(-199)
Traceback (most recent call last):
...
ValueError: math domain error
'''


if num < 1:
raise ValueError(" math domain error")
else:
return factorial(num-1)


if __name__ == "__main__":
from doctest import testmod
testmod()

24 changes: 24 additions & 0 deletions maths/radians.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from math import pi
'''

Coverts the given angle from degrees to radians
https://en.wikipedia.org/wiki/Radian
'''
def radians(degree):

"""
>>> radians(180)
3.141592653589793
>>> radians(92)
1.6057029118347832
>>> radians(274)
4.782202150464463
"""

return degree / (180/pi)


if __name__ == "__main__":
from doctest import testmod
testmod()

37 changes: 37 additions & 0 deletions strings/lower.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


def lower(word):

'''
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'
'''
lower_string = ""

for char in word:
# 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
if ord(char) >= 65 and ord(char) <= 90:
lower_string += chr(ord(char) + 32)
else:
lower_string += char

return lower_string



if __name__ == "__main__":
from doctest import testmod
testmod()

32 changes: 32 additions & 0 deletions strings/split.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

def split(string,seperator = " "):
'''
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()
32 changes: 32 additions & 0 deletions strings/upper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def upper(word):
'''
Will convert the entire string to uppercase letters

>>> upper("wow")
'WOW'
>>> upper("Hello")
'HELLO'
>>> upper("WHAT")
'WHAT'

>>> upper("wh[]32")
'WH[]32'
'''
upper_string = ""

for char in word:
# 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
if ord(char) >= 97 and ord(char) <= 122:
upper_string += chr(ord(char) -32)
else:
upper_string += char

return upper_string



if __name__ == "__main__":
from doctest import testmod
testmod()