【每日一题1221】指数对比

:woman_mage: 相信你一眼就能看出 210和 215 哪个数字更大,以及210和 310 的比较结果。但是对于一些底数和指数都不相同的两个数字,例如39和 56,该如何比较它们的大小呢?

请编写一个函数,接收两个由[底数, 指数] 格式表示的数字,比较它们之间的大小。如果第一个数字较大则返回-1,如果它们相等则返回0,否则返回1。

示例:
输入: [2,10],[2,15],返回:1
输入: [3,9],[5,6],返回:-1

题目难度:中等
题目来源:CodeWars:Compare powers

def solution(n1: list, n2: list) -> int:
    # your code

assert solution([2,10], [2,15]) == 1
assert solution([2,10], [2,10]) == 0
assert solution([3,9],[5,6]) == -1
def solution(n1: list, n2: list) -> int:
    if n1[0] ** n1[1] > n2[0] ** n2[1]:
        return -1
    elif n1[0] ** n1[1] == n2[0] ** n2[1]:
        return 0
    else:
        return 1


assert solution([2, 10], [2, 15]) == 1
assert solution([2, 10], [2, 10]) == 0
assert solution([3, 9], [5, 6]) == -1
def solution(n1: list, n2: list) -> int:
    return -1 if n1[0] ** n1[1] > n2[0] ** n2[1] else 0 if n1[0] ** n1[1] == n2[0] ** n2[1] else 1


assert solution([2, 10], [2, 15]) == 1
assert solution([2, 10], [2, 10]) == 0
assert solution([3, 9], [5, 6]) == -1
def solution(n1: list, n2: list) -> int:
    print(n1[0] ** n1[1], n2[0] ** n2[1])
    if n1[0] ** n1[1] > n2[0] ** n2[1]:
        return -1
    elif n1[0] ** n1[1] == n2[0] ** n2[1]:
        return 0
    else:
        return 1

assert solution([2, 10], [2, 15]) == 1
assert solution([2, 10], [2, 10]) == 0
assert solution([3, 9], [5, 6]) == -1
def solution(n1: list, n2: list) -> int:
    if pow(n1[0],n1[1]) == pow(n2[0],n2[1]):
        return 0
    elif pow(n1[0],n1[1]) > pow(n2[0],n2[1]):
        return -1
    else:
        return 1

assert solution([2,10], [2,15]) == 1
assert solution([2,10], [2,10]) == 0
assert solution([3,9],[5,6]) == -1
def solution(n1: list, n2: list) -> int:
    if n1[0] ** n1[1] > n2[0] ** n2[1]:
        return -1
    elif n1[0] ** n1[1] == n2[0] ** n2[1]:
        return 0
    else:
        return 1

assert solution([2,10], [2,15]) == 1
assert solution([2,10], [2,10]) == 0
assert solution([3,9],[5,6]) == -1
def solution(n1: list, n2: list) -> int:
    # your code
    num1 = n1[0] ** n1[1]
    num2 = n2[0] ** n2[1]
    if num1 > num2:
        return -1
    elif num1 == num2:
        return 0
    else:
        return 1


assert solution([2, 10], [2, 15]) == 1
assert solution([2, 10], [2, 10]) == 0
assert solution([3, 9], [5, 6]) == -1
def solution(n1: list, n2: list) -> int:
    return 0 if pow(n1[0], n1[1]) == pow(n2[0], n2[1]) else 1 if pow(n1[0], n1[1]) < pow(n2[0], n2[1]) else -1


assert solution([2, 10], [2, 15]) == 1
assert solution([2, 10], [2, 10]) == 0
assert solution([3, 9], [5, 6]) == -1
import  math
def solution5(x:list,y:list):
    if x[0]>0 and y[0]>0:
        if math.pow(x[0],x[1])>math.pow(y[0],y[1]):
            return -1
        elif math.pow(x[0],x[1])<math.pow(y[0],y[1]):
            return 1
        elif math.pow(x[0],x[1])==math.pow(y[0],y[1]):
            return 0
    else:
        raise ValueError

assert solution5([2,10], [2,15]) == 1
assert solution5([2,10], [2,10]) == 0
assert solution5([3,9],[5,6]) == -1
def solution(n1: list, n2: list) -> int:
    a0,a1 = n1[0],n1[1]
    a = a0**a1
    b0,b1 = n2[0],n2[1]
    b = b0**b1
    if a < b: return 1
    elif a > b: return -1
    else: return 0
def solution(n1: list, n2: list) -> int:
    if n1[0] ** n1[1] > n2[0] ** n2[1]:
        return -1
    elif n1[0] ** n1[1] == n2[0] ** n2[1]:
        return 0
    else:
        return 1


assert solution([2, 10], [2, 15]) == 1
assert solution([2, 10], [2, 10]) == 0
assert solution([3, 9], [5, 6]) == -1
关闭