나는 이렇게 학습한다/Algorithm & SQL

0824. N-th Power

daco2020 2022. 8. 24. 18:43
반응형

This kata is from check py.checkio.org

You are given an array with positive numbers and a non-negative 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 3^2 == 9;
  • array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.



Solution:

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

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

0826. A wolf in sheep's clothing  (0) 2022.08.26
0825. Find the first non-consecutive number  (0) 2022.08.25
Alternate capitalization  (0) 2022.08.23
Deodorant Evaporator  (0) 2022.08.22
Sum of Minimums!  (0) 2022.08.21