본문 바로가기
카테고리 없음

[CheckIO] Solve Index Power

by UltraLowTemp-Physics 2021. 3. 10.
반응형

Problem: py.checkio.org/en/mission/index-power/
My Solution: 

def index_power(array: list, n: int) -> int:
    """
        Find Nth power of the element with index N.
    """

    return array[n] ** n if len(array) > n else -1
    

if __name__ == '__main__':
    print('Example:')
    print(index_power([1, 2, 3, 4], 2))
    
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert index_power([1, 2, 3, 4], 2) == 9, "Square"
    assert index_power([1, 3, 10, 100], 3) == 1000000, "Cube"
    assert index_power([0, 1], 0) == 1, "Zero power"
    assert index_power([1, 2], 3) == -1, "IndexError"
    print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")

댓글