Home Alone – Kevin is not here!

First, let’s watch this short video clip from the movie Home Alone 2.

This video clip highlights a typical case of recursion.

Recursive Functions

A recursive function is a function which calls itself.
To make sure that a recursive function does not loop indefinitely it needs to check if a condition is met: If it is, it should stop calling itself.

In this challenge we are writing a recursive function that asks the person standing next to us to “pass this bag to Kevin”. Each person in the queue forwards this message by calling the same function. However if we reach the end of the queue or if we reach Kevin then we stop asking. So in this case we have two conditions that can stop the recursion: we reach the end of the queue/list or we reach Kevin.

Give this to Kevin: Recursive Function

For more examples based on recursive functions, check this post.

Share Button
Posted in Computer Science, Python - Advanced, Python Challenges

Our Latest Book