Assignment
Assignment
CODE:
class Item:
def _init_(self, weight, value):
self.weight = weight
self.value = value
self.cost = value / weight
def main():
n = int(input("Enter the number of items: "))
items = []
for _ in range(n):
weight, value = map(int, input("Enter weight and value of
item {}: ".format(_+1)).split())
items.append(Item(weight, value))
capacity = int(input("Enter the capacity of knapsack: "))
if _name_ == "_main_":
main()
OUTPUT:
𝐒𝐭𝐚𝐧𝐝𝐚𝐫𝐝 𝐢𝐧𝐩𝐮𝐭
Enter the number of items: 3
Enter weight and value of item 1: 10 60
Enter weight and value of item 2: 20 100
Enter weight and value of item 3: 30 120
Enter the capacity of knapsack: 50
𝐒𝐭𝐚𝐧𝐝𝐚𝐫𝐝 𝐨𝐮𝐭𝐩𝐮𝐭
Items in knapsack and their fractions:
Weight: 10.0, Value: 60.0
Weight: 20.0, Value: 100.0
Weight: 20.0, Value: 60.0
Total value in knapsack: 220.0