Spaces:
Running
Running
File size: 14,890 Bytes
2ce1061 61eccf3 2ce1061 61eccf3 2ce1061 8485798 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 | # server/tasks/task_medium.py
# 15 medium tasks: each function has TWO bugs (logic + edge case).
# Agent must fix both to get full reward.
import random
MEDIUM_TASKS = [
{
"task_id": "medium_001",
"domain": "data processing",
"instructions": (
"The function should return the average of a list, returning 0.0 for an empty list. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def safe_average(nums):
if len(nums) == 0:
return -1
total = 0
for n in nums:
total += n
return total / len(nums) + 1
""",
"fixed_code": """\
def safe_average(nums):
if len(nums) == 0:
return 0.0
total = 0
for n in nums:
total += n
return total / len(nums)
""",
"test_cases": [
{"input": [2, 4, 6], "expected": 4.0},
{"input": [], "expected": 0.0},
{"input": [10], "expected": 10.0},
],
"test_cases_description": "Average of list; empty list returns 0.0, not -1; no +1 added to result",
},
{
"task_id": "medium_002",
"domain": "string processing",
"instructions": (
"The function should count vowels in a string (case-insensitive). "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def count_vowels(s):
vowels = 'aeiou'
count = 0
for ch in s:
if ch in vowels:
count += 1
return count + 1
""",
"fixed_code": """\
def count_vowels(s):
vowels = 'aeiouAEIOU'
count = 0
for ch in s:
if ch in vowels:
count += 1
return count
""",
"test_cases": [
{"input": "hello", "expected": 2},
{"input": "HELLO", "expected": 2},
{"input": "rhythm", "expected": 0},
],
"test_cases_description": "Counts vowels case-insensitively without off-by-one",
},
{
"task_id": "medium_003",
"domain": "list operations",
"instructions": (
"The function should flatten a list of lists into one list. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def flatten(lists):
result = []
for sublist in lists:
for item in sublist:
result.append(item)
return result[1:]
""",
"fixed_code": """\
def flatten(lists):
result = []
for sublist in lists:
for item in sublist:
result.append(item)
return result
""",
"test_cases": [
{"input": [[[1, 2], [3, 4]]], "expected": [1, 2, 3, 4]},
{"input": [[[1]]], "expected": [1]},
{"input": [[[], [5, 6]]], "expected": [5, 6]},
],
"test_cases_description": "Flattens nested lists correctly without slicing off first element",
},
{
"task_id": "medium_004",
"domain": "math",
"instructions": (
"The function should return the GCD of two numbers. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def gcd(a, b):
while b != 0:
a = b
b = a % b
return b
""",
"fixed_code": """\
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
""",
"test_cases": [
{"input": [12, 8], "expected": 4},
{"input": [100, 75], "expected": 25},
{"input": [7, 3], "expected": 1},
],
"test_cases_description": "Correct GCD using Euclidean algorithm",
},
{
"task_id": "medium_005",
"domain": "data processing",
"instructions": (
"The function should count frequency of each element in a list and return a dict. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def count_frequency(lst):
freq = {}
for item in lst:
if item in freq:
freq[item] = 1
else:
freq[item] = freq[item] + 1
return freq
""",
"fixed_code": """\
def count_frequency(lst):
freq = {}
for item in lst:
if item in freq:
freq[item] += 1
else:
freq[item] = 1
return freq
""",
"test_cases": [
{"input": [1, 2, 2, 3, 3, 3], "expected": {1: 1, 2: 2, 3: 3}},
{"input": ["a", "b", "a"], "expected": {"a": 2, "b": 1}},
{"input": [5], "expected": {5: 1}},
],
"test_cases_description": "Correctly counts frequency; swapped if/else logic fixed",
},
{
"task_id": "medium_006",
"domain": "string processing",
"instructions": (
"The function should check if two strings are anagrams (case-insensitive). "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def are_anagrams(s1, s2):
if len(s1) != len(s2):
return True
return sorted(s1) == sorted(s2)
""",
"fixed_code": """\
def are_anagrams(s1, s2):
if len(s1) != len(s2):
return False
return sorted(s1.lower()) == sorted(s2.lower())
""",
"test_cases": [
{"input": ["listen", "silent"], "expected": True},
{"input": ["hello", "world"], "expected": False},
{"input": ["Listen", "Silent"], "expected": True},
],
"test_cases_description": "Anagram check with case-insensitivity and correct early-return logic",
},
{
"task_id": "medium_007",
"domain": "data processing",
"instructions": (
"The function should merge two sorted lists into one sorted list. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def merge_sorted(a, b):
result = []
i, j = 0, 0
while i < len(a) and j < len(b):
if a[i] < b[j]:
result.append(b[j])
i += 1
else:
result.append(a[i])
j += 1
result.extend(a[i:])
result.extend(b[j:])
return result
""",
"fixed_code": """\
def merge_sorted(a, b):
result = []
i, j = 0, 0
while i < len(a) and j < len(b):
if a[i] < b[j]:
result.append(a[i])
i += 1
else:
result.append(b[j])
j += 1
result.extend(a[i:])
result.extend(b[j:])
return result
""",
"test_cases": [
{"input": [[1, 3, 5], [2, 4, 6]], "expected": [1, 2, 3, 4, 5, 6]},
{"input": [[1, 2], [3, 4]], "expected": [1, 2, 3, 4]},
{"input": [[], [1, 2]], "expected": [1, 2]},
],
"test_cases_description": "Merges two sorted lists correctly",
},
{
"task_id": "medium_008",
"domain": "API handler",
"instructions": (
"The function validates a user registration dict. "
"It should return True only if 'email' and 'password' are present and password >= 8 chars. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def validate_registration(data):
if 'email' not in data:
return False
if len(data.get('password', '')) > 8:
return False
return True
""",
"fixed_code": """\
def validate_registration(data):
if 'email' not in data:
return False
if len(data.get('password', '')) < 8:
return False
return True
""",
"test_cases": [
{"input": {"email": "a@b.com", "password": "strongpass"}, "expected": True},
{"input": {"email": "a@b.com", "password": "short"}, "expected": False},
{"input": {"password": "strongpass"}, "expected": False},
],
"test_cases_description": "Validates registration with correct password length check",
},
{
"task_id": "medium_009",
"domain": "math",
"instructions": (
"The function should return True if a number is a perfect square. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def is_perfect_square(n):
if n < 0:
return True
root = int(n ** 0.5)
return root * root != n
""",
"fixed_code": """\
def is_perfect_square(n):
if n < 0:
return False
root = int(n ** 0.5)
return root * root == n
""",
"test_cases": [
{"input": 16, "expected": True},
{"input": 15, "expected": False},
{"input": -4, "expected": False},
],
"test_cases_description": "Correctly identifies perfect squares including negative number check",
},
{
"task_id": "medium_010",
"domain": "data processing",
"instructions": (
"The function should return the top-k most frequent elements in a list. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def top_k_frequent(nums, k):
freq = {}
for n in nums:
freq[n] = freq.get(n, 0) + 1
sorted_items = sorted(freq.items(), key=lambda x: x[1])
return [item[0] for item in sorted_items[:k]]
""",
"fixed_code": """\
def top_k_frequent(nums, k):
freq = {}
for n in nums:
freq[n] = freq.get(n, 0) + 1
sorted_items = sorted(freq.items(), key=lambda x: x[1], reverse=True)
return [item[0] for item in sorted_items[:k]]
""",
"test_cases": [
{"input": [[1, 1, 1, 2, 2, 3], 2], "expected": [1, 2]},
{"input": [[4, 4, 5, 5, 5], 1], "expected": [5]},
{"input": [[1, 2, 3], 3], "expected": [1, 2, 3]},
],
"test_cases_description": "Returns top-k frequent elements in descending frequency order",
},
{
"task_id": "medium_011",
"domain": "string processing",
"instructions": (
"The function should return the longest common prefix of a list of strings. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def longest_common_prefix(strs):
if not strs:
return ''
prefix = strs[1]
for s in strs:
while not s.startswith(prefix):
prefix = prefix[:-1]
if not prefix:
return ''
return prefix
""",
"fixed_code": """\
def longest_common_prefix(strs):
if not strs:
return ''
prefix = strs[0]
for s in strs:
while not s.startswith(prefix):
prefix = prefix[:-1]
if not prefix:
return ''
return prefix
""",
"test_cases": [
{"input": ["flower", "flow", "flight"], "expected": "fl"},
{"input": ["dog", "racecar", "car"], "expected": ""},
{"input": ["interview", "interact", "interface"], "expected": "inter"},
],
"test_cases_description": "Correct longest common prefix starting from index 0",
},
{
"task_id": "medium_012",
"domain": "list operations",
"instructions": (
"The function should rotate a list to the right by k positions. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def rotate_right(lst, k):
if not lst:
return lst
k = k % len(lst)
return lst[k:] + lst[:k]
""",
"fixed_code": """\
def rotate_right(lst, k):
if not lst:
return lst
k = k % len(lst)
return lst[-k:] + lst[:-k]
""",
"test_cases": [
{"input": [[1, 2, 3, 4, 5], 2], "expected": [4, 5, 1, 2, 3]},
{"input": [[1, 2, 3], 1], "expected": [3, 1, 2]},
{"input": [[], 3], "expected": []},
],
"test_cases_description": "Rotates list to the right correctly",
},
{
"task_id": "medium_013",
"domain": "API handler",
"instructions": (
"The function parses a query string into a dict. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def parse_query_string(query):
if not query:
return None
result = {}
for pair in query.split('&'):
if '=' in pair:
key, value = pair.split('=')
result[value] = key
return result
""",
"fixed_code": """\
def parse_query_string(query):
if not query:
return {}
result = {}
for pair in query.split('&'):
if '=' in pair:
key, value = pair.split('=', 1)
result[key] = value
return result
""",
"test_cases": [
{"input": "name=Alice&age=30", "expected": {"name": "Alice", "age": "30"}},
{"input": "", "expected": {}},
{"input": "key=value=extra", "expected": {"key": "value=extra"}},
],
"test_cases_description": "Parses query string; empty returns {}; key=value order correct; split on first = only",
},
{
"task_id": "medium_014",
"domain": "data processing",
"instructions": (
"The function should return all pairs of numbers in a list that sum to target. "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def find_pairs(nums, target):
pairs = []
seen = set()
for n in nums:
complement = target + n
if complement in seen:
pairs.append((complement, n))
seen.add(n)
return pairs
""",
"fixed_code": """\
def find_pairs(nums, target):
pairs = []
seen = set()
for n in nums:
complement = target - n
if complement in seen:
pairs.append((complement, n))
seen.add(n)
return pairs
""",
"test_cases": [
{"input": [[2, 7, 11, 15], 9], "expected": [(2, 7)]},
{"input": [[1, 2, 3, 4], 5], "expected": [(2, 3), (1, 4)]},
{"input": [[1, 2], 10], "expected": []},
],
"test_cases_description": "Finds all pairs summing to target using complement = target - n",
},
{
"task_id": "medium_015",
"domain": "math",
"instructions": (
"The function should return the nth Fibonacci number (0-indexed). "
"It has TWO bugs. Fix both."
),
"buggy_code": """\
def fibonacci(n):
if n == 0:
return 1
if n == 1:
return 1
a, b = 0, 1
for _ in range(2, n):
a, b = b, a + b
return b
""",
"fixed_code": """\
def fibonacci(n):
if n == 0:
return 0
if n == 1:
return 1
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
""",
"test_cases": [
{"input": 0, "expected": 0},
{"input": 1, "expected": 1},
{"input": 6, "expected": 8},
],
"test_cases_description": "Correct Fibonacci: fib(0)=0, fib(1)=1, fib(6)=8",
},
]
def get_random_medium_task() -> dict:
return random.choice(MEDIUM_TASKS).copy()
def get_task_by_id(task_id: str) -> dict:
for t in MEDIUM_TASKS:
if t["task_id"] == task_id:
return t.copy()
return random.choice(MEDIUM_TASKS).copy()
|