Answer to Question #138453 in Python for Harshitha

Question #138453
Consider the following Python function.
def mystery(l):
if l == []:
return(l)
else:
return(mystery(l[1:])+l[:1])
What does mystery([22,14,19,65,82,55]) return?
1
Expert's answer
2020-10-16T13:11:28-0400
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]"""


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS