buggy_code:
def merge_sorted(a, b):
    result = []
    i = j = 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
    return result

original_code:
def merge_sorted(a, b):
    result = []
    i = j = 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

initial_error: AssertionError: merge_sorted([1,3],[2,4,5]) expected [1,2,3,4,5], got [1,2,3]


buggy_code:
def rotate_matrix(matrix):
    n = len(matrix)
    for i in range(n):
        for j in range(i, n):
            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
    return matrix

original_code:
def rotate_matrix(matrix):
    n = len(matrix)
    for i in range(n):
        for j in range(i, n):
            matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
    for row in matrix:
        row.reverse()
    return matrix


buggy_code:
def format_name(first, last):
    return f'{last}, {first}'

def process_user(first_name, last_name):
    return format_name(last_name, first_name)

original_code:
def format_name(first, last):
    return f'{last}, {first}'

def process_user(first_name, last_name):
    return format_name(first_name, last_name)

initial_error: AssertionError: expected 'Doe, John', got 'John, Doe'

input: John Doe

expected_output: Doe, John

input: Alice Smith

expected_output: Smith, Alice

input: A B

expected_output: B, A

input: X Y

expected_output: Y, X


buggy_code:
def apply_discount(price, discount):
    return price - (price * discount)

def calculate_total(price, discount_rate):
    return apply_discount(discount_rate, price)

original_code:
def apply_discount(price, discount):
    return price - (price * discount)

def calculate_total(price, discount_rate):
    return apply_discount(price, discount_rate)

initial_error: AssertionError: expected 80.0, got -19.8


buggy_code:
def join_parts(domain, path):
    return f'https://{domain}/{path}'

def build_url(domain, path):
    return join_parts(path, domain)

original_code:
def join_parts(domain, path):
    return f'https://{domain}/{path}'

def build_url(domain, path):
    return join_parts(domain, path)

initial_error: AssertionError: expected 'https://example.com/api', got 'https://api/example.com'

input: example.com

input: api

input: google.com

input: search

input: a.com

input: b

input: x.org

input: y


buggy_code:
def safe_divide(num, den):
    if den == 0:
        return 0
    return num / den

def divide_numbers(a, b):
    return safe_divide(b, a)

original_code:
def safe_divide(num, den):
    if den == 0:
        return 0
    return num / den

def divide_numbers(a, b):
    return safe_divide(a, b)

initial_error: AssertionError: expected 2.0, got 0.5


buggy_code:
def calc_area(w, h):
    return w * h

def create_rectangle(width, height):
    return {'w': width, 'h': height, 'area': calc_area(height, height)}

original_code:
def calc_area(w, h):
    return w * h

def create_rectangle(width, height):
    return {'w': width, 'h': height, 'area': calc_area(width, height)}

initial_error: AssertionError: doesn't strictly fail math but conceptually wrong


buggy_code:
def compute_pow(base, exp):
    return base ** exp

def power_wrapper(base, exp):
    return compute_pow(exp, base)

original_code:
def compute_pow(base, exp):
    return base ** exp

def power_wrapper(base, exp):
    return compute_pow(base, exp)

initial_error: AssertionError: expected 8, got 9


buggy_code:
seen = {1}
def filter_unique(items):
    res = []
    for item in items:
        if item not in seen:
            seen.add(item)
            res.append(item)
    return res

def get_unique_items(items):
    return filter_unique(items)

original_code:
def filter_unique(items, seen):
    res = []
    for item in items:
        if item not in seen:
            seen.add(item)
            res.append(item)
    return res

def get_unique_items(items):
    return filter_unique(items, set())

initial_error: AssertionError: test fails on second call


buggy_code:
total = 10
def add_to_total(val):
    global total
    total += val
    return total

def accumulate_values(vals):
    return [add_to_total(v) for v in vals]

original_code:
def accumulate_values(vals):
    total = 0
    res = []
    for v in vals:
        total += v
        res.append(total)
    return res

initial_error: AssertionError: expected [1, 3], got [1, 3] then [4, 6] on next call


buggy_code:
def helper(val, lst=[1]):
    lst.append(val)
    return lst

