omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / si / rosetta / exercises / knapsack / .meta / example.om
| 1 | note Exercism-style exercise "knapsack": https://exercism.org/tracks/python/exercises/knapsack |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note Item i weighs ws[i] and is worth vs[i]. maximumvalue is the largest total worth of items (each |
| 4 | note taken at most once) whose total weight is at most cap (cap at most 1000). |
| 5 | note Check it with: sh exercise.sh exercises/knapsack |
| 6 | om2 |
| 7 | const KMAX ulong 1001 |
| 8 | global kdp ulong KMAX |
| 9 | fn additem w ulong v ulong cap ulong to void |
| 10 | for k ulong add sub cap w 1 |
| 11 | d = sub cap k |
| 12 | x = add get kdp sub d w v |
| 13 | if gt x get kdp d |
| 14 | put kdp d x |
| 15 | end |
| 16 | end |
| 17 | end |
| 18 | fn maximumvalue ws view ulong * vs view ulong * cap ulong to ulong |
| 19 | for c ulong add cap 1 |
| 20 | put kdp c 0 |
| 21 | end |
| 22 | for i ulong len ws |
| 23 | w = get ws i |
| 24 | if le w cap |
| 25 | additem w get vs i cap |
| 26 | end |
| 27 | end |
| 28 | ret get kdp cap |
| 29 | end |