测试评估
| 指标 | 结果 |
|---|---|
| Base 测试 | PASS |
| Plus 测试 | PASS |
| 代码长度 | 746 字符 |
模型生成的代码 (746 字符)
def next_power_of_2(n: int) -> int:
"""
Returns the smallest power of 2 that is greater than or equal to n.
Examples:
next_power_of_2(0) -> 1
next_power_of_2(3) -> 4
next_power_of_2(16) -> 16
"""
if n <= 1:
return 1
# Subtract 1 so that if n is already a power of 2 we keep it unchanged
n -= 1
# Propagate the highest set bit to all lower bits
# We need to shift enough times to cover the bit length of n.
# Since Python ints have arbitrary precision, we use a loop based on bit_length.
shift = 1
while shift < n.bit_length():
n |= n >> shift
shift <<= 1
# Now n is of the form (2^k - 1). Adding 1 gives the next power of two.
return n + 1
Base 失败测试用例
- 无失败测试
Plus 失败测试用例
- 无失败测试