# Challenge 1¶

In this first challenge, we need to sum the even indexed items and then multiply this sum by the final element. The base example is:

$$[0,1,2,3,4,5] = (0+2+4)*5 = 30$$

Where 0,2,4 are the numbers at the even indexes (0,2,4). Remember that Python starts indexing at 0 and that 0 is even.

In [5]:
def checkio(array):

#make sure array is not empty
if array == []:
return 0

even_index = array[0::2] #create a list that is the even arrays

sum_even = sum(even_index) #sum this array

las_num = array[-1] #retrieve the final number to multiply the sum by

# return the sum multiplied by the final number
return sum_even*las_num

#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio([0, 1, 2, 3, 4, 5]) == 30, "(0+2+4)*5=30"
assert checkio([1, 3, 5]) == 30, "(1+5)*5=30"
assert checkio([6]) == 36, "(6)*6=36"
assert checkio([]) == 0, "An empty array = 0"


Code Review

• array[0::2]: subset the array from 0 to the end (the blank bit between ::), but takes every second index.
• array[-1]: subset the array by retrieving the final element (counting backwards using -1).

# Challenge 2¶

In this challenge, we want to find a secret message by returning the capital letters in a string. My strategy for tackling this challenge was to use a list comprehension that iterates through each letter in the text and determines if the letter is capitalize.

In [11]:
def find_message(text):

message = []
message[:] = [letter for letter in text if letter.isupper()==True]

return ''.join(message)

if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert find_message(u"How are you? Eh, ok. Low or Lower? Ohhh.") == "HELLO", "hello"
assert find_message(u"hello world!") == "", "Nothing"
assert find_message(u"HELLO WORLD!!!") == "HELLOWORLD", "Capitals"


Code Review

• The list comprehension should be self-explanatory if you have even a limited knowledge of control flows. The novel feature is that Python allows you to do this in a single line. Very Pythonic. The result is an array of just the capital letters from the string.
• ''.join(message): we combined the array of capital letters using the 'join function. We do not want any spaces, so we live the first bit blank. If we wanted to have a comma between each capital letter, we would write ','.join(message).

# Challenge 3¶

In this challenge, we want to raise the nth index to the nth power. This is very straightforward. The only twist is that we need to use a try except flow in case an index is passed that is outside of the range of the array.

In [7]:
def index_power(array, n):

try:
result = array[n]**(n)
return result
except IndexError:
return -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"