Skip to content

Commit cfa57a6

Browse files
Merge pull request #1 from OmkarPathak/master
Pull from upstream
2 parents dcbb1b8 + 9aa3851 commit cfa57a6

File tree

7 files changed

+279
-85
lines changed

7 files changed

+279
-85
lines changed

docs/Math.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ Features
2424
* Algorithms available:
2525
- LCM (lcm)
2626
- Sieve of Eratostenes (sieve_of_eratosthenes)
27+
- Factorial
28+
- Binary To decimal conversion
29+
- Decimal to binary conversion
30+
- Hex To decimal conversion
31+
- Decimal to hex conversion
2732

2833
* To see all the available functions in a module there is a `modules()` function available. For example,
2934

@@ -70,3 +75,43 @@ Sieve of Eratostenes
7075
.. function:: sieve_of_eratostenes.get_code()
7176

7277
- **Return Value** : returns the code for the ``sieve_of_eratostenes.sieve_of_eratostenes()`` function
78+
79+
Factorial
80+
---------
81+
82+
* Functions and their uses
83+
84+
.. function:: factorial.factorial(number)
85+
86+
- **number** : integer number of which factorial is to be found
87+
- **Return Value** : returns the integer of factorial of the number
88+
89+
.. function:: factorial.get_code()
90+
91+
- **Return Value** : returns the code for the ``factorial.factorial()`` function
92+
93+
94+
Conversion
95+
----------
96+
97+
* Functions and their uses
98+
99+
.. function:: conversion.decimal_to_binary(number)
100+
101+
- **number** : decimal number in string or integer format
102+
- **Return Value** : returns the string of equivalent binary number
103+
104+
.. function:: conversion.binary_to_decimal(number)
105+
106+
- **number** : binary number in string or integer format
107+
- **Return Value** : returns the integer of equivalent decimal number
108+
109+
.. function:: conversion.decimal_to_hex(number)
110+
111+
- **number** : decimal number in string or integer format
112+
- **Return Value** : returns the string of equivalent hex number
113+
114+
.. function:: conversion.hex_to_decimal(number)
115+
116+
- **number** : hex number in string or integer format
117+
- **Return Value** : returns the integer of equivalent decimal number

docs/Pathing.rst

Lines changed: 0 additions & 76 deletions
This file was deleted.

docs/Searching.rst

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ Binary Search
9191

9292
* Functions and their uses
9393

94-
.. function:: binary_search.search(List, key)
94+
.. function:: binary_search.search(_list, target)
9595
:module: pygorithm.searching
9696

97-
- **List** : *Sorted* list in which the key is to be searched
98-
- **key** : key to be searched in the list
99-
- **Return Value** : returns the position (index) of the key if key found, else returns -1
97+
- **_list** : *Sorted* list in which the target is to be searched
98+
- **target** : target to be searched in the list
99+
- **Return Value** : returns the position (index) of the target if target found, else returns False
100100

101101
.. function:: binary_search.time_complexities()
102102

@@ -111,11 +111,11 @@ Linear Search
111111

112112
* Functions and their uses
113113

114-
.. function:: linear_search.search(List, key)
114+
.. function:: linear_search.search(_list, target)
115115

116-
- **List** : the list in which item is to searched
117-
- **key** : key to be searched in the list
118-
- **Return Value** : returns the position (index) of the key if key found, else returns -1
116+
- **_list** : the list in which item is to searched
117+
- **target** : target to be searched in the list
118+
- **Return Value** : returns the position (index) of the target if target found, else returns False
119119

120120
.. function:: linear_search.time_complexities()
121121

@@ -182,3 +182,23 @@ Quick Select Search
182182
.. function:: quick_select.get_code()
183183

184184
- **Return Value** : returns the code for the ``quick_select.search()`` function
185+
186+
Interpolation Search
187+
--------------------
188+
189+
* Functions and their uses
190+
191+
.. function:: interpolation_search.search(_list, target)
192+
:module: pygorithm.searching
193+
194+
- **_list** : *Sorted* list in which the target is to be searched
195+
- **target** : target to be searched in the list
196+
- **Return Value** : returns the position (index) of the target if target found, else returns False
197+
198+
.. function:: interpolation_search.time_complexities()
199+
200+
- **Return Value** : returns time complexities (Best, Average, Worst)
201+
202+
.. function:: interpolation_search.get_code()
203+
204+
- **Return Value** : returns the code for the ``interpolation_search.search()`` function

