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