小编Vip*_*nha的帖子

我的函数nearest_power(base,num)在某些测试用例中失败了

def closest_power(base, num):
    '''
    base: base of the exponential, integer > 1
    num: number you want to be closest to, integer > 0
    Find the integer exponent such that base**exponent is closest to num.
    Note that the base**exponent may be either greater or smaller than num.
    In case of a tie, return the smaller value.
    Returns the exponent.

    '''

    result=0
    exp=1
    while base**exp<num:
        if base**exp <= num < base**(exp+1):
           result = exp

        elif num - base**exp <= base**(exp+1) - num: …
Run Code Online (Sandbox Code Playgroud)

python python-3.x

2
推荐指数
1
解决办法
1577
查看次数

标签 统计

python ×1

python-3.x ×1