## DEV Community is a community of 669,334 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Day 59 Of 100DaysOfCode : More About Algorithm Durga Pokharel
A mathematics student learning to code.

This is my 59th day of #100daysofcode and #python learning like yesterday today also learned more properties of SQL (subquery inside where, subquery inside from) from datacamp. Also tried to slove assignment in coursera.

Here is a code for fractional knapsack

# python code

``````# from https://www.tutorialspoint.com/program-to-implement-the-fractional-knapsack-problem-in-python
class FractionalKnapsack:
def solve(self, weights, values, capacity):
res = 0
for pair in sorted(zip(weights, values), key=lambda x: - x/x):
if not bool(capacity):
break
if pair > capacity:
res += int(pair / (pair / capacity))
capacity = 0
elif pair <= capacity:
res += pair
capacity -= pair
return int(res)

ob = FractionalKnapsack()
weights = [20, 50, 30]
values = [60, 100, 120]
capacity = 50
print(ob.solve(weights, values, capacity))
``````

Possible optimal solution will be,

``````180
``````