interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / tasks / greatest-subsequential-sum / greatest-subsequential-sum.om

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