interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / knapsack-problem-unbounded / oracle.py

16 lines · 868 bytes Raw Blame History
1# Independent reference for "Knapsack problem/Unbounded" (Python 3, standard library only).
2# expected.txt is this program's stdout; omc never produced it.
3from fractions import Fraction as F
4from itertools import product
5items = [("panacea", 3000, F("0.3"), F("0.025")), ("ichor", 1800, F("0.2"), F("0.015")), ("gold", 2500, F("2.0"), F("0.002"))]
6W, V = F(25), F("0.25")
7fits = []
8for n in product(*(range(int(min(W / w, V / v)) + 1) for _, _, w, v in items)):
9 w = sum(k * it[2] for k, it in zip(n, items))
10 v = sum(k * it[3] for k, it in zip(n, items))
11 if w <= W and v <= V:
12 fits.append((sum(k * it[1] for k, it in zip(n, items)), n, w, v))
13best = max(f[0] for f in fits)
14print("panacea ichor gold value weight volume")
15for val, n, w, v in sorted(f for f in fits if f[0] == best):
16 print(*n, val, f"{int(w * 10)}/10", f"{int(v * 1000)}/1000")