I’ve recently dug up old code from my University days, which I thought I’d share for the benefit/misfortune of others. There’s a common problem in programming called the knapsack problem. Here was my solution based on the dynamic algorithm. # Math is used to floor/round the floats to an interger and back! import math # Total allowed…

# Category: code

## Knapsack Problem (0-1 solution) – Greedy Algorithm

I’ve recently dug up old code from my University days, which I thought I’d share for the benefit/misfortune of others. There’s a common problem in programming called the knapsack problem. Here was my solution based on the greedy algorithm. # Name, Weight and Value of Items items = [(“Weapon and Ammunition”, 4.13, 1.4), (“Water”, 2.13,…