interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / determine-if-a-string-is-collapsible.wiki

88 lines · 2369 bytes Raw Blame History
1=={{header|Omegese}}==
2{{works with|omc|sha256 2c824805, aien-dev/omega-mv e5b32567, Linux AArch64}}
3Omega has no imports, so the small output helpers (<code>emit</code> and friends) are part of the program.
4<syntaxhighlight lang="text">
5om2
6text nl "\n"
7text lq "<<<"
8text rq ">>>"
9text tlen " length "
10text c0 "\"If I were two-faced, would I be wearing this one?\" --- Abraham Lincoln "
11text c1 "..1111111111111111111111111111111111111111111111111111111111111117777888"
12text c2 "I never give 'em hell, I just tell the truth, and they think it's hell. "
13text c3 " --- Harry S Truman "
14note emit writes every byte of a to stdout; a short write is retried, an error exits 1
15fn emit a view uchar * to void
16o = ulong 0
17while lt o len a bound 4096
18k = write 1 a o sub len a o
19if le k 0
20exit 1
21end
22o = add o ulong k
23end
24end
25note emitu writes n in decimal; 20 digits hold every ulong
26fn emitu n ulong to void
27d = new uchar 20 0
28i = ulong 20
29v = n
30while true bound 20
31i = sub i 1
32put d i uchar add rem v 10 48
33v = div v 10
34if eq v 0
35break
36end
37end
38emit part d i sub 20 i
39end
40fn line a view uchar * to void
41emit lq
42emit a
43emit rq
44emit tlen
45emitu len a
46emit nl
47end
48note collapse keeps a byte only when it differs from the byte before it
49fn collapse a view uchar * to void
50o = new uchar 256 0
51n = ulong 0
52for i ulong len a
53c = get a i
54k = true
55if gt i 0
56k = ne c get a sub i 1
57end
58if k
59put o n c
60n = add n 1
61end
62end
63line a
64line part o 0 n
65end
66fn main to int
67note the empty string, as a slice of length 0
68collapse part c0 0 0
69collapse c0
70collapse c1
71collapse c2
72collapse c3
73ret 0
74end
75</syntaxhighlight>
76{{out}}
77<pre>
78&lt;&lt;&lt;>>> length 0
79&lt;&lt;&lt;>>> length 0
80&lt;&lt;&lt;"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> length 72
81&lt;&lt;&lt;"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> length 70
82&lt;&lt;&lt;..1111111111111111111111111111111111111111111111111111111111111117777888>>> length 72
83&lt;&lt;&lt;.178>>> length 4
84&lt;&lt;&lt;I never give 'em hell, I just tell the truth, and they think it's hell. >>> length 72
85&lt;&lt;&lt;I never give 'em hel, I just tel the truth, and they think it's hel. >>> length 69
86&lt;&lt;&lt; --- Harry S Truman >>> length 72
87&lt;&lt;&lt; - Hary S Truman >>> length 17
88</pre>