kernelascent-tasks / data /medium.jsonl
muahmed7338's picture
Upload data/medium.jsonl with huggingface_hub
9898df9 verified
Raw History Blame Contribute Delete
34.9 kB
{"name": "count_pair_sums_with_duplicates", "fn": "count_pair_sums", "spec": "Given a list of ints nums (possibly empty, possibly containing duplicates) and an int target, return the number of index pairs (i, j) with i < j and nums[i] + nums[j] == target.", "reference_code": "def count_pair_sums(nums, target):\n seen = {}\n count = 0\n for x in nums:\n count += seen.get(target - x, 0)\n seen[x] = seen.get(x, 0) + 1\n return count\n", "buggy_code": "def count_pair_sums(nums, target):\n counts = {}\n for x in nums:\n counts[x] = counts.get(x, 0) + 1\n total = 0\n for x in counts:\n y = target - x\n if y in counts:\n if x < y:\n total += counts[x] * counts[y]\n elif x == y:\n total += counts[x] * (counts[x] - 1)\n return total\n", "sampler_code": "def sample(rng):\n n = rng.randint(2, 12)\n nums = rng.sample(range(-100, 101), n)\n i, j = rng.sample(range(n), 2)\n target = nums[i] + nums[j]\n return (nums, target)\n", "edge_code": "def edge(rng):\n k = rng.randint(0, 3)\n if k == 0:\n return ([], rng.randint(-5, 5))\n if k == 1:\n v = rng.randint(-5, 5)\n return ([v] * rng.randint(2, 6), 2 * v)\n if k == 2:\n v = rng.randint(1, 5)\n nums = [v] * rng.randint(2, 4) + [-v] * rng.randint(2, 4) + [0] * rng.randint(2, 3)\n rng.shuffle(nums)\n return (nums, 0)\n nums = [rng.randint(-3, 3) for _ in range(rng.randint(5, 12))]\n return (nums, 2 * rng.randint(-3, 3))\n", "examples": "[[[[1, 2, 3, 4], 5], 2], [[[3, 3, 3], 6], 3], [[[-2, 2, -2, 2, 0, 0], 0], 5]]", "tier": "medium", "split": "public", "id": "0074b16353c7"}
{"name": "count-diff-pairs", "fn": "count_diff_pairs", "spec": "Given a list of integers nums (possibly empty, may contain duplicates and negatives) and an integer k >= 0, return the number of index pairs (i, j) with i < j such that abs(nums[i] - nums[j]) == k.", "reference_code": "def count_diff_pairs(nums, k):\n from collections import Counter\n c = Counter(nums)\n if k == 0:\n return sum(v * (v - 1) // 2 for v in c.values())\n return sum(v * c.get(x + k, 0) for x, v in c.items())\n", "buggy_code": "def count_diff_pairs(nums, k):\n from collections import Counter\n c = Counter(nums)\n total = 0\n for x, v in c.items():\n total += v * c.get(x + k, 0)\n return total\n", "sampler_code": "def sample(rng):\n n = rng.randint(0, 12)\n nums = [rng.randint(-50, 50) for _ in range(n)]\n k = rng.randint(1, 8)\n return (nums, k)\n", "edge_code": "def edge(rng):\n case = rng.randint(0, 3)\n if case == 0:\n return ([], 0)\n if case == 1:\n v = rng.randint(-5, 5)\n return ([v] * rng.randint(1, 6), 0)\n if case == 2:\n nums = [rng.randint(-3, 3) for _ in range(rng.randint(2, 10))]\n return (nums, 0)\n nums = [rng.randint(-3, 3) for _ in range(rng.randint(2, 10))]\n return (nums, rng.randint(1, 3))\n", "examples": "[[[[1, 2, 3, 4], 1], 3], [[[1, 1, 1], 0], 3], [[[3, -1, 3, 7], 4], 4]]", "tier": "medium", "split": "public", "id": "0b35252be8f5"}
{"name": "competition_ranks_with_ties", "fn": "competition_ranks", "spec": "Given a list of integers `scores`, return a list of the same length where result[i] is the 1-based competition rank of scores[i], defined as 1 plus the number of elements strictly greater than scores[i]; tied values share the same rank and subsequent ranks are skipped ('1224' style). Domain: any list of ints, possibly empty, possibly containing duplicates.", "reference_code": "def competition_ranks(scores):\n result = []\n for x in scores:\n greater = 0\n for y in scores:\n if y > x:\n greater += 1\n result.append(greater + 1)\n return result\n", "buggy_code": "def competition_ranks(scores):\n order = sorted(scores, reverse=True)\n rank_of = {}\n for i, v in enumerate(order):\n rank_of[v] = i + 1\n return [rank_of[v] for v in scores]\n", "sampler_code": "def sample(rng):\n n = rng.randint(1, 8)\n vals = rng.sample(range(-50, 51), n)\n return (vals,)\n", "edge_code": "def edge(rng):\n r = rng.random()\n if r < 0.15:\n return ([],)\n if r < 0.4:\n n = rng.randint(1, 6)\n v = rng.randint(-5, 5)\n return ([v] * n,)\n n = rng.randint(2, 7)\n pool = rng.sample(range(-10, 11), rng.randint(1, 3))\n vals = [rng.choice(pool) for _ in range(n)]\n vals.append(rng.choice(vals))\n rng.shuffle(vals)\n return (vals,)\n", "examples": "[[[[3, 1, 2]], [1, 3, 2]], [[[10, 10, 5]], [1, 1, 3]], [[[7, 7, 7]], [1, 1, 1]]]", "tier": "medium", "split": "public", "id": "0ea099cf004c"}
{"name": "merge-touching-intervals", "fn": "merge_intervals", "spec": "Given a list of closed integer intervals [a, b] with a <= b (as 2-element lists), return a new list of merged intervals sorted by start, where any intervals that overlap OR merely touch at an endpoint (e.g. [1,4] and [4,6], or [3,3] and [3,5]) are combined into one; empty input returns [].", "reference_code": "def merge_intervals(intervals):\n ivs = sorted((a, b) for a, b in intervals)\n out = []\n for a, b in ivs:\n if out and a <= out[-1][1]:\n if b > out[-1][1]:\n out[-1] = [out[-1][0], b]\n else:\n out.append([a, b])\n return out\n", "buggy_code": "def merge_intervals(intervals):\n ivs = sorted(intervals)\n out = []\n for a, b in ivs:\n if out and a < out[-1][1]:\n out[-1][1] = max(out[-1][1], b)\n else:\n out.append([a, b])\n return out\n", "sampler_code": "def sample(rng):\n n = rng.randrange(0, 7)\n ivs = []\n for _ in range(n):\n a = 2 * rng.randrange(-50, 50) + 1\n b = a + 2 * rng.randrange(0, 12) + 1\n ivs.append([a, b])\n return (ivs,)\n", "edge_code": "def edge(rng):\n kind = rng.randrange(5)\n if kind == 0:\n return ([],)\n if kind == 1:\n p = rng.randrange(-10, 20)\n ivs = []\n for _ in range(rng.randrange(2, 5)):\n q = p + rng.randrange(1, 5)\n ivs.append([p, q])\n p = q\n rng.shuffle(ivs)\n return (ivs,)\n if kind == 2:\n x = rng.randrange(-5, 20)\n ivs = [[x, x], [x, x], [x, x + rng.randrange(0, 4)]]\n rng.shuffle(ivs)\n return (ivs,)\n if kind == 3:\n a = -rng.randrange(1, 10)\n b = a + rng.randrange(1, 6)\n ivs = [[a, b], [b, b + rng.randrange(0, 5)], [b - 1, b]]\n rng.shuffle(ivs)\n return (ivs,)\n a = rng.randrange(-10, 10)\n b = a + rng.randrange(1, 5)\n c = b + rng.randrange(0, 5)\n ivs = [[a, b], [a, b], [b, c]]\n rng.shuffle(ivs)\n return (ivs,)\n", "examples": "[[[[[1, 3], [2, 6], [8, 10], [15, 18]]], [[1, 6], [8, 10], [15, 18]]], [[[[1, 4], [4, 5]]], [[1, 5]]], [[[]], []]]", "tier": "medium", "split": "public", "id": "1850a9bccc06"}
{"name": "count-pairs-with-sum", "fn": "count_pairs_with_sum", "spec": "Return the number of index pairs (i, j) with i < j such that nums[i] + nums[j] == target. nums is a list of integers (possibly empty, may contain duplicates and negative values); target is any integer.", "reference_code": "def count_pairs_with_sum(nums, target):\n from collections import Counter\n cnt = Counter(nums)\n total = 0\n for v, c in cnt.items():\n w = target - v\n if w == v:\n total += c * (c - 1) // 2\n elif w in cnt and v < w:\n total += c * cnt[w]\n return total\n", "buggy_code": "def count_pairs_with_sum(nums, target):\n from collections import Counter\n cnt = Counter(nums)\n total = 0\n for v, c in cnt.items():\n w = target - v\n if w in cnt:\n total += c * cnt[w]\n # every unordered pair was counted twice (once from each side)\n return total // 2\n", "sampler_code": "def sample(rng):\n n = rng.randint(0, 12)\n nums = rng.sample(range(-30, 31), n)\n target = rng.randint(-40, 40)\n return (nums, target)\n", "edge_code": "def edge(rng):\n r = rng.random()\n if r < 0.15:\n return ([], rng.randint(-5, 5))\n half = rng.randint(-10, 10)\n target = 2 * half\n if r < 0.4:\n nums = [half] * rng.randint(2, 6)\n else:\n nums = [half] * rng.randint(2, 5)\n extra = rng.randint(0, 6)\n for _ in range(extra):\n nums.append(rng.randint(-15, 15))\n rng.shuffle(nums)\n return (nums, target)\n", "examples": "[[[[1, 5, 3], 6], 1], [[[3, 3, 3], 6], 3], [[[-2, 2, 0, 0, 0], 0], 4]]", "tier": "medium", "split": "public", "id": "32e3c0685897"}
{"name": "next_permutation_with_duplicates", "fn": "next_permutation", "spec": "Given a list of integers (possibly containing duplicates), return a NEW list that is the lexicographically next greater permutation of its elements; if no greater permutation exists (the list is non-increasing, including empty or single-element lists), return the elements sorted ascending. The input list must not be mutated.", "reference_code": "def next_permutation(nums):\n a = list(nums)\n n = len(a)\n i = n - 2\n while i >= 0 and a[i] >= a[i + 1]:\n i -= 1\n if i < 0:\n a.reverse()\n return a\n j = n - 1\n while a[j] <= a[i]:\n j -= 1\n a[i], a[j] = a[j], a[i]\n a[i + 1:] = a[i + 1:][::-1]\n return a\n", "buggy_code": "def next_permutation(nums):\n a = list(nums)\n n = len(a)\n i = n - 2\n while i >= 0 and a[i] > a[i + 1]:\n i -= 1\n if i < 0:\n a.reverse()\n return a\n j = n - 1\n while a[j] < a[i]:\n j -= 1\n a[i], a[j] = a[j], a[i]\n a[i + 1:] = a[i + 1:][::-1]\n return a\n", "sampler_code": "def sample(rng):\n n = rng.randint(1, 7)\n vals = rng.sample(range(-20, 21), n)\n if rng.random() < 0.15:\n vals.sort(reverse=True)\n return (vals,)\n", "edge_code": "def edge(rng):\n kind = rng.randint(0, 5)\n if kind == 0:\n return ([],)\n if kind == 1:\n return ([rng.randint(-5, 5)],)\n if kind == 2:\n v = rng.randint(-3, 3)\n return ([v] * rng.randint(2, 5),)\n if kind == 3:\n return ([1, 2, 2],)\n if kind == 4:\n return ([2, 1, 1],)\n n = rng.randint(2, 7)\n vals = [rng.randint(0, 2) for _ in range(n)]\n return (vals,)\n", "examples": "[[[[1, 2, 3]], [1, 3, 2]], [[[3, 2, 1]], [1, 2, 3]], [[[1, 2, 2]], [2, 1, 2]]]", "tier": "medium", "split": "public", "id": "330adc6f06bc"}
{"name": "remove-k-digits", "fn": "remove_k_digits", "spec": "Given a non-empty string num of decimal digits (leading zeros allowed) and an integer k with 0 <= k <= len(num), return the smallest integer obtainable by deleting exactly k digits from num, as a decimal string with no leading zeros ('0' if the value is zero).", "reference_code": "def remove_k_digits(num, k):\n stack = []\n for d in num:\n while k > 0 and stack and stack[-1] > d:\n stack.pop()\n k -= 1\n stack.append(d)\n if k > 0:\n stack = stack[:len(stack) - k]\n result = ''.join(stack).lstrip('0')\n return result if result else '0'\n", "buggy_code": "def remove_k_digits(num, k):\n if k >= len(num):\n return '0'\n stack = []\n for d in num:\n while k and stack and stack[-1] > d:\n stack.pop()\n k -= 1\n stack.append(d)\n if k:\n stack = stack[:-k]\n return ''.join(stack).lstrip('0')\n", "sampler_code": "def sample(rng):\n n = rng.randint(2, 10)\n num = rng.choice('123456789') + ''.join(rng.choice('0123456789') for _ in range(n - 1))\n k = rng.randint(0, (n - 1) // 2)\n return (num, k)\n", "edge_code": "def edge(rng):\n c = rng.randrange(5)\n if c == 0:\n n = rng.randint(1, 6)\n num = ''.join(rng.choice('0123456789') for _ in range(n))\n return (num, n)\n if c == 1:\n num = '0' * rng.randint(1, 5)\n return (num, rng.randint(0, len(num) - 1))\n if c == 2:\n lead = rng.choice('123456789')\n zeros = '0' * rng.randint(1, 5)\n return (lead + zeros, 1)\n if c == 3:\n zeros = rng.randint(1, 4)\n others = rng.randint(1, 3)\n digits = ['0'] * zeros + [rng.choice('123456789') for _ in range(others)]\n rng.shuffle(digits)\n return (''.join(digits), others)\n n = rng.randint(1, 3)\n num = ''.join(rng.choice('0123456789') for _ in range(n))\n return (num, 0)\n", "examples": "[[[\"1432219\", 3], \"1219\"], [[\"10200\", 1], \"200\"], [[\"100\", 1], \"0\"]]", "tier": "medium", "split": "public", "id": "3bea51bb5e3f"}
{"name": "range_compress", "fn": "compress_ranges", "spec": "Given a list of integers (unsorted, possibly containing duplicates and negatives), return a comma-separated string listing the sorted DISTINCT values, where every maximal run of consecutive integers of length >= 2 is written as 'a-b' and an isolated value is written as 'a'; an empty list yields ''.", "reference_code": "def compress_ranges(nums):\n vals = sorted(set(nums))\n if not vals:\n return \"\"\n parts = []\n start = prev = vals[0]\n for x in vals[1:]:\n if x == prev + 1:\n prev = x\n else:\n parts.append(str(start) if start == prev else f\"{start}-{prev}\")\n start = prev = x\n parts.append(str(start) if start == prev else f\"{start}-{prev}\")\n return \",\".join(parts)\n", "buggy_code": "def compress_ranges(nums):\n if not nums:\n return \"\"\n vals = sorted(nums)\n parts = []\n start = prev = vals[0]\n for x in vals[1:]:\n if x == prev + 1:\n prev = x\n else:\n parts.append(str(start) if start == prev else f\"{start}-{prev}\")\n start = prev = x\n parts.append(str(start) if start == prev else f\"{start}-{prev}\")\n return \",\".join(parts)\n", "sampler_code": "def sample(rng):\n k = rng.randint(1, 12)\n vals = rng.sample(range(-30, 60), k)\n return (vals,)\n", "edge_code": "def edge(rng):\n mode = rng.randint(0, 4)\n if mode == 0:\n return ([],)\n if mode == 1:\n return ([rng.randint(-10, 10)],)\n if mode == 2:\n v = rng.randint(-10, 10)\n return ([v] * rng.randint(2, 5),)\n if mode == 3:\n base = rng.randint(-8, 8)\n vals = [base, base + 1, base + 1, base + 2, base + 4, base + 4]\n rng.shuffle(vals)\n return (vals,)\n k = rng.randint(2, 10)\n vals = rng.choices(range(-6, 6), k=k)\n return (vals,)\n", "examples": "[[[[1, 2, 3, 5, 7, 8]], \"1-3,5,7-8\"], [[[4, 4, 5, 4]], \"4-5\"], [[[]], \"\"]]", "tier": "medium", "split": "public", "id": "4f73f89ad857"}
{"name": "merge-touching-intervals", "fn": "merge_intervals", "spec": "Given a list of closed integer intervals [a, b] with a <= b (any order), return the merged intervals sorted by start as a list of [start, end] lists; intervals that overlap OR merely touch (one ends exactly where another starts) must be merged into one. An empty input returns [].", "reference_code": "def merge_intervals(intervals):\n if not intervals:\n return []\n ivs = sorted((int(a), int(b)) for a, b in intervals)\n out = [[ivs[0][0], ivs[0][1]]]\n for a, b in ivs[1:]:\n last = out[-1]\n if a <= last[1]:\n if b > last[1]:\n last[1] = b\n else:\n out.append([a, b])\n return out\n", "buggy_code": "def merge_intervals(intervals):\n ivs = sorted((int(a), int(b)) for a, b in intervals)\n out = [[ivs[0][0], ivs[0][1]]]\n for a, b in ivs[1:]:\n last = out[-1]\n if a < last[1]:\n if b > last[1]:\n last[1] = b\n else:\n out.append([a, b])\n return out\n", "sampler_code": "def sample(rng):\n n = rng.randint(1, 6)\n ivs = []\n for _ in range(n):\n a = 2 * rng.randint(0, 5000)\n b = a + 2 * rng.randint(1, 400) + 1\n ivs.append([a, b])\n return (ivs,)\n", "edge_code": "def edge(rng):\n kind = rng.randint(0, 3)\n if kind == 0:\n return ([],)\n if kind == 1:\n cur = rng.randint(0, 50)\n ivs = []\n for _ in range(rng.randint(2, 5)):\n nxt = cur + rng.randint(1, 5)\n ivs.append([cur, nxt])\n cur = nxt\n rng.shuffle(ivs)\n return (ivs,)\n if kind == 2:\n a = rng.randint(0, 20)\n b = a + rng.randint(1, 5)\n ivs = [[a, b], [b, b], [b, b + rng.randint(1, 3)]]\n rng.shuffle(ivs)\n return (ivs,)\n a = rng.randint(-10, 10)\n return ([[a, a]],)\n", "examples": "[[[[[1, 3], [2, 6], [8, 10], [15, 18]]], [[1, 6], [8, 10], [15, 18]]], [[[[1, 4], [4, 5]]], [[1, 5]]], [[[]], []]]", "tier": "medium", "split": "public", "id": "5517481a9bc8"}
{"name": "luhn-check-digit", "fn": "luhn_check_digit", "spec": "Given a string of ASCII digits (the payload, possibly empty, length <= 40), return the Luhn check digit as an int 0-9: the digit which, when appended to the payload, makes the full number pass the Luhn mod-10 checksum (starting from the check digit on the right, every second digit is doubled, with doubled values > 9 reduced by 9).", "reference_code": "def luhn_check_digit(payload):\n total = 0\n # walk from the right: the digit just left of the check digit is doubled\n for i, ch in enumerate(reversed(payload)):\n d = ord(ch) - 48\n if i % 2 == 0:\n d *= 2\n if d > 9:\n d -= 9\n total += d\n return (10 - total % 10) % 10\n", "buggy_code": "def luhn_check_digit(payload):\n total = 0\n for i, ch in enumerate(payload):\n d = ord(ch) - 48\n if i % 2 == 0:\n d *= 2\n if d > 9:\n d -= 9\n total += d\n return (10 - total % 10) % 10\n", "sampler_code": "def sample(rng):\n # payloads for 8/12/16/20-digit account numbers (most common: 16-digit cards)\n n = rng.choice([15, 15, 15, 15, 7, 11, 19])\n return (\"\".join(str(rng.randrange(10)) for _ in range(n)),)\n", "edge_code": "def edge(rng):\n k = rng.randrange(6)\n if k == 0:\n return (\"\",)\n if k == 1:\n return (str(rng.randrange(10)),)\n if k == 2:\n # Amex-style 15-digit numbers -> 14-digit payload\n return (\"\".join(str(rng.randrange(10)) for _ in range(14)),)\n if k == 3:\n # 13-digit numbers -> 12-digit payload\n return (\"\".join(str(rng.randrange(10)) for _ in range(12)),)\n if k == 4:\n return (\"10\" * rng.choice([1, 2, 3]),)\n n = rng.choice([2, 4, 6, 8, 10])\n return (\"\".join(str(rng.randrange(10)) for _ in range(n)),)\n", "examples": "[[[\"7992739871\"], 3], [[\"453201511283036\"], 6], [[\"\"], 0]]", "tier": "medium", "split": "public", "id": "5ad0cd8d0fa5"}
{"name": "vigenere_cipher", "fn": "vigenere_encrypt", "spec": "Encrypt text with a Vigenere cipher using a non-empty key of lowercase letters: each ASCII letter is shifted forward by its key letter's value (a=0..z=25), preserving case, cycling through the key; non-letter characters are output unchanged and do NOT consume a key position (the key index only advances on letters).", "reference_code": "def vigenere_encrypt(text, key):\n out = []\n k = 0\n n = len(key)\n for ch in text:\n if 'a' <= ch <= 'z':\n shift = ord(key[k % n]) - 97\n out.append(chr((ord(ch) - 97 + shift) % 26 + 97))\n k += 1\n elif 'A' <= ch <= 'Z':\n shift = ord(key[k % n]) - 97\n out.append(chr((ord(ch) - 65 + shift) % 26 + 65))\n k += 1\n else:\n out.append(ch)\n return ''.join(out)\n", "buggy_code": "def vigenere_encrypt(text, key):\n out = []\n n = len(key)\n for i, ch in enumerate(text):\n shift = ord(key[i % n]) - 97\n if 'a' <= ch <= 'z':\n out.append(chr((ord(ch) - 97 + shift) % 26 + 97))\n elif 'A' <= ch <= 'Z':\n out.append(chr((ord(ch) - 65 + shift) % 26 + 65))\n else:\n out.append(ch)\n return ''.join(out)\n", "sampler_code": "def sample(rng):\n letters = 'abcdefghijklmnopqrstuvwxyz'\n alphabet = letters + letters.upper()\n n = rng.randint(1, 20)\n text = ''.join(rng.choice(alphabet) for _ in range(n))\n key = ''.join(rng.choice(letters) for _ in range(rng.randint(1, 6)))\n return (text, key)\n", "edge_code": "def edge(rng):\n letters = 'abcdefghijklmnopqrstuvwxyz'\n alphabet = letters + letters.upper()\n key = ''.join(rng.sample(letters, rng.randint(2, 5)))\n seps = ' ,.!?-0123456789_'\n words = []\n for _ in range(rng.randint(2, 4)):\n wl = rng.randint(1, 6)\n words.append(''.join(rng.choice(alphabet) for _ in range(wl)))\n text = rng.choice(seps).join(words)\n if rng.random() < 0.5:\n text = rng.choice(seps) + text\n if rng.random() < 0.5:\n text = text + rng.choice(seps)\n return (text, key)\n", "examples": "[[[\"attackatdawn\", \"lemon\"], \"lxfopvefrnhr\"], [[\"Hello, World!\", \"key\"], \"Rijvs, Uyvjn!\"], [[\"\", \"abc\"], \"\"]]", "tier": "medium", "split": "public", "id": "6614091c09d6"}
{"name": "lex_topo_order", "fn": "topo_order", "spec": "Given n nodes labeled 0..n-1 and a list of directed edges (u, v) meaning u must come before v (the edge list may contain repeated edges, each distinct edge counts once), return the lexicographically smallest topological ordering as a list of ints, or None if the graph has a cycle (including self-loops); n may be 0, in which case the answer is [].", "reference_code": "def topo_order(n, edges):\n import heapq\n indeg = [0] * n\n adj = [set() for _ in range(n)]\n for u, v in edges:\n if v not in adj[u]:\n adj[u].add(v)\n indeg[v] += 1\n heap = [i for i in range(n) if indeg[i] == 0]\n heapq.heapify(heap)\n order = []\n while heap:\n u = heapq.heappop(heap)\n order.append(u)\n for v in adj[u]:\n indeg[v] -= 1\n if indeg[v] == 0:\n heapq.heappush(heap, v)\n return order if len(order) == n else None\n", "buggy_code": "def topo_order(n, edges):\n import heapq\n indeg = [0] * n\n adj = [set() for _ in range(n)]\n for u, v in edges:\n adj[u].add(v)\n indeg[v] += 1\n heap = [i for i in range(n) if indeg[i] == 0]\n heapq.heapify(heap)\n order = []\n while heap:\n u = heapq.heappop(heap)\n order.append(u)\n for v in adj[u]:\n indeg[v] -= 1\n if indeg[v] == 0:\n heapq.heappush(heap, v)\n return order\n", "sampler_code": "def sample(rng):\n n = rng.randint(1, 8)\n perm = list(range(n))\n rng.shuffle(perm)\n pos = {v: i for i, v in enumerate(perm)}\n edges = []\n seen = set()\n m = rng.randint(0, n * 2)\n for _ in range(m):\n u = rng.randrange(n)\n v = rng.randrange(n)\n if u == v:\n continue\n if pos[u] > pos[v]:\n u, v = v, u\n if (u, v) in seen:\n continue\n seen.add((u, v))\n edges.append((u, v))\n rng.shuffle(edges)\n return (n, edges)\n", "edge_code": "def edge(rng):\n kind = rng.randrange(4)\n if kind == 0:\n return (0, [])\n if kind == 1:\n n = rng.randint(2, 6)\n perm = list(range(n))\n rng.shuffle(perm)\n edges = []\n for i in range(n - 1):\n e = (perm[i], perm[i + 1])\n edges.append(e)\n if i == 0 or rng.random() < 0.6:\n edges.append(e)\n rng.shuffle(edges)\n return (n, edges)\n if kind == 2:\n n = rng.randint(1, 5)\n v = rng.randrange(n)\n edges = [(v, v)]\n for _ in range(rng.randint(0, 3)):\n a, b = rng.randrange(n), rng.randrange(n)\n if a != b:\n edges.append((a, b))\n rng.shuffle(edges)\n return (n, edges)\n n = rng.randint(2, 7)\n perm = list(range(n))\n rng.shuffle(perm)\n k = rng.randint(2, n)\n edges = [(perm[i], perm[(i + 1) % k]) for i in range(k)]\n for i in range(k, n):\n edges.append((perm[rng.randrange(i)], perm[i]))\n rng.shuffle(edges)\n return (n, edges)\n", "examples": "[[[4, [[2, 0], [2, 1], [0, 3], [1, 3]]], [2, 0, 1, 3]], [[2, [[0, 1], [0, 1]]], [0, 1]], [[3, [[0, 1], [1, 2], [2, 1]]], null]]", "tier": "medium", "split": "public", "id": "a9070607b6c8"}
{"name": "gregorian_add_days", "fn": "add_days", "spec": "Given a valid proleptic Gregorian date (year >= 1, month 1-12, valid day) and an integer n with 0 <= n <= 100000 (resulting year stays <= 9999), return the list [year, month, day] of the date exactly n days later, using the full Gregorian leap-year rule (divisible by 4, except century years unless divisible by 400).", "reference_code": "def add_days(year, month, day, n):\n def is_leap(y):\n return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)\n\n def days_in_month(y, m):\n if m == 2:\n return 29 if is_leap(y) else 28\n if m in (4, 6, 9, 11):\n return 30\n return 31\n\n day += n\n while day > days_in_month(year, month):\n day -= days_in_month(year, month)\n month += 1\n if month > 12:\n month = 1\n year += 1\n return [year, month, day]\n", "buggy_code": "def add_days(year, month, day, n):\n def is_leap(y):\n return y % 4 == 0\n\n def days_in_month(y, m):\n if m == 2:\n return 29 if is_leap(y) else 28\n if m in (4, 6, 9, 11):\n return 30\n return 31\n\n day += n\n while day > days_in_month(year, month):\n day -= days_in_month(year, month)\n month += 1\n if month > 12:\n month = 1\n year += 1\n return [year, month, day]\n", "sampler_code": "def sample(rng):\n def is_leap(y):\n return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)\n year = rng.randint(1901, 2090)\n month = rng.randint(1, 12)\n if month == 2:\n dim = 29 if is_leap(year) else 28\n elif month in (4, 6, 9, 11):\n dim = 30\n else:\n dim = 31\n day = rng.randint(1, dim)\n n = rng.randint(0, 3000)\n return (year, month, day, n)\n", "edge_code": "def edge(rng):\n y = rng.choice([1700, 1800, 1900, 2100, 2200, 2300, 1600, 2000, 2400])\n t = rng.randint(0, 4)\n if t == 0:\n return (y, 2, 28, rng.randint(1, 5))\n if t == 1:\n return (y - 1, 12, rng.randint(1, 31), rng.randint(90, 150))\n if t == 2:\n return (y, rng.randint(1, 12), 1, 0)\n if t == 3:\n return (y, 1, 1, 365)\n return (1899, 3, 1, rng.randint(70000, 75000))\n", "examples": "[[[2023, 12, 25, 10], [2024, 1, 4]], [[2024, 2, 28, 2], [2024, 3, 1]], [[1900, 2, 28, 1], [1900, 3, 1]]]", "tier": "medium", "split": "public", "id": "bca3886c9ac1"}
{"name": "summarize-sorted-ranges", "fn": "summarize_ranges", "spec": "Given a non-decreasing list of integers (duplicates allowed, possibly empty), return a list of strings describing the maximal runs of consecutive integer values in order: a run covering the single value v is 'v', a run covering values a..b with a<b is 'a-b' (e.g. '-3--1'). Repeated values belong to the same run and never break it.", "reference_code": "def summarize_ranges(nums):\n out = []\n i = 0\n n = len(nums)\n while i < n:\n start = nums[i]\n end = start\n i += 1\n while i < n and nums[i] <= end + 1:\n end = nums[i]\n i += 1\n out.append(str(start) if start == end else f\"{start}-{end}\")\n return out\n", "buggy_code": "def summarize_ranges(nums):\n out = []\n i = 0\n n = len(nums)\n while i < n:\n start = nums[i]\n end = start\n i += 1\n while i < n and nums[i] == end + 1:\n end = nums[i]\n i += 1\n out.append(str(start) if start == end else f\"{start}-{end}\")\n return out\n", "sampler_code": "def sample(rng):\n k = rng.randint(0, 12)\n nums = sorted(rng.sample(range(-15, 40), k))\n return (nums,)\n", "edge_code": "def edge(rng):\n mode = rng.randint(0, 3)\n if mode == 0:\n x = rng.randint(-5, 5)\n return ([x] * rng.randint(2, 4),)\n if mode == 1:\n base = sorted(rng.sample(range(-10, 20), rng.randint(1, 8)))\n nums = []\n for x in base:\n nums.extend([x] * rng.randint(1, 3))\n if len(nums) == len(base):\n nums.append(nums[-1])\n return (sorted(nums),)\n if mode == 2:\n start = rng.randint(-8, 3)\n nums = []\n for i in range(rng.randint(2, 6)):\n nums.extend([start + i] * (2 if i % 2 == 0 else 1))\n return (nums,)\n lo = rng.randint(-6, 0)\n tail = sorted(rng.sample(range(lo + 3, lo + 15), 3))\n return ([lo, lo, lo + 1, lo + 1] + tail,)\n", "examples": "[[[[0, 1, 2, 4, 5, 7]], [\"0-2\", \"4-5\", \"7\"]], [[[1, 1, 2, 5, 5]], [\"1-2\", \"5\"]], [[[-3, -3, -2, 0]], [\"-3--2\", \"0\"]]]", "tier": "medium", "split": "public", "id": "bcd576d1025b"}
{"name": "sign-changes-ignore-zeros", "fn": "sign_changes", "spec": "Given a list of integers, return the number of times the sign flips between consecutive NONZERO elements (zeros are skipped entirely and never count as a sign); an empty list or a list with fewer than two nonzero elements returns 0.", "reference_code": "def sign_changes(xs):\n count = 0\n prev = 0\n for x in xs:\n if x == 0:\n continue\n s = 1 if x > 0 else -1\n if prev != 0 and s != prev:\n count += 1\n prev = s\n return count\n", "buggy_code": "def sign_changes(xs):\n count = 0\n for i in range(1, len(xs)):\n if (xs[i] > 0) != (xs[i - 1] > 0):\n count += 1\n return count\n", "sampler_code": "def sample(rng):\n n = rng.randrange(1, 13)\n xs = [rng.choice([-1, 1]) * rng.randrange(1, 10) for _ in range(n)]\n return (xs,)\n", "edge_code": "def edge(rng):\n k = rng.randrange(5)\n if k == 0:\n return ([],)\n if k == 1:\n return ([0] * rng.randrange(1, 6),)\n if k == 2:\n n = rng.randrange(2, 10)\n xs = [rng.choice([-1, 1]) * rng.randrange(1, 10) for _ in range(n)]\n xs.insert(rng.randrange(len(xs) + 1), 0)\n return (xs,)\n if k == 3:\n return ([0, rng.choice([-5, 5])] + [0] * rng.randrange(0, 3),)\n xs = [rng.choice([-3, 0, 0, 3]) for _ in range(rng.randrange(3, 12))]\n return (xs,)\n", "examples": "[[[[3, -1, 4, -2]], 3], [[[1, 0, 1]], 0], [[[]], 0]]", "tier": "medium", "split": "public", "id": "c2a60d284687"}
{"name": "strict-lis-length", "fn": "lis_length", "spec": "Return the length of the longest strictly increasing subsequence of a list of integers (may contain duplicates and negatives); an empty list returns 0.", "reference_code": "def lis_length(nums):\n import bisect\n tails = []\n for x in nums:\n i = bisect.bisect_left(tails, x)\n if i == len(tails):\n tails.append(x)\n else:\n tails[i] = x\n return len(tails)\n", "buggy_code": "def lis_length(nums):\n import bisect\n tails = []\n for x in nums:\n i = bisect.bisect_right(tails, x)\n if i == len(tails):\n tails.append(x)\n else:\n tails[i] = x\n return len(tails)\n", "sampler_code": "def sample(rng):\n k = rng.randint(0, 12)\n return (rng.sample(range(-50, 51), k),)\n", "edge_code": "def edge(rng):\n c = rng.randint(0, 3)\n if c == 0:\n return ([],)\n if c == 1:\n v = rng.randint(-5, 5)\n return ([v] * rng.randint(1, 6),)\n if c == 2:\n return ([rng.randint(-3, 3) for _ in range(rng.randint(2, 10))],)\n base = sorted(rng.sample(range(-10, 11), rng.randint(1, 5)))\n out = []\n for b in base:\n out.extend([b] * rng.randint(1, 3))\n return (out,)\n", "examples": "[[[[10, 9, 2, 5, 3, 7, 101, 18]], 4], [[[2, 2, 2]], 1], [[[-1, 0, 0, 1]], 3]]", "tier": "medium", "split": "public", "id": "d67464f91351"}
{"name": "split-top-level-commas", "fn": "split_top_level", "spec": "Split a string on commas that are not enclosed within any (balanced) parentheses, returning the list of substrings exactly as they appear (no stripping); commas inside parentheses are kept. Behaves like str.split(',') when there are no parentheses: an empty string yields [''] and a trailing top-level comma yields a trailing '' field. Input parentheses are always balanced.", "reference_code": "def split_top_level(s):\n parts = []\n depth = 0\n cur = []\n for ch in s:\n if ch == '(':\n depth += 1\n cur.append(ch)\n elif ch == ')':\n depth -= 1\n cur.append(ch)\n elif ch == ',' and depth == 0:\n parts.append(''.join(cur))\n cur = []\n else:\n cur.append(ch)\n parts.append(''.join(cur))\n return parts\n", "buggy_code": "def split_top_level(s):\n parts = []\n depth = 0\n cur = ''\n for ch in s:\n if ch == '(':\n depth += 1\n elif ch == ')':\n depth -= 1\n if ch == ',' and depth == 0:\n parts.append(cur)\n cur = ''\n else:\n cur += ch\n if cur:\n parts.append(cur)\n return parts\n", "sampler_code": "def sample(rng):\n def field(depth=0):\n n = rng.randint(1, 4)\n out = []\n for _ in range(n):\n r = rng.random()\n if r < 0.6 or depth >= 2:\n out.append(rng.choice('abcxyz'))\n else:\n inner = ','.join(field(depth + 1) for _ in range(rng.randint(0, 3)))\n out.append('(' + inner + ')')\n return ''.join(out)\n fields = [field() for _ in range(rng.randint(1, 5))]\n return (','.join(fields),)\n", "edge_code": "def edge(rng):\n cases = ['', ',', 'a,', ',,', 'a,(b,c),', '(a,b),', 'a,,', '((x)),', '()', 'a,b,c,,', '(,),', ',(),']\n if rng.random() < 0.3:\n letters = ''.join(rng.choice('abc') for _ in range(rng.randint(1, 3)))\n return (letters + '(' + ','.join(rng.choice('xy') for _ in range(rng.randint(0, 3))) + '),',)\n return (rng.choice(cases),)\n", "examples": "[[[\"a,(b,c),d\"], [\"a\", \"(b,c)\", \"d\"]], [[\"f(x,y),g(z)\"], [\"f(x,y)\", \"g(z)\"]], [[\"a,\"], [\"a\", \"\"]]]", "tier": "medium", "split": "public", "id": "d74ddbdf22e2"}