Skip to content

Commit

Permalink
added algorithm knapsack/python
Browse files Browse the repository at this point in the history
  • Loading branch information
Sartech-B committed Oct 6, 2018
1 parent e72f3e1 commit 5f84fd2
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions knapsack/Python/knapsack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def knapsack(v, w, n, W):
'''
Function solves 0/1 Knapsack problem
Function to calculate frequency of items to be added to knapsack to maximise value
such that total weight is less than or equal to W
:param v: set of values of n objects, first element is useless because relevant values are assumed to begin from index 1
:param w: set of weights of n objects, first element is useless because relevant weights are assumed to begin from index 1
:param n: number of items
:pram W: maximum weight allowd by knapsack
'''
m = [[0 for j in range(W+1)] for i in range(n+1)]
for j in range(W+1):
m[0][j] = 0

for i in range(1, n+1):
for j in range(W+1):
if w[i]>j:
m[i][j] = m[i-1][j]
else:
m[i][j] = max(m[i-1][j], m[i-1][j-w[i]]+v[i])
return m[n][W]

def main():
v = [0, 1, 2, 3, 4, 5]
w = [0, 5, 6, 2, 3, 4]
n = 5
W = 8
print("Answer to 0/1 Knapsack problem(Maximum value possible in given weight limit): "+str(knapsack(v, w, n, W)))

if __name__ == '__main__':
main()

2 comments on commit 5f84fd2

@sangamcse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on 5f84fd2.

There are 25 results for the section all.pyjava. They have been shortened and will not be shown inline because they are more than 10.

Message File Line
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 2
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 3
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 4
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 5
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 6
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 7
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 8
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 9
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 10
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 11
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 12
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 13
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 15
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 16
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 17
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 18
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 19
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 20
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 21
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 24
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 25
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 26
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 27
Line contains following spacing inconsistencies: - Tabs used instead of spaces. knapsack/Python/knapsack.py 28
Line contains following spacing inconsistencies: - No newline at EOF. knapsack/Python/knapsack.py 31

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

@sangamcse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on 5f84fd2.

There are 35 results for the section all.autopep8. They have been shortened and will not be shown inline because they are more than 10.

Message File Line
The code does not comply to PEP8. knapsack/Python/knapsack.py 2
The code does not comply to PEP8. knapsack/Python/knapsack.py 24
The code does not comply to PEP8. knapsack/Python/knapsack.py 31
W191 indentation contains tabs knapsack/Python/knapsack.py 3
W191 indentation contains tabs knapsack/Python/knapsack.py 4
E501 line too long (83 > 80 characters) knapsack/Python/knapsack.py 4
W191 indentation contains tabs knapsack/Python/knapsack.py 5
W191 indentation contains tabs knapsack/Python/knapsack.py 6
E501 line too long (121 > 80 characters) knapsack/Python/knapsack.py 6
W191 indentation contains tabs knapsack/Python/knapsack.py 7
E501 line too long (123 > 80 characters) knapsack/Python/knapsack.py 7
W191 indentation contains tabs knapsack/Python/knapsack.py 8
W191 indentation contains tabs knapsack/Python/knapsack.py 9
W191 indentation contains tabs knapsack/Python/knapsack.py 10
W191 indentation contains tabs knapsack/Python/knapsack.py 11
W191 indentation contains tabs knapsack/Python/knapsack.py 12
W191 indentation contains tabs knapsack/Python/knapsack.py 13
W191 indentation contains tabs knapsack/Python/knapsack.py 15
W191 indentation contains tabs knapsack/Python/knapsack.py 16
W191 indentation contains tabs knapsack/Python/knapsack.py 17
E225 missing whitespace around operator knapsack/Python/knapsack.py 17
W191 indentation contains tabs knapsack/Python/knapsack.py 18
W191 indentation contains tabs knapsack/Python/knapsack.py 19
W191 indentation contains tabs knapsack/Python/knapsack.py 20
W191 indentation contains tabs knapsack/Python/knapsack.py 21
E302 expected 2 blank lines, found 1 knapsack/Python/knapsack.py 23
W191 indentation contains tabs knapsack/Python/knapsack.py 24
W191 indentation contains tabs knapsack/Python/knapsack.py 25
W191 indentation contains tabs knapsack/Python/knapsack.py 26
W191 indentation contains tabs knapsack/Python/knapsack.py 27
W191 indentation contains tabs knapsack/Python/knapsack.py 28
E501 line too long (114 > 80 characters) knapsack/Python/knapsack.py 28
E305 expected 2 blank lines after class or function definition, found 1 knapsack/Python/knapsack.py 30
E101 indentation contains mixed spaces and tabs knapsack/Python/knapsack.py 31
W292 no newline at end of file knapsack/Python/knapsack.py 31

Until GitMate provides an online UI to show a better overview, you can run coala locally for more details.

Please sign in to comment.