Spaces:
Sleeping
Sleeping
Deploy: tasks/medium.py
Browse files- tasks/medium.py +35 -0
tasks/medium.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from server.models import TaskInfo
|
| 2 |
+
|
| 3 |
+
MEDIUM_TASK = TaskInfo(
|
| 4 |
+
task_id="medium-1",
|
| 5 |
+
difficulty="medium",
|
| 6 |
+
description="Fix the logical bug in the binary search implementation.",
|
| 7 |
+
buggy_code="""def binary_search(arr, target):
|
| 8 |
+
left, right = 0, len(arr) - 1
|
| 9 |
+
while left < right:
|
| 10 |
+
mid = (left + right) // 2
|
| 11 |
+
if arr[mid] == target:
|
| 12 |
+
return mid
|
| 13 |
+
elif arr[mid] < target:
|
| 14 |
+
left = mid
|
| 15 |
+
else:
|
| 16 |
+
right = mid - 1
|
| 17 |
+
return -1""",
|
| 18 |
+
test_code="""
|
| 19 |
+
import unittest
|
| 20 |
+
class TestMedium(unittest.TestCase):
|
| 21 |
+
def test_found_middle(self):
|
| 22 |
+
self.assertEqual(binary_search([1, 2, 3, 4, 5], 3), 2)
|
| 23 |
+
def test_found_edges(self):
|
| 24 |
+
self.assertEqual(binary_search([1, 2, 3, 4, 5], 1), 0)
|
| 25 |
+
self.assertEqual(binary_search([1, 2, 3, 4, 5], 5), 4)
|
| 26 |
+
def test_not_found(self):
|
| 27 |
+
self.assertEqual(binary_search([1, 2, 3, 4, 5], 6), -1)
|
| 28 |
+
def test_empty(self):
|
| 29 |
+
self.assertEqual(binary_search([], 1), -1)
|
| 30 |
+
def test_single_element(self):
|
| 31 |
+
self.assertEqual(binary_search([5], 5), 0)
|
| 32 |
+
self.assertEqual(binary_search([5], 3), -1)
|
| 33 |
+
""",
|
| 34 |
+
optimal_time_seconds=0.05
|
| 35 |
+
)
|