Skip to content

Commit 0adcdc7

Browse files
committed
Merge branch 'master' of https://github.com/IanDoarn/pygorithm into IanDoarn-master
'New '
2 parents f5e7915 + b4da00c commit 0adcdc7

19 files changed

+169
-37
lines changed

pygorithm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
'math',
6969
'searching',
7070
'sorting',
71-
'string',
71+
'strings',
7272
'pathfinding'
7373
'geometry',
7474
'greedy_algorithm'

pygorithm/binary/ascii.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
Author: Ian Doarn
99
"""
10+
from pygorithm.binary.binary_utils import pad
1011
from pygorithm.binary.base10 import to_base2 as b10_to_b2
1112
from pygorithm.binary.base2 import to_base16 as b2_to_b16, \
1213
to_ascii as b2_to_ascii
@@ -47,7 +48,8 @@ def to_base2(string, visualize=False, as_string=False):
4748
x, str(ord(x)),
4849
str(b10_to_b2(ord(x)))
4950
))
50-
_list.append(str(b10_to_b2(ord(x))))
51+
value = pad(str(b10_to_b2(ord(x))))
52+
_list.append(value)
5153

5254
if as_string:
5355
return ' '.join(_list)

pygorithm/binary/base10.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def to_base16(n, visualize=False):
6262
print("{} % 16 = {} -> hex = {}".format(
6363
str(n), str(n % 16), HEX_VALUES[n % 16]
6464
))
65-
_list.append(HEX_VALUES[n % 16])
65+
_list.append(HEX_VALUES[n % 16])
6666
n = int(n / 16)
6767

6868
if visualize:

pygorithm/binary/base16.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Author: Ian Doarn
1010
"""
1111
from pygorithm.binary.base2 import to_ascii as b2_to_ascii
12+
from pygorithm.binary.binary_utils import pad
1213
from math import pow
1314

1415
HEX_BINARY_VALUES = {
@@ -45,7 +46,7 @@ def to_base2(h, visualize=False):
4546
print("{} -> {}".format(
4647
value, HEX_BINARY_VALUES[value]
4748
))
48-
_list.append(HEX_BINARY_VALUES[value])
49+
_list.append(pad(HEX_BINARY_VALUES[value]))
4950

5051
return int(''.join(_list))
5152

pygorithm/binary/binary_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Helper methods for binary package
3+
Author: Ian Doarn
4+
"""
5+
6+
7+
def pad(value: str, return_type=str) -> """Pad binary value with zeros""":
8+
if len(value) % 4 != 0:
9+
pad_amount = 4 - (len(value) % 4)
10+
return return_type(('0' * pad_amount) + value)
11+
else:
12+
return return_type(value)
13+
14+
15+
def to_string(binary_array: list, delimiter=' ') -> """Convert binary array to string""":
16+
return delimiter.join(binary_array)

pygorithm/dynamic_programming/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
__all__ = [
88
'binary_knapsack',
9-
'lis',
9+
'lis'
1010
]

pygorithm/dynamic_programming/binary_knapsack.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
'''
1+
"""
22
Author: Omkar Pathak
33
Created At: 25th August 2017
4-
'''
5-
4+
"""
65
import inspect
6+
# TODO: Explain how this works / Explain what a knapsack is
7+
78

8-
def knapsack(W, value, weight):
9-
'''
10-
:param W: maximum weight capacity
11-
:param value: an array of values of items in the knapsack
12-
:param weight: an array of weights of items in the knapsack
13-
'''
9+
def knapsack(w, value, weight):
10+
"""
11+
:param w: maximum weight capacity
12+
:param value: an array of values of items in the knapsack
13+
:param weight: an array of weights of items in the knapsack
14+
"""
1415
if type(value) is not list:
1516
raise TypeError("binary knapsack only accepts lists, not {}".format(str(type(value))))
1617
if type(weight) is not list:
@@ -22,13 +23,13 @@ def knapsack(W, value, weight):
2223
# n = number of items
2324
n = len(value)
2425

25-
knap_sack = [[0 for x in range(W+1)] for x in range(n+1)]
26+
knap_sack = [[0 for _ in range(w+1)] for _ in range(n+1)]
2627

27-
for j in range(W + 1):
28+
for j in range(w + 1):
2829
knap_sack[0][j] = 0
2930

3031
for i in range(n + 1):
31-
for w in range(W + 1):
32+
for w in range(w + 1):
3233
if weight[i - 1] <= w:
3334
knap_sack[i][w] = max(value[i - 1] + knap_sack[i - 1][w - weight[i - 1]], knap_sack[i - 1][w])
3435
else:

pygorithm/dynamic_programming/lis.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1-
'''
1+
"""
22
Author: Omkar Pathak
33
Created At: 25th August 2017
4-
'''
4+
"""
5+
56

67
def longest_increasing_subsequence(_list):
7-
'''
8+
"""
89
The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a
910
given sequence such that all elements of the subsequence are sorted in increasing order. For example,
1011
the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.
11-
'''
12+
:param _list:
13+
:return:
14+
"""
1215
# Initialize list with some value
1316
lis = [1] * len(_list)
1417
# list for storing the elements in an lis
1518
elements = [0] * len(_list)
1619

1720
# Compute optimized LIS values in bottom up manner
18-
for i in range (1 , len(_list)):
19-
for j in range(0 , i):
20-
if _list[i] > _list[j] and lis[i]< lis[j] + 1:
21+
for i in range(1, len(_list)):
22+
for j in range(0, i):
23+
if _list[i] > _list[j] and lis[i] < lis[j] + 1:
2124
lis[i] = lis[j]+1
2225
elements[i] = j
2326

24-
idx = 0
25-
2627
# find the maximum of the whole list and get its index in idx
2728
maximum = max(lis)
2829
idx = lis.index(maximum)
@@ -35,6 +36,7 @@ def longest_increasing_subsequence(_list):
3536

3637
return (maximum, seq[::-1])
3738

39+
3840
def get_code():
3941
"""
4042
returns the code for the longest_increasing_subsequence function

pygorithm/greedy_algorithm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
__all__ = [
88
'fractional_knapsack',
9-
'activity_selection',
9+
'activity_selection'
1010
]

pygorithm/greedy_algorithm/activity_selection.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Author: OMKAR PATHAK
33
Created On: 26th August 2017
44
"""
5-
65
import inspect
6+
# TODO: Explain what this is / how it works
7+
78

89
def activity_selection(start_times, finish_times):
910
"""
@@ -37,6 +38,7 @@ def activity_selection(start_times, finish_times):
3738

3839
return activity
3940

41+
4042
def get_code():
4143
"""
4244
returns the code for the activity_selection function

0 commit comments

Comments
 (0)