interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / si / rosetta / exercises / run-length-encoding / .meta / example.om

71 lines · 1378 bytes Raw Blame History
1note Exercism-style exercise "run-length-encoding": https://exercism.org/tracks/python/exercises/run-length-encoding
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3note encode writes the run-length encoding of s into o: each run of 2 or more equal bytes becomes its
4note decimal count followed by the byte, a single byte stays as it is. decode reverses it. Both
5note return the number of bytes written.
6note Check it with: sh exercise.sh exercises/run-length-encoding
7om2
8fn app o edit uchar * p ulong s view uchar * to ulong
9q = p
10for i ulong len s
11put o q get s i
12q = add q 1
13end
14ret q
15end
16fn appnum o edit uchar * p ulong n ulong to ulong
17d = new uchar 20 0
18i = ulong 20
19v = n
20while true bound 20
21i = sub i 1
22put d i uchar add rem v 10 48
23v = div v 10
24if eq v 0
25break
26end
27end
28ret app o p part d i sub 20 i
29end
30fn encode s view uchar * o edit uchar * to ulong
31p = ulong 0
32i = ulong 0
33n = len s
34while lt i n bound 999999
35c = get s i
36r = ulong 1
37while lt add i r n bound 999999
38if ne get s add i r c
39break
40end
41r = add r 1
42end
43if gt r 1
44p = appnum o p r
45end
46put o p c
47p = add p 1
48i = add i r
49end
50ret p
51end
52fn decode s view uchar * o edit uchar * to ulong
53p = ulong 0
54r = ulong 0
55for i ulong len s
56c = get s i
57if and ge c 48 le c 57
58r = add mul r 10 ulong sub c 48
59else
60if eq r 0
61r = ulong 1
62end
63for j ulong r
64put o p c
65p = add p 1
66end
67r = ulong 0
68end
69end
70ret p
71end