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 / equilibrium-index.wiki

136 lines · 2440 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 sp " "
8text minus "-"
9text d1 "-7 1 5 2 -4 3 0"
10text d2 "2 4 6"
11text d3 "2 9 2"
12text d4 "1 -1 1 -1 1 -1 1"
13text tidx "equilibrium indices:"
14global vals long 16
15note emit writes every byte of a to stdout; a short write is retried, an error exits 1
16fn emit a view uchar * to void
17o = ulong 0
18while lt o len a bound 4096
19k = write 1 a o sub len a o
20if le k 0
21exit 1
22end
23o = add o ulong k
24end
25end
26note emitu writes n in decimal; 20 digits hold every ulong
27fn emitu n ulong to void
28d = new uchar 20 0
29i = ulong 20
30v = n
31while true bound 20
32i = sub i 1
33put d i uchar add rem v 10 48
34v = div v 10
35if eq v 0
36break
37end
38end
39emit part d i sub 20 i
40end
41note emiti writes a signed n; the magnitude is taken as a ulong so the minimum long cannot overflow
42fn emiti n long to void
43if lt n 0
44emit minus
45emitu add bitcast ulong not n 1
46else
47emitu ulong n
48end
49end
50note parse reads the decimal numbers in s (separated by spaces, an optional - sign) into vals and
51note returns how many; one pass over s, so the loop is bounded by len s
52fn parse s view uchar * to ulong
53n = ulong 0
54v = long 0
55sg = long 1
56dig = false
57for i ulong len s
58c = get s i
59if eq c 45
60sg = long -1
61else
62if and ge c 48 le c 57
63v = add mul v 10 long sub c 48
64dig = true
65else
66if dig
67put vals n mul sg v
68n = add n 1
69end
70v = long 0
71sg = long 1
72dig = false
73end
74end
75end
76if dig
77put vals n mul sg v
78n = add n 1
79end
80ret n
81end
82note show writes vals[0..n-1] separated by spaces, then a newline
83fn show n ulong to void
84for i ulong n
85if gt i 0
86emit sp
87end
88emiti get vals i
89end
90emit nl
91end
92note eqidx keeps the sum to the left and the sum to the right of i in one pass after the total
93fn eqidx n ulong to void
94tot = long 0
95for i ulong n
96tot = add tot get vals i
97end
98lsum = long 0
99emit tidx
100for i ulong n
101rsum = sub sub tot lsum get vals i
102if eq lsum rsum
103emit sp
104emitu i
105end
106lsum = add lsum get vals i
107end
108emit nl
109end
110fn main to int
111n = parse d1
112show n
113eqidx n
114n = parse d2
115show n
116eqidx n
117n = parse d3
118show n
119eqidx n
120n = parse d4
121show n
122eqidx n
123ret 0
124end
125</syntaxhighlight>
126{{out}}
127<pre>
128-7 1 5 2 -4 3 0
129equilibrium indices: 3 6
1302 4 6
131equilibrium indices:
1322 9 2
133equilibrium indices: 1
1341 -1 1 -1 1 -1 1
135equilibrium indices: 0 1 2 3 4 5 6
136</pre>