This kata is from check py.checkio.org

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 3^2 == 9;
* array = [1, 2, 3] and N = 3, but N is outside of the array, so the result is -1.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Starter code: def index(array, n):
	

Solutions:
def index(array, n):
	try:
		return array[n] ** n
	except:
		return -1


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


def index(array, n):
	try:
		return pow(array[n], n)
	except IndexError:
		return -1


from typing import List

def index(array: List[int], n: int) -> int:
	try:
		return pow(array[n], n)
	except IndexError:
		return -1


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


index = lambda a, n: -1 if len(a) <= n else a[n] ** n


def index(array, n):
	if n < len(array):
		return pow(array[n], n)
	return -1


import math

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


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


def index(a, n):
	if len(a) - 1 < n:
		return -1
	return a[n] ** n


def index(arr, n):
	try:
		return arr[n] ** n
	except:
		return -1


index = lambda a, n: -(n >= len(a)) or a[n] ** n


index = lambda a, i: a[i] ** i if len(a) - 1 >= i else -1


def index(array, n):
	for idx in range(len(array)):
		if idx == n:
			return array[idx] ** n
	return -1


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


def index(array, n):
	if 0 <= n < len(array):
		return pow(array[n], n)
	else:
		return -1


def index(array, n):
	try:
		return array[n] ** n
	except:
		return -1
print(index([1, 2, 3, 4], 2))


def index(array, n):
	if len(array) - 1 >= n:
		for i in range(len(array)):
			return array[n] ** n
	else:
		return -1


def index(array: list, n: int) -> int:
	for (k, i) in enumerate(range(len(array))):
		if i == n:
			return array[i] ** n
	return -1


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


import math

def index(array, n):
	if len(array) == 0:
		return -1
	for i in array:
		if n > len(array) - 1:
			return -1
		if i is array[n]:
			return math.pow(i, n)


import math

def index(array, n):
	for i in range(len(array)):
		if i == n:
			return array[i] ** n
	return -1


def index(array, n):
	if len(array) <= n:
		return -1
	else:
		nth = array[n]
		nth_sq = nth ** n
	return nth_sq


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


index = lambda l, n: l[n] ** n if n < len(l) else -1


def index(array, n):
	for (i, val) in enumerate(array):
		if n == i:
			return val ** n
	else:
		return -1


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


def index(array, n):
	try:
		x = array[n]
	except IndexError:
		return -1
	y = x ** n
	return y


def index(array, n):
	if len(array) < n + 1:
		return -1
	elif len(array) >= n + 1:
		number = array[abs(n)]
		return number ** n
	else:
		pass


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


def index(array, n):
	for (idx, i) in enumerate(array):
		if idx == n:
			x = array[idx] ** n
			return x
	else:
		return -1


def index(array, n):
	if n < len(array):
		a = array[n]
		num = a ** n
		return num
	else:
		return -1


def index(array, n):
	contar = 0
	for numero in array:
		contar = contar + 1
	if contar > n:
		y = array[n]
		x = y ** n
		return x
	else:
		return -1


def index(array, n):
	if n < len(array):
		target = array[n]
		ans = target ** n
		return ans
	else:
		return -1


def index(array, n):
	while True:
		try:
			return array[n] ** n
			break
		except IndexError:
			return -1


index = lambda a, n: a[n] ** n if n < len(a) else ~0


def index(array, n):
	try:
		a = array[n] ** n
		return a
	except IndexError:
		return -1


def index(array, n):
	nth_power = None
	try:
		nth_power = array[n] ** n
	except:
		nth_power = -1
	return nth_power


def index(array, n):
	try:
		x = array.pop(n)
	except IndexError:
		return -1
	return x ** n


def index(array, n):
	if n > len(array) - 1:
		return -1
	else:
		result = 1
		i = 0
		while i < n:
			result *= array[n]
			i += 1
		return result


def index(array, n):
	if n > len(array) - 1:
		return -1
	elif n == 0:
		return 1
	else:
		pw = array[n]
		i = 1
		while i != n:
			array[n] *= pw
			i += 1
		return array[n]


def index(array, n):
	try:
		if n:
			index = array[n]
			return index ** n
		elif n == 0:
			return 1
	except:
		return -1


def index(array, N):
	res = 0
	uzunluk = len(array)
	if N >= uzunluk:
		return -1
	ussuAlinacakSayi = array[N]
	return ussuAlinacakSayi ** N


def index(array, n):
	return pow(array.pop(n), n) if len(array) > n else -1


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


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


def index(array, n):
	if n >= 0 and n < len(array):
		x = 1
		for i in range(n):
			x = x * array[n]
		return x
	else:
		return -1


def index(array, n):
	try:
		return array[n] ** n
	except IndexError:
		return -1
	finally:
		pass


def index(array, n):
	try:
		return array[n] ** n
	except:
		return -1
array = [1, 3, 10, 100]
index(array, 3)


def index(array, n):
	if n >= len(array):
		return -1
	print(array, n, '', array[n], '', array[n] ** 2)
	return array[n] ** n


def index(array, n):
	try:
		return array[n] ** n
	except (IndexError, TypeError):
		return -1


def index(array, n):
	if len(array) - 1 < n:
		return -1
	for (ind, value) in enumerate(array):
		if ind == n:
			return value ** n


def index(array, n):
	arr = []
	for i in array:
		arr.append(pow(i, n))
	if n > len(arr) - 1:
		return -1
	return arr[n]


def index(array, n=-1):
	if n < 0 or n > len(array) - 1 or n == None:
		val = -1
		return val
	elif n <= len(array) - 1:
		val = array[n]
		val = val ** n
		return val