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 / change / .meta / example.om
| 1 | note Exercism-style exercise "change": https://exercism.org/tracks/python/exercises/change |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note findfewestcoins writes the fewest coins from the denominations coins (each usable any number |
| 4 | note of times) that add up to target (at most 1000) into o, in ascending order, and returns how many; |
| 5 | note or -1 when target cannot be made, -2 when target is negative. |
| 6 | note Check it with: sh exercise.sh exercises/change |
| 7 | om2 |
| 8 | const CMAX ulong 1001 |
| 9 | const CINF ulong 1000000 |
| 10 | global cbest ulong CMAX |
| 11 | global clast ulong CMAX |
| 12 | fn fill coins view ulong * t ulong to void |
| 13 | put cbest 0 0 |
| 14 | for v ulong t |
| 15 | w = add v 1 |
| 16 | put cbest w CINF |
| 17 | for j ulong len coins |
| 18 | c = get coins j |
| 19 | if and gt c 0 le c w |
| 20 | b = add get cbest sub w c 1 |
| 21 | if lt b get cbest w |
| 22 | put cbest w b |
| 23 | put clast w c |
| 24 | end |
| 25 | end |
| 26 | end |
| 27 | end |
| 28 | end |
| 29 | fn findfewestcoins coins view ulong * target long o edit ulong * to long |
| 30 | if lt target 0 |
| 31 | ret -2 |
| 32 | end |
| 33 | t = ulong target |
| 34 | fill coins t |
| 35 | if ge get cbest t CINF |
| 36 | ret -1 |
| 37 | end |
| 38 | k = trace o t |
| 39 | sortasc o k |
| 40 | ret long k |
| 41 | end |
| 42 | fn trace o edit ulong * t ulong to ulong |
| 43 | k = ulong 0 |
| 44 | r = t |
| 45 | while gt r 0 bound 1001 |
| 46 | c = get clast r |
| 47 | put o k c |
| 48 | k = add k 1 |
| 49 | r = sub r c |
| 50 | end |
| 51 | ret k |
| 52 | end |
| 53 | fn sortasc o edit ulong * k ulong to void |
| 54 | for a ulong k |
| 55 | for b ulong sub sub k 1 a |
| 56 | x = get o b |
| 57 | y = get o add b 1 |
| 58 | if gt x y |
| 59 | put o b y |
| 60 | put o add b 1 x |
| 61 | end |
| 62 | end |
| 63 | end |
| 64 | end |