| t1_001 |
| ORIG: |
| def binary_search(arr, target): |
| left, right = 0, len(arr) - 1 |
| while left <= right: |
| mid = (left + right) // 2 |
| if arr[mid] == target: |
| return mid |
| elif arr[mid] < target: |
| left = mid + 1 |
| else: |
| right = mid - 1 |
| return -1 |
| BUGGY: |
| def binary_search(arr, target): |
| left, right = 0, len(arr) |
| while left < right: |
| mid = (left + right) // 2 |
| if arr[mid] == target: |
| return mid |
| elif arr[mid] < target: |
| left = mid + 1 |
| else: |
| right = mid |
| return -1 |
| TESTS: |
| [{'input': [[1, 3, 5, 7, 9], 5], 'expected_output': 2}, {'input': [[1, 3, 5, 7, 9], 1], 'expected_output': 0}, {'input': [[1, 3, 5, 7, 9], 9], 'expected_output': 4}, {'input': [[1, 3, 5, 7, 9], 4], 'expected_output': -1}] |
|
|
| t1_003 |
| ORIG: |
| def find_max(nums): |
| max_val = nums[0] |
| for i in range(1, len(nums)): |
| if nums[i] > max_val: |
| max_val = nums[i] |
| return max_val |
| BUGGY: |
| def find_max(nums): |
| max_val = nums[0] |
| for i in range(1, len(nums) + 1): |
| if nums[i] > max_val: |
| max_val = nums[i] |
| return max_val |
| TESTS: |
| [{'input': [3, 1, 4, 1, 5, 9], 'expected_output': 9}, {'input': [1], 'expected_output': 1}, {'input': [-5, -1, -3], 'expected_output': -1}, {'input': [7, 7, 7], 'expected_output': 7}] |
|
|
| t1_005 |
| ORIG: |
| def sum_list(nums): |
| total = 0 |
| for i in range(len(nums)): |
| total += nums[i] |
| return total |
| BUGGY: |
| def sum_list(nums): |
| total = 0 |
| for i in range(len(nums) - 1): |
| total += nums[i] |
| return total |
| TESTS: |
| [{'input': [1, 2, 3], 'expected_output': 6}, {'input': [0], 'expected_output': 0}, {'input': [10, 20, 30, 40], 'expected_output': 100}, {'input': [], 'expected_output': 0}] |
|
|
| t1_006 |
| ORIG: |
| def is_sorted(lst): |
| for i in range(len(lst) - 1): |
| if lst[i] > lst[i + 1]: |
| return False |
| return True |
| BUGGY: |
| def is_sorted(lst): |
| for i in range(len(lst) - 1): |
| if lst[i] > lst[i + 1]: |
| return True |
| return False |
| TESTS: |
| [{'input': [1, 2, 3], 'expected_output': True}, {'input': [3, 1, 2], 'expected_output': False}, {'input': [1], 'expected_output': True}, {'input': [2, 2, 2], 'expected_output': True}] |
|
|
| t1_026 |
| ORIG: |
| def longest_common_prefix(strs): |
| if not strs: |
| return "" |
| prefix = strs[0] |
| for s in strs[1:]: |
| while not s.startswith(prefix): |
| prefix = prefix[:-1] |
| if not prefix: |
| return "" |
| return prefix |
| BUGGY: |
| 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 |
| TESTS: |
| [{'input': [['flower', 'flow', 'flight']], 'expected_output': 'fl'}, {'input': [['dog', 'racecar', 'car']], 'expected_output': ''}, {'input': [[]], 'expected_output': ''}, {'input': [['a']], 'expected_output': 'a'}] |
|
|
| t1_033 |
| ORIG: |
| def house_robber(nums): |
| if not nums: |
| return 0 |
| if len(nums) == 1: |
| return nums[0] |
| dp = [0] * len(nums) |
| dp[0] = nums[0] |
| dp[1] = max(nums[0], nums[1]) |
| for i in range(2, len(nums)): |
| dp[i] = max(dp[i-1], dp[i-2] + nums[i]) |
| return dp[-1] |
| BUGGY: |
| def house_robber(nums): |
| if not nums: |
| return 0 |
| if len(nums) == 1: |
| return nums[0] |
| dp = [0] * len(nums) |
| dp[0] = nums[0] |
| dp[1] = min(nums[0], nums[1]) |
| for i in range(2, len(nums)): |
| dp[i] = max(dp[i-1], dp[i-2] + nums[i]) |
| return dp[-1] |
| TESTS: |
| [{'input': [[1, 2, 3, 1]], 'expected_output': 4}, {'input': [[2, 7, 9, 3, 1]], 'expected_output': 12}, {'input': [[]], 'expected_output': 0}, {'input': [[5]], 'expected_output': 5}] |
|
|
| t1_034 |
| ORIG: |
| def house_robber(nums): |
| if not nums: |
| return 0 |
| if len(nums) == 1: |
| return nums[0] |
| dp = [0] * len(nums) |
| dp[0] = nums[0] |
| dp[1] = max(nums[0], nums[1]) |
| for i in range(2, len(nums)): |
| dp[i] = max(dp[i-1], dp[i-2] + nums[i]) |
| return dp[-1] |
| BUGGY: |
| def house_robber(nums): |
| if not nums: |
| return 0 |
| if len(nums) == 1: |
| return nums[0] |
| dp = [0] * len(nums) |
| dp[0] = nums[0] |
| dp[1] = max(nums[0], nums[1]) |
| for i in range(1, len(nums)): |
| dp[i] = max(dp[i-1], dp[i-2] + nums[i]) |
| return dp[-1] |
| TESTS: |
| [{'input': [[1, 2, 3, 1]], 'expected_output': 4}, {'input': [[2, 7, 9, 3, 1]], 'expected_output': 12}, {'input': [[]], 'expected_output': 0}, {'input': [[5]], 'expected_output': 5}] |
|
|
| t2_003 |
| ORIG: |
| def flatten(lst): |
| result = [] |
| for item in lst: |
| if isinstance(item, list): |
| result.extend(flatten(item)) |
| else: |
| result.append(item) |
| return result |
| BUGGY: |
| def flatten(lst): |
| result = [] |
| for item in lst: |
| if isinstance(item, list): |
| result.append(flatten(item)) |
| else: |
| result.append(item) |
| return result |
| TESTS: |
| [{'input': [[1, [2]], 3], 'expected_output': [1, 2, 3]}, {'input': [1, 2, 3], 'expected_output': [1, 2, 3]}, {'input': [[1, 2], [3, [4, 5]]], 'expected_output': [1, 2, 3, 4, 5]}] |
|
|
| t2_026 |
| ORIG: |
| def find_min_max(nums): |
| if not nums: |
| return None |
| mn = min(nums) |
| mx = max(nums) |
| return (mn, mx) |
| BUGGY: |
| def find_min_max(nums): |
| if not nums: |
| return None |
| mn = min(nums) |
| mx = max(nums) |
| return (mn, mn) |
| TESTS: |
| [{'input': [[1, 2, 5]], 'expected_output': (1, 5)}, {'input': [[3, 3]], 'expected_output': (3, 3)}, {'input': [[-1, 0, 1]], 'expected_output': (-1, 1)}, {'input': [[]], 'expected_output': None}] |
|
|
| t2_029 |
| ORIG: |
| def swap_halves(lst): |
| mid = len(lst) // 2 |
| left = lst[:mid] |
| right = lst[mid:] |
| return right + left |
| BUGGY: |
| def swap_halves(lst): |
| mid = len(lst) // 2 |
| left = lst[:mid] |
| right = lst[mid:] |
| return left + left |
| TESTS: |
| [{'input': [[1, 2, 3, 4]], 'expected_output': [3, 4, 1, 2]}, {'input': [[1, 2, 3]], 'expected_output': [3, 1, 2]}, {'input': [[1]], 'expected_output': [1]}, {'input': [[]], 'expected_output': []}] |
|
|
| t3_002 |
| ORIG: |
| 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: |
| 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 |
| TESTS: |
| [{'input': [[1, 2], [3, 4]], 'expected_output': [[3, 1], [4, 2]]}, {'input': [[1, 2, 3], [4, 5, 6], [7, 8, 9]], 'expected_output': [[7, 4, 1], [8, 5, 2], [9, 6, 3]]}] |
|
|
| t3_007 |
| ORIG: |
| def calc_area(w, h): |
| return w * h |
|
|
| def create_rectangle(width, height): |
| return {'w': width, 'h': height, 'area': calc_area(width, height)} |
| BUGGY: |
| def calc_area(w, h): |
| return w * h |
|
|
| def create_rectangle(width, height): |
| return {'w': width, 'h': height, 'area': calc_area(height, width)} |
| TESTS: |
| [{'input': [5, 10], 'expected_output': {'w': 5, 'h': 10, 'area': 50}}, {'input': [2, 3], 'expected_output': {'w': 2, 'h': 3, 'area': 6}}, {'input': [1, 1], 'expected_output': {'w': 1, 'h': 1, 'area': 1}}, {'input': [0, 5], 'expected_output': {'w': 0, 'h': 5, 'area': 0}}] |
|
|
| t3_009 |
| ORIG: |
| 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()) |
| BUGGY: |
| seen = set() |
| 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) |
| TESTS: |
| [{'input': [[1, 2, 2, 3]], 'expected_output': [1, 2, 3]}, {'input': [[1, 2, 2, 3]], 'expected_output': [1, 2, 3]}, {'input': [[4, 4, 5]], 'expected_output': [4, 5]}, {'input': [[4, 4, 5]], 'expected_output': [4, 5]}] |
|
|
| t3_010 |
| ORIG: |
| def accumulate_values(vals): |
| total = 0 |
| res = [] |
| for v in vals: |
| total += v |
| res.append(total) |
| return res |
| BUGGY: |
| total = 0 |
| def add_to_total(val): |
| global total |
| total += val |
| return total |
|
|
| def accumulate_values(vals): |
| return [add_to_total(v) for v in vals] |
| TESTS: |
| [{'input': [[1, 2]], 'expected_output': [1, 3]}, {'input': [[1, 2]], 'expected_output': [1, 3]}, {'input': [[5, 5]], 'expected_output': [5, 10]}, {'input': [[5, 5]], 'expected_output': [5, 10]}] |
|
|
| t3_011 |
| ORIG: |
| def helper(val, lst=None): |
| if lst is None: |
| lst = [] |
| lst.append(val) |
| return lst |
|
|
| def append_to_default(val): |
| return helper(val) |
| BUGGY: |
| def helper(val, lst=[]): |
| lst.append(val) |
| return lst |
|
|
| def append_to_default(val): |
| return helper(val) |
| TESTS: |
| [{'input': 1, 'expected_output': [1]}, {'input': 2, 'expected_output': [2]}, {'input': 3, 'expected_output': [3]}, {'input': 4, 'expected_output': [4]}] |
|
|
| t3_012 |
| ORIG: |
| def count_calls(n): |
| calls = 0 |
| res = [] |
| for _ in range(n): |
| calls += 1 |
| res.append(calls) |
| return res |
| BUGGY: |
| calls = 0 |
| def tracker(): |
| global calls |
| calls += 1 |
| return calls |
|
|
| def count_calls(n): |
| res = [] |
| for _ in range(n): |
| res.append(tracker()) |
| return res |
| TESTS: |
| [{'input': 2, 'expected_output': [1, 2]}, {'input': 2, 'expected_output': [1, 2]}, {'input': 3, 'expected_output': [1, 2, 3]}, {'input': 3, 'expected_output': [1, 2, 3]}] |
|
|
| t3_013 |
| ORIG: |
| 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 |
| BUGGY: |
| words_cache = [] |
| 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 |
| TESTS: |
| [{'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'}] |
|
|
| t3_014 |
| ORIG: |
| 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 |
| BUGGY: |
| errors = [] |
| 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 [] |
| TESTS: |
| [{'input': [['e1']], 'expected_output': ['e1']}, {'input': [['e2']], 'expected_output': ['e2']}, {'input': [['e3', 'e4']], 'expected_output': ['e3', 'e4']}, {'input': [['e5']], 'expected_output': ['e5']}] |
|
|