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