본문 바로가기

Python_Matter/Solve

Python Learn the basics Quiz 52

반응형

Q>

You are given an array with positive numbers and a number N.

(양수와 수 N의 배열이 주어집니다.)

You should find the N-th power of the element in the array with the index N.

(배열의 N 번째 지수는 N입니다.)

If N is outside of the array, then return -1.

(N이 배열 외부에 있으면 -1을 반환합니다.)

Don't forget that the first element has the index 0.

(첫 번째 요소의 인덱스가 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

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)

 

A>

def index_power(array: list, n: int) -> int:
    # n이 리스트의 값보다 작은가?
    if n < len(array):
        # 작으면 n번 인덱스를 가져와서 n을 제곱
        return array[n] ** n
    # 그 이외는 -1
    else:
        return -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!")

 

O>

Example:
9
Coding complete? Click 'Check' to review your tests and earn cool rewards!

Process finished with exit code 0

 

S>

https://py.checkio.org/

반응형

'Python_Matter > Solve' 카테고리의 다른 글

Python Learn the basics Quiz 54  (0) 2019.06.11
Python Learn the basics Quiz 53  (0) 2019.06.11
Python Learn the basics Quiz 51  (0) 2019.06.11
Python Learn the basics Quiz 50  (0) 2019.06.10
Python Learn the basics Quiz 49  (0) 2019.06.10