[Python] Checkio Solution 解答 – Index Power

架網編程

Problem:

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.

 

Level:

Elementary

 

Input:

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

 

Output:

The result as an integer.

 

Precondition:

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

 

Source: https://py.checkio.org


My Solution:

def index_power(array, n): 
  if n < len(array): 
    return array[n] ** n 
  else: 
    return -1

Comment:

Again, it's like the exercise on Codecademy. =)

發佈日期:2017年9月14日 | 作者:橘小佑

Copyright © Vantopics