File size: 22,484 Bytes
85f14d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
��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']}]