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 / greatest-subsequential-sum.wiki

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