def mystery(l):
if l == []:
return []
else:
return (mystery(l[1:])+l[:1])
if __name__ == '__main__':
print(mystery([22,14,19,65,82,55]))
""" The function is recursive; a list is used as an argument. The function stops and the result is returned when (l [1:]) equals []. The result of executing l [: 1] is the first element of the list. Therefore, the result of the function will be the sum [] + l [n] + l [n-1] + ... + l [1] + l [0] where n is the length of the list l. Then, when calling the function with the argument [22,14 , 19,65,82,55] we get the list read from the end [55, 82, 65, 19, 14, 22]
Answer: [55, 82, 65, 19, 14, 22]"""
Comments
Leave a comment