Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Index Power by colinmcnicholl
def index_power(array, n):
"""Input: Two arguments. An array as a list of integers and a number
as a integer.
This function finds the N-th power of the element in the array with
the index N. If N is outside of the array, then return -1.
Output: The result as an integer.
"""
return array[n]**n if n < len(array) else -1
if __name__ == '__main__':
#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!")
Feb. 12, 2019