interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / research / om-bench / bench / families_public.py

382 lines · 19284 bytes Raw Blame History
1"""Public families: split train (8 families) and dev (4 families). 4 variants each.
2
3Every oracle below is an independent reference implementation written by OmegaEval.
4"""
5from benchlib import mk, line, lines, ints, out, numlist, text_lines, words_line, M64
6
7TRAIN = ["sumlist", "countb", "extrema", "gcdlcm", "reverse", "multiples", "caesar", "digits"]
8DEV = ["primes", "fibo", "wordst", "brackets"]
9
10
11def _sumlist():
12 F = "sumlist"
13 g = lambda r: numlist(r, 1, 200, (1 << 64) - 1)
14 small = lambda r: numlist(r, 1, 200, 1 << 31)
15 return [
16 mk(F, "v1", "Read one line of numbers. Print their sum modulo 2^64.",
17 lambda i: out(sum(ints(i)) % M64), ["1 2 3\n", "18446744073709551615 2\n"], g,
18 ["0\n", "18446744073709551615 18446744073709551615 18446744073709551615\n"]),
19 mk(F, "v2", "Read one line of numbers, each below 2^32. Print the sum of their squares modulo 2^64.",
20 lambda i: out(sum(x * x for x in ints(i)) % M64), ["1 2 3\n", "4294967295\n"], small,
21 ["0\n", "4294967295 4294967295 4294967295\n"]),
22 mk(F, "v3", "Read one line of numbers. Print the sum of the even numbers modulo 2^64 (print 0 if there are none).",
23 lambda i: out(sum(x for x in ints(i) if x % 2 == 0) % M64), ["1 2 3 4\n", "7\n"], g,
24 ["1 3 5\n", "18446744073709551614 18446744073709551614\n"]),
25 mk(F, "v4", "Read one line of numbers. Print the sum, modulo 2^64, of the numbers at even positions (the 1st, 3rd, 5th and so on).",
26 lambda i: out(sum(ints(i)[0::2]) % M64), ["5 100 7\n", "9\n"], g, ["0 1\n"]),
27 ]
28
29
30def _countb():
31 F = "countb"
32 alpha = "abcxyz AB0123456789\n"
33 g = lambda r: text_lines(r, r.randint(1, 30), alpha.replace("\n", ""), 60)
34 return [
35 mk(F, "v1", "Count the bytes equal to lowercase letter a (byte 97) in the whole input. Print the count.",
36 lambda i: out(i.count(b"a")), ["banana\n", "xyz\n"], g, ["\n", "a\n" * 1000]),
37 mk(F, "v2", "Count the newline bytes (byte 10) in the input. Print the count.",
38 lambda i: out(i.count(b"\n")), ["one\ntwo\n", "x\n"], g, ["\n" * 5000]),
39 mk(F, "v3", "Count the decimal digit bytes 0 to 9 (bytes 48 to 57) in the input. Print the count.",
40 lambda i: out(sum(1 for c in i if 48 <= c <= 57)), ["a1b22\n", "none\n"], g, ["\n", "9" * 65000 + "\n"]),
41 mk(F, "v4", "Count the space bytes (byte 32) in the input. Print the count.",
42 lambda i: out(i.count(b" ")), ["a b c\n", "abc\n"], g, ["\n", " " * 3000 + "\n"]),
43 ]
44
45
46def _extrema():
47 F = "extrema"
48 g = lambda r: numlist(r, 1, 300, (1 << 64) - 1)
49 def argmax(xs):
50 m = max(xs)
51 return xs.index(m)
52 return [
53 mk(F, "v1", "Read one line of at least one number. Print the largest.",
54 lambda i: out(max(ints(i))), ["3 9 2\n", "5\n"], g, ["0\n", "18446744073709551615 0\n"]),
55 mk(F, "v2", "Read one line of at least one number. Print the smallest.",
56 lambda i: out(min(ints(i))), ["3 9 2\n", "5\n"], g, ["18446744073709551615\n", "7 0 7\n"]),
57 mk(F, "v3", "Read one line of at least one number. Print the largest minus the smallest.",
58 lambda i: out(max(ints(i)) - min(ints(i))), ["3 9 2\n", "5\n"], g, ["0 18446744073709551615\n"]),
59 mk(F, "v4", "Read one line of at least one number. Print the position (counting from 0) of the first occurrence of the largest number.",
60 lambda i: out(argmax(ints(i))), ["3 9 2 9\n", "5\n"], g, ["1 1 1\n", "0 0 18446744073709551615\n"]),
61 ]
62
63
64def _gcdlcm():
65 from math import gcd
66 F = "gcdlcm"
67 g2 = lambda r: line(r.randint(1, (1 << 64) - 1), r.randint(1, (1 << 64) - 1))
68 def g2c(r):
69 k = r.randint(1, 1 << 20)
70 return line(k * r.randint(1, 1 << 20), k * r.randint(1, 1 << 20))
71 def glist(r):
72 k = r.randint(1, 1000)
73 return line(*[k * r.randint(1, 1 << 40) for _ in range(r.randint(1, 50))])
74 def glcm(r):
75 return line(r.randint(1, 1 << 31), r.randint(1, 1 << 31))
76 def gl(i):
77 xs = ints(i)
78 r = 0
79 for x in xs:
80 r = gcd(r, x)
81 return out(r)
82 return [
83 mk(F, "v1", "Read two positive numbers on one line. Print their greatest common divisor.",
84 lambda i: out(gcd(*ints(i))), ["12 18\n", "7 13\n"], g2c, ["1 1\n", "18446744073709551615 18446744073709551615\n"] + [None] * 0),
85 mk(F, "v2", "Read one line of at least one positive number. Print the greatest common divisor of all of them.",
86 gl, ["12 18 30\n", "7\n"], glist, ["18446744073709551615\n"]),
87 mk(F, "v3", "Read two positive numbers below 2^32 on one line. Print their least common multiple.",
88 lambda i: out(ints(i)[0] * ints(i)[1] // gcd(*ints(i))), ["4 6\n", "7 13\n"], glcm,
89 ["4294967295 4294967294\n", "1 1\n"]),
90 mk(F, "v4", "Read two positive numbers on one line. Print yes if their greatest common divisor is 1, otherwise print no.",
91 lambda i: out("yes" if gcd(*ints(i)) == 1 else "no"), ["4 9\n", "6 9\n"], g2,
92 ["1 1\n", "18446744073709551615 18446744073709551614\n", "2 18446744073709551614\n"]),
93 ]
94
95
96def _reverse():
97 F = "reverse"
98 alpha = "abcdefghij XYZ"
99 g = lambda r: text_lines(r, 1, alpha, 200, 1)
100 gw = lambda r: words_line(r, 1, 40)
101 gm = lambda r: text_lines(r, r.randint(1, 20), "abcdef", 30)
102 gn = lambda r: line(r.randint(0, (1 << 64) - 1))
103 def rev_lines(i):
104 rows = i.split(b"\n")[:-1]
105 return b"".join(x[::-1] + b"\n" for x in rows), 0
106 return [
107 mk(F, "v1", "Read one line of text. Print the line's bytes in reverse order, followed by a newline.",
108 lambda i: (i[:-1][::-1] + b"\n", 0), ["abc\n", "a b\n"], g, ["x\n", "\n"]),
109 mk(F, "v2", "Read one line of words separated by single spaces. Print the words in reverse order, separated by single spaces.",
110 lambda i: (b" ".join(i[:-1].split(b" ")[::-1]) + b"\n", 0), ["one two three\n", "solo\n"], gw),
111 mk(F, "v3", "Read lines of text. Print every line with its bytes reversed, keeping the order of the lines.",
112 rev_lines, ["abc\nde\n", "\n"], gm),
113 mk(F, "v4", "Read one number. Print its decimal digits in reverse order with leading zeros removed (print 0 for 0).",
114 lambda i: out(int(str(ints(i)[0])[::-1])), ["1200\n", "345\n"], gn, ["0\n", "18446744073709551615\n", "10\n"]),
115 ]
116
117
118def _multiples():
119 F = "multiples"
120 gn = lambda r: line(r.randint(1, 2000))
121 gb = lambda r: line(r.randint(1, 10 ** 9))
122 def fizz(i):
123 n = ints(i)[0]
124 rows = []
125 for k in range(1, n + 1):
126 rows.append("FizzBuzz" if k % 15 == 0 else "Fizz" if k % 3 == 0 else "Buzz" if k % 5 == 0 else str(k))
127 return ("\n".join(rows) + "\n").encode(), 0
128 return [
129 mk(F, "v1", "Read one number n between 1 and 2000. For k from 1 to n print one line: FizzBuzz if k is divisible by 15, otherwise Fizz if divisible by 3, otherwise Buzz if divisible by 5, otherwise k.",
130 fizz, ["5\n", "15\n"], gn, ["1\n", "2000\n"]),
131 mk(F, "v2", "Read one number n below 10^9. Print how many numbers k with 1 <= k < n are divisible by 3 or by 5.",
132 lambda i: out(sum(1 for k in range(1, ints(i)[0]) if k % 3 == 0 or k % 5 == 0) if ints(i)[0] < 100000 else ((ints(i)[0] - 1) // 3 + (ints(i)[0] - 1) // 5 - (ints(i)[0] - 1) // 15)),
133 ["10\n", "1\n"], gb, ["0\n", "999999999\n"]),
134 mk(F, "v3", "Read one number n below 10^9. Print the sum of all numbers k with 1 <= k < n that are divisible by 3 or by 5.",
135 lambda i: out(sum(k for k in range(1, ints(i)[0]) if k % 3 == 0 or k % 5 == 0)) if ints(i)[0] < 100000 else out(_s35(ints(i)[0])),
136 ["10\n", "1\n"], gb, ["0\n", "999999999\n"]),
137 mk(F, "v4", "Read one number n between 1 and 2000. Print n lines: line k (from 1) holds k*k.",
138 lambda i: (b"".join(line(k * k) for k in range(1, ints(i)[0] + 1)), 0), ["3\n", "1\n"], gn, ["2000\n"]),
139 ]
140
141
142def _s35(n):
143 def s(d):
144 m = (n - 1) // d
145 return d * m * (m + 1) // 2
146 return s(3) + s(5) - s(15)
147
148
149def _caesar():
150 F = "caesar"
151 alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.,"
152 g = lambda r: text_lines(r, r.randint(1, 10), alpha, 80)
153 def shift(k):
154 def f(i):
155 o = bytearray()
156 for c in i:
157 if 97 <= c <= 122:
158 o.append((c - 97 + k) % 26 + 97)
159 elif 65 <= c <= 90:
160 o.append((c - 65 + k) % 26 + 65)
161 else:
162 o.append(c)
163 return bytes(o), 0
164 return f
165 tasks = []
166 for vid, k in (("v1", 1), ("v2", 3), ("v3", 13), ("v4", 25)):
167 tasks.append(mk(F, vid, f"Copy the input to the output, replacing every ASCII letter by the letter {k} places later in the alphabet, wrapping from z to a and from Z to A and keeping its case. All other bytes, including newlines, are copied unchanged.",
168 shift(k), ["Hello, World\n", "xyz XYZ\n"], g, ["\n", "zZ\n"]))
169 return tasks
170
171
172def _digits():
173 F = "digits"
174 g = lambda r: line(r.randint(0, (1 << 64) - 1))
175 def droot(n):
176 while n >= 10:
177 n = sum(int(c) for c in str(n))
178 return n
179 def pnz(n):
180 p = 1
181 for c in str(n):
182 if c != "0":
183 p *= int(c)
184 return p
185 return [
186 mk(F, "v1", "Read one number. Print the sum of its decimal digits.",
187 lambda i: out(sum(int(c) for c in str(ints(i)[0]))), ["123\n", "0\n"], g, ["18446744073709551615\n", "9999999999999999999\n"]),
188 mk(F, "v2", "Read one number. Repeatedly replace it by the sum of its decimal digits until it is below 10. Print the result.",
189 lambda i: out(droot(ints(i)[0])), ["38\n", "0\n"], g, ["18446744073709551615\n", "9\n", "10\n"]),
190 mk(F, "v3", "Read one number. Print how many decimal digits it has (0 has one digit).",
191 lambda i: out(len(str(ints(i)[0]))), ["123\n", "0\n"], g, ["18446744073709551615\n", "10000000000000000000\n", "9\n"]),
192 mk(F, "v4", "Read one number. Print the product of its nonzero decimal digits (print 1 if it has none).",
193 lambda i: out(pnz(ints(i)[0])), ["1203\n", "0\n"], g, ["18446744073709551615\n", "9999999999999999999\n"]),
194 ]
195
196
197# ---------------- dev ----------------
198
199def _primes():
200 F = "primes"
201 def isprime(n):
202 if n < 2:
203 return False
204 if n % 2 == 0:
205 return n == 2
206 d = 3
207 while d * d <= n:
208 if n % d == 0:
209 return False
210 d += 2
211 return True
212 def sieve(n):
213 s = bytearray([1]) * (n + 1)
214 s[0:2] = b"\x00\x00"[: min(2, n + 1)]
215 for p in range(2, int(n ** 0.5) + 1):
216 if s[p]:
217 s[p * p :: p] = bytearray(len(s[p * p :: p]))
218 return s
219 S = sieve(2_000_000)
220 PR = [i for i in range(len(S)) if S[i]]
221 def lpf(n):
222 best, d = 1, 2
223 while d * d <= n:
224 while n % d == 0:
225 best, n = d, n // d
226 d += 1
227 return max(best, n) if n > 1 else best
228 return [
229 mk(F, "v1", "Read one number below 10^12. Print prime if it is a prime number, otherwise print composite (0 and 1 are composite for this task).",
230 lambda i: out("prime" if isprime(ints(i)[0]) else "composite"), ["7\n", "1\n"],
231 lambda r: line(r.choice([r.randint(0, 10 ** 12 - 1), r.choice(PR[-2000:]) * 1, r.choice(PR[1000:5000]) * r.choice(PR[1000:5000])])),
232 ["0\n", "2\n", "999999999989\n", "999966000289\n"]),
233 mk(F, "v2", "Read one number n below 1000000. Print how many primes are less than or equal to n.",
234 lambda i: out(sum(S[: ints(i)[0] + 1])), ["10\n", "1\n"], lambda r: line(r.randint(0, 999999)), ["0\n", "2\n", "999999\n"]),
235 mk(F, "v3", "Read one number n between 1 and 10000. Print the n-th prime number (the 1st prime is 2).",
236 lambda i: out(PR[ints(i)[0] - 1]), ["1\n", "5\n"], lambda r: line(r.randint(1, 10000)), ["10000\n"]),
237 mk(F, "v4", "Read one number between 2 and 10^12. Print its largest prime factor.",
238 lambda i: out(lpf(ints(i)[0])), ["12\n", "13\n"], lambda r: line(r.randint(2, 10 ** 12)),
239 ["2\n", "1000000000000\n", "999999999989\n", "999966000289\n"]),
240 ]
241
242
243def _fibo():
244 F = "fibo"
245 def fib(n, m=None):
246 a, b_ = 0, 1
247 for _ in range(n):
248 a, b_ = b_, a + b_
249 if m:
250 a, b_ = a % m, b_ % m
251 return a
252 def count_le(n):
253 a, b_, c = 0, 1, 0
254 while a <= n:
255 c += 1
256 a, b_ = b_, a + b_
257 return c
258 def even_sum(n):
259 a, b_, s = 0, 1, 0
260 while a <= n:
261 if a % 2 == 0:
262 s += a
263 a, b_ = b_, a + b_
264 return s
265 return [
266 mk(F, "v1", "Read one number n below 100000. Print the n-th Fibonacci number modulo 2^64, where F(0)=0 and F(1)=1.",
267 lambda i: out(fib(ints(i)[0], M64)), ["10\n", "0\n"], lambda r: line(r.randint(0, 99999)), ["1\n", "93\n", "94\n", "99999\n"]),
268 mk(F, "v2", "Read two numbers n and m on one line, n below 1000000 and m between 1 and 10^9. Print F(n) modulo m, where F(0)=0 and F(1)=1.",
269 lambda i: out(fib(ints(i)[0], ints(i)[1]) % ints(i)[1]), ["10 7\n", "0 1\n"],
270 lambda r: line(r.randint(0, 999999), r.randint(1, 10 ** 9)), ["1 1\n", "999999 1000000000\n"]),
271 mk(F, "v3", "Read one number n. Print how many terms of the sequence F(0)=0, F(1)=1, F(k)=F(k-1)+F(k-2) are less than or equal to n (count F(1) and F(2) separately).",
272 lambda i: out(count_le(ints(i)[0])), ["1\n", "10\n"], lambda r: line(r.randint(0, (1 << 64) - 1)), ["0\n", "18446744073709551615\n"]),
273 mk(F, "v4", "Read one number n below 10^18. Print the sum of the even Fibonacci numbers that are less than or equal to n (F(0)=0, F(1)=1).",
274 lambda i: out(even_sum(ints(i)[0])), ["10\n", "1\n"], lambda r: line(r.randint(0, 10 ** 18 - 1)), ["0\n", "999999999999999999\n"]),
275 ]
276
277
278def _wordst():
279 F = "wordst"
280 alpha = "abcdefgABCDEFG"
281 def gw(r):
282 rows = []
283 for _ in range(r.randint(1, 20)):
284 rows.append(" ".join("".join(r.choice(alpha) for _ in range(r.randint(1, 12))) for _ in range(r.randint(0, 10))))
285 return ("\n".join(rows) + "\n").encode()
286 def words(i):
287 return i.split()
288 return [
289 mk(F, "v1", "The input is lines of words; words are separated by single spaces and a line may be empty. Print the number of words.",
290 lambda i: out(len(words(i))), ["a bb\nccc\n", "\n"], gw),
291 mk(F, "v2", "The input is lines of words; words are separated by single spaces and a line may be empty. Print the length in bytes of the longest word (0 if there are no words).",
292 lambda i: out(max([len(w) for w in words(i)] or [0])), ["a bbb\ncc\n", "\n"], gw),
293 mk(F, "v3", "The input is lines of words; words are separated by single spaces and a line may be empty. Print how many words start with an uppercase letter A to Z.",
294 lambda i: out(sum(1 for w in words(i) if 65 <= w[0] <= 90)), ["Ab cd Ef\n", "\n"], gw),
295 mk(F, "v4", "The input is lines of words; words are separated by single spaces and a line may be empty. Print the number of the line (counting from 1) that has the most words; on a tie print the smallest such line number.",
296 lambda i: out(1 + max(range(len(i.split(b'\n')[:-1])), key=lambda k: (len(i.split(b'\n')[k].split()), -k))), ["a\nb c\n", "x\n"], gw, ["\n\n\n"]),
297 ]
298
299
300def _brackets():
301 F = "brackets"
302 def gp(r, al="()"):
303 n = r.randint(0, 400)
304 if r.random() < 0.5:
305 s, st = [], []
306 pairs = {"(": ")", "[": "]", "{": "}"}
307 opens = [c for c in al if c in pairs]
308 for _ in range(n):
309 if st and r.random() < 0.5:
310 s.append(pairs[st.pop()])
311 else:
312 c = r.choice(opens)
313 st.append(c)
314 s.append(c)
315 while st:
316 s.append(pairs[st.pop()])
317 s = "".join(s)
318 if r.random() < 0.3 and s:
319 k = r.randrange(len(s))
320 s = s[:k] + s[k + 1:]
321 else:
322 s = "".join(r.choice(al) for _ in range(n))
323 return (s + "\n").encode()
324 def bal(i, al):
325 pairs = {")": "(", "]": "[", "}": "{"}
326 st = []
327 for k, c in enumerate(i[:-1].decode()):
328 if c in "([{":
329 st.append(c)
330 else:
331 if not st or st[-1] != pairs[c]:
332 return False, k
333 st.pop()
334 return not st, (len(i) - 1 if st else -1)
335 def depth(i):
336 d = m = 0
337 for c in i[:-1].decode():
338 d += 1 if c == "(" else -1
339 m = max(m, d)
340 return m
341 def first_bad(i):
342 st = []
343 for k, c in enumerate(i[:-1].decode()):
344 if c == "(":
345 st.append(k)
346 elif st:
347 st.pop()
348 else:
349 return k
350 return st[0] if st else -1
351 return [
352 mk(F, "v1", "Read one line made only of ( and ) characters (possibly empty). Print yes if the parentheses are balanced, otherwise no.",
353 lambda i: out("yes" if bal(i, "()")[0] else "no"), ["(())()\n", "())\n"], lambda r: gp(r), ["\n", ")(\n", "((\n", "(()\n"]),
354 mk(F, "v2", "Read one line made only of ( and ) characters that is balanced. Print the maximum nesting depth (0 for an empty line).",
355 lambda i: out(depth(i)), ["(())()\n", "\n"], lambda r: _balanced(r), ["\n", "(" * 30000 + ")" * 30000 + "\n"]),
356 mk(F, "v3", "Read one line made only of the characters ( ) [ ] { } (possibly empty). Print yes if every bracket is closed by the matching kind in the right order, otherwise no.",
357 lambda i: out("yes" if bal(i, "()[]{}")[0] else "no"), ["([]{})\n", "([)]\n"], lambda r: gp(r, "()[]{}"), ["\n", "}\n", "([\n", "{}(\n"]),
358 mk(F, "v4", "Read one line made only of ( and ) characters. Scanning left to right, a ) with no open ( before it is unmatched; a ( never closed is unmatched. Print the position (counting from 0) of the first unmatched ) if there is one, otherwise of the leftmost unmatched (, otherwise print none.",
359 lambda i: out("none" if first_bad(i) < 0 else first_bad(i)), ["(()\n", "()\n"], lambda r: gp(r), ["\n", ")\n", "(((\n"]),
360 ]
361
362
363def _balanced(r):
364 n = r.randint(0, 300)
365 s, d = [], 0
366 for _ in range(n):
367 if d and r.random() < 0.5:
368 s.append(")")
369 d -= 1
370 else:
371 s.append("(")
372 d += 1
373 s.append(")" * d)
374 return ("".join(s) + "\n").encode()
375
376
377BUILDERS = {
378 "sumlist": _sumlist, "countb": _countb, "extrema": _extrema, "gcdlcm": _gcdlcm, "reverse": _reverse,
379 "multiples": _multiples, "caesar": _caesar, "digits": _digits,
380 "primes": _primes, "fibo": _fibo, "wordst": _wordst, "brackets": _brackets,
381}
382SPLIT = {**{f: "train" for f in TRAIN}, **{f: "dev" for f in DEV}}