测试评估
| 指标 | 结果 |
|---|---|
| Base 测试 | PASS |
| Plus 测试 | FAIL |
| 代码长度 | 1339 字符 |
模型生成的代码 (1339 字符)
def is_majority(sorted_arr, n, x):
"""
Returns True if x is the majority element in sorted_arr of length n,
i.e., appears more than n/2 times. The array is sorted in non-decreasing order.
"""
# Helper to find the first occurrence of x using binary search
def first_occurrence(arr, low, high, target):
result = -1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] == target:
result = mid
high = mid - 1 # continue searching left
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return result
# Helper to find the last occurrence of x using binary search
def last_occurrence(arr, low, high, target):
result = -1
while low <= high:
mid = low + (high - low) // 2
if arr[mid] == target:
result = mid
low = mid + 1 # continue searching right
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return result
first = first_occurrence(sorted_arr, 0, n - 1, x)
if first == -1:
return False
last = last_occurrence(sorted_arr, 0, n - 1, x)
count = last - first + 1
return count > n // 2
Base 失败测试用例
- 无失败测试
Plus 失败测试用例
[[], 1, 0]