Skip to content

Commit 06e1b29

Browse files
Ian Doarniandoarn
authored andcommitted
Syntax updates, ~nothing important~
1 parent 6ce882c commit 06e1b29

File tree

6 files changed

+34
-30
lines changed

6 files changed

+34
-30
lines changed

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
@@ -5,5 +5,5 @@
55

66
__all__ = [
77
'fractional_knapsack',
8-
'activity_selection',
8+
'activity_selection'
99
]

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

pygorithm/greedy_algorithm/fractional_knapsack.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
Author: SHARAD BHAT
33
Created On: 22nd August 2017
44
"""
5-
65
import inspect
6+
# TODO: Explain how this works / Explain what a knapsack is
7+
78

8-
def knapsack(W, item_values, item_weights):
9+
def knapsack(w, item_values, item_weights):
910
"""
10-
:param W: maximum weight capacity
11+
:param w: maximum weight capacity
1112
:param item_values: a list of values of items in the knapsack
1213
:param item_weights: a list of weights of items in the knapsack
1314
"""
@@ -38,21 +39,19 @@ def knapsack(W, item_values, item_weights):
3839
item_weights[i], item_weights[maximum] = item_weights[maximum], item_weights[i]
3940

4041
# Placing items in knapsack
41-
remaining_space = W
42+
remaining_space = w
4243
profit = 0
4344
for i in range(0, n):
4445
if remaining_space > item_weights[i]:
4546
profit += item_values[i]
4647
remaining_space -= item_weights[i]
4748
else:
4849
profit += fractional_weights[i] * remaining_space
49-
remaining_space = 0
5050
break
5151

5252
return profit
5353

5454

55-
5655
def get_code():
5756
"""
5857
returns the code for the knapsack function

0 commit comments

Comments
 (0)