pygorithm/math/conversion.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""
2+
Author: OMKAR PATHAK
3+
Created On: 23 August 2017
4+
"""
5+
6+
def decimal_to_binary(number):
7+
'''
8+
This function calculates the binary of the given decimal number
9+
:param number: decimal number in string or integer format
10+
:return : string of the equivalent binary number
11+
12+
Algo:
13+
1. Divide the decimal number by 2. Treat the division as an integer division.
14+
2. Write down the remainder (in binary).
15+
3. Divide the result again by 2. Treat the division as an integer division.
16+
4. Repeat step 2 and 3 until result is 0.
17+
5. The binary value is the digit sequence of the remainders from the last to first.
18+
'''
19+
if isinstance(number, str):
20+
number = int(number)
21+
binary = []
22+
while number >= 1:
23+
remainder = number % 2
24+
binary.append(remainder)
25+
number = number // 2
26+
27+
return ''.join(map(str, binary[::-1]))
28+
29+
def binary_to_decimal(number):
30+
'''
31+
This function calculates the decimal of the given binary number
32+
:param number: decimal number in string or integer format
33+
:return : integer of the equivalent decimal number
34+
35+
Algo:
36+
1. Get the last digit of the binary number.
37+
2. Multiply the current digit with (2^power), store the result.
38+
3. Increment power by 1.
39+
4. Repeat from step 2 until all digits have been multiplied.
40+
5. Sum the result of step 2 to get the answer number.
41+
'''
42+
decimal = []
43+
number = list(str(number)[::-1])
44+
for i in range(len(number)):
45+
decimal.append(int(number[i]) * (2 ** i))
46+
47+
return sum(decimal)
48+
49+
def decimal_to_hex(number):
50+
'''
51+
This function calculates the hex of the given decimal number
52+
:param number: decimal number in string or integer format
53+
:return : string of the equivalent hex number
54+
55+
Algo:
56+
1. Divide the decimal number by 16. Treat the division as an integer division.
57+
2. Write down the remainder (in hexadecimal).
58+
3. Divide the result again by 16. Treat the division as an integer division.
59+
4. Repeat step 2 and 3 until result is 0.
60+
5. The hex value is the digit sequence of the remainders from the last to first.
61+
'''
62+
if isinstance(number, str):
63+
number = int(number)
64+
hexadec = []
65+
hex_equivalents = {10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'}
66+
while number >= 1:
67+
remainder = number % 16
68+
if remainder < 10:
69+
hexadec.append(remainder)
70+
elif remainder >= 10:
71+
hexadec.append(hex_equivalents[remainder])
72+
73+
number = number // 16
74+
75+
return ''.join(map(str, hexadec[::-1]))
76+
77+
def hex_to_decimal(number):
78+
'''
79+
This function calculates the decimal of the given hex number
80+
:param number: hex number in string or integer format
81+
:return : integer of the equivalent decimal number
82+
83+
Algo:
84+
1. Get the last digit of the hex number.
85+
2. Multiply the current digit with (16^power), store the result.
86+
3. Increment power by 1.
87+
4. Repeat from step 2 until all digits have been multiplied.
88+
5. Sum the result of step 2 to get the answer number.
89+
'''
90+
decimal = []
91+
decimal_equivalents = {'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15}
92+
number = list(str(number)[::-1])
93+
for i in range(len(number)):
94+
try:
95+
if int(number[i]) < 10:
96+
decimal.append(int(number[i]) * (16 ** i))
97+
except ValueError:
98+
decimal.append(decimal_equivalents[number[i]] * (16 ** i))
99+
100+
return sum(decimal)
101+
102+
print(hex_to_decimal('7DE'))

pygorithm/math/factorial.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Author: OMKAR PATHAK
3+
Created On: 22 August 2017
4+
"""
5+
6+
def factorial(number):
7+
''' This function calculates the factorial of a number '''
8+
if not isinstance(number, int):
9+
raise Exception('Enter an integer number to find the factorial')
10+
if number == 1 or number == 0:
11+
return 1
12+
else:
13+
return number * factorial(number - 1)
14+
15+
def get_code():
16+
"""
17+
returns the code for the factorial function
18+
"""
19+
return inspect.getsource(factorial)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
Author: SHARAD BHAT
3+
Created On: 22nd August 2017
4+
5+
- Best O(1)
6+
- Average O(log(logn))
7+
- Worst O(n)
8+
"""
9+
10+
import inspect
11+
12+
def search(_list, target):
13+
"""
14+
This function performs an interpolation search
15+
on a sorted list and returns the index
16+
of item if successful else returns False
17+
18+
:param _list: list to search
19+
:param target: item to search for
20+
:return: index of item if successful else returns False
21+
"""
22+
23+
if type(_list) is not list:
24+
raise TypeError("interpolation search only accepts lists, not {}".format(str(type(_list))))
25+
26+
# First element
27+
low = 0
28+
# Last element
29+
high = len(_list) - 1
30+
31+
# List is assumed to be sorted
32+
while low <= high and target >= _list[low] and target <= _list[high]:
33+
position = low + int(((float(high - low) / (_list[high] - _list[low])) * (target - _list[low])))
34+
35+
if _list[position] == target:
36+
return position
37+
38+
# If target is greater, search in right half
39+
if _list[position] < target:
40+
low = position + 1
41+
42+
# If target is smaller, search in left half
43+
else:
44+
high = position - 1
45+
46+
return False
47+
48+
49+
50+
def time_complexities():
51+
"""
52+
Return information on functions
53+
time complexity
54+
:return: string
55+
"""
56+
return "Best Case: O(1), Average Case: O(log(logn)), Worst Case: O(logn)"
57+
58+
59+
def get_code():
60+
"""
61+
easily retrieve the source code
62+
of the function
63+
:return: source code
64+
"""
65+
return inspect.getsource(search)

0 commit comments

Comments
 (0)