def append_to_default(val):
    return helper(val)

original_code:
def helper(val, lst=None):
    if lst is None:
        lst = []
    lst.append(val)
    return lst

def append_to_default(val):
    return helper(val)

initial_error: AssertionError: expected [2], got [1, 2]

function: helper


buggy_code:
calls = 5
def tracker():
    global calls
    calls += 1
    return calls

def count_calls(n):
    res = []
    for _ in range(n):
        res.append(tracker())
    return res

original_code:
def count_calls(n):
    calls = 0
    res = []
    for _ in range(n):
        calls += 1
        res.append(calls)
    return res

initial_error: AssertionError: expected [1, 2], got [3, 4] on second run

function: tracker


buggy_code:
words_cache = ['ERROR']
def add_word(w):
    words_cache.append(w)
    return ' '.join(words_cache)

def build_sentence(words):
    res = ''
    for w in words:
        res = add_word(w)
    return res

original_code:
def build_sentence(words):
    words_cache = []
    def add_word(w):
        words_cache.append(w)
        return ' '.join(words_cache)
    res = ''
    for w in words:
        res = add_word(w)
    return res

initial_error: AssertionError: expected 'hello world', got '... hello world'

input: hello world

expected_output: hello world

input: foo bar

expected_output: foo bar

input: a b c

expected_output: a b c

input: x

expected_output: x


buggy_code:
errors = ['fatal']
def log_error(err):
    errors.append(err)
    return errors

def collect_errors(err_list):
    for e in err_list:
        res = log_error(e)
    return res if err_list else []

original_code:
def collect_errors(err_list):
    errors = []
    def log_error(err):
        errors.append(err)
        return errors
    res = []
    for e in err_list:
        res = log_error(e)
    return res

initial_error: AssertionError: state leak between calls


buggy_code:
def get_first(lst):
    return lst[0]

def process_data(data):
    if not data:
        return None
    return [get_first(d) for d in data]

original_code:
def get_first(lst):
    if not lst:
        return None
    return lst[0]

def process_data(data):
    if not data:
        return []
    return [get_first(d) for d in data]

initial_error: IndexError: list index out of range


buggy_code:
def calc_avg(scores):
    return sum(scores) / len(scores)

def average_scores(students):
    return {k: calc_avg(v) for k, v in students.items()}

original_code:
def calc_avg(scores):
    if not scores:
        return 0
    return sum(scores) / len(scores)

def average_scores(students):
    return {k: calc_avg(v) for k, v in students.items()}

initial_error: ZeroDivisionError: division by zero


buggy_code:
def find_max(lst):
    return max(lst)

def find_max_nested(nested_lists):
    return [find_max(l) for l in nested_lists]

original_code:
def find_max(lst):
    if not lst:
        return None
    return max(lst)

def find_max_nested(nested_lists):
    return [find_max(l) for l in nested_lists]

initial_error: ValueError: max() arg is an empty sequence


buggy_code:
def extract_ext(filename):
    return filename.split('.')[1]

def get_extensions(files):
    return [extract_ext(f) for f in files]

original_code:
def extract_ext(filename):
    parts = filename.split('.')
    if len(parts) < 2:
        return ''
    return parts[-1]

def get_extensions(files):
    return [extract_ext(f) for f in files]

input: a.txt

input: b

input: a.txt

input: b.pdf

expected_output: txt pdf

input: noext


buggy_code:
def get_len(item):
    return len(item)

def get_lengths(items):
    return [get_len(i) for i in items]

original_code:
def get_len(item):
    if item is None:
        return 0
    return len(item)

def get_lengths(items):
    return [get_len(i) for i in items]

initial_error: TypeError: object of type 'NoneType' has no len()

input: abc

input: a b


buggy_code:
def to_int(s):
    return int(s)

def parse_integers(strings):
    return [to_int(s) for s in strings]

original_code:
def to_int(s):
    try:
        return int(s)
    except ValueError:
        return 0

def parse_integers(strings):
    return [to_int(s) for s in strings]

initial_error: ValueError: invalid literal for int() with base 10

input: abc

input: foo bar