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
omega-mv / si / rosetta / tasks / greatest-subsequential-sum / greatest-subsequential-sum.om
| 1 | note Rosetta Code task "Greatest subsequential sum": https://rosettacode.org/wiki/Greatest_subsequential_sum |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | om2 |
| 4 | text nl "\n" |
| 5 | text sp " " |
| 6 | text minus "-" |
| 7 | text d0 "-1 -2 3 5 6 -2 -1 4 -4 2 -1" |
| 8 | text d1 "-1 -2 -3 -4" |
| 9 | text d2 "4 -1 2 1 -5 4 0 3" |
| 10 | text d3 "2 -3 2 -3 2" |
| 11 | text tsum "sum: " |
| 12 | text tsub "subsequence:" |
| 13 | global vals long 16 |
| 14 | global pre long 17 |
| 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 gss tries every start i and end j using prefix sums; the empty subsequence (sum 0) is allowed, |
| 93 | note and on a tie the earliest start wins, then the shortest run, so the answer is unique |
| 94 | fn gss n ulong to void |
| 95 | put pre 0 0 |
| 96 | for i ulong n |
| 97 | put pre add i 1 add get pre i get vals i |
| 98 | end |
| 99 | bs = long 0 |
| 100 | bi = ulong 0 |
| 101 | bj = ulong 0 |
| 102 | for i ulong n |
| 103 | for x ulong sub n i |
| 104 | j = add add i x 1 |
| 105 | s = sub get pre j get pre i |
| 106 | if gt s bs |
| 107 | bs = s |
| 108 | bi = i |
| 109 | bj = j |
| 110 | end |
| 111 | end |
| 112 | end |
| 113 | emit tsum |
| 114 | emiti bs |
| 115 | emit nl |
| 116 | emit tsub |
| 117 | for k ulong sub bj bi |
| 118 | emit sp |
| 119 | emiti get vals add bi k |
| 120 | end |
| 121 | emit nl |
| 122 | end |
| 123 | fn main to int |
| 124 | n = parse d0 |
| 125 | show n |
| 126 | gss n |
| 127 | n = parse d1 |
| 128 | show n |
| 129 | gss n |
| 130 | n = parse d2 |
| 131 | show n |
| 132 | gss n |
| 133 | n = parse d3 |
| 134 | show n |
| 135 | gss n |
| 136 | ret 0 |
| 137 | end |