interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / exercises / change / .meta / example.om

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