취미생활/CheckIO

[checkIO]Element 03.Index Power

우주먼지의하루 2020. 2. 24. 10:58
728x90

You are given an array with positive numbers and a number N. You should find the N-th power of the element in the array with the index N. If N is outside of the array, then return -1. Don't forget that the first element has the index 0.

Let's look at a few examples:
- array = [1, 2, 3, 4] and N = 2, then the result is 32 == 9;
- array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.

Input: Two arguments. An array as a list of integers and a number as a integer.

Output: The result as an integer.

Example:

index_power([1, 2, 3, 4], 2) == 9

index_power([1, 3, 10, 100], 3) == 1000000

index_power([0, 1], 0) == 1

index_power([1, 2], 3) == -1

 

12345

How it is used: This mission teaches you how to use basic arrays and indexes when combined with simple mathematics.

Precondition: 0 < len(array) ≤ 10
0 ≤ N
all(0 ≤ x ≤ 100 for x in array)

 

 

* 맨 마지막 숫자 N으로 배열 안의 숫자 선택

* 맨 마지막 숫자 N만큼 선택한 숫자 제곱

 

 

- 풀이

 

def index_power(array: list, n: int) -> int:
    if len(array) <= n:
        return -1
    else:
        choice_num = array[n]
        return (choice_num ** n)

 

- 다른 풀이

 

def index_power(array: list, n: int) -> int:
    """
        Find Nth power of the element with index N.
    """
    if n < len(array):
        return array[n]**n
    else:
        return -1

 

반응형

'취미생활 > CheckIO' 카테고리의 다른 글

[checkIO]Element 05.Secret Message  (0) 2020.03.01
[checkIO]Element 04.Digits Multiplication  (0) 2020.02.26
[checkIO]Element 02.Easy Unpack  (0) 2020.02.22
[checkIO]Element 01.Say Hi  (0) 2020.02.21
[checkIO]Home 12.Xs and Os Referee  (0) 2020.02.19