Skip to content

Instantly share code, notes, and snippets.

@ericdorsey
Created April 19, 2015 02:15
Show Gist options
  • Save ericdorsey/96a0ce65e4b51b6177db to your computer and use it in GitHub Desktop.
Save ericdorsey/96a0ce65e4b51b6177db to your computer and use it in GitHub Desktop.
Inception in Python
def inception(target_depth, current_depth=0, going_deeper=True):
if going_deeper == True:
if current_depth < target_depth:
current_depth += 1
if current_depth != target_depth:
print("Dream Level: {0}, going deeper..".format(current_depth))
elif current_depth == target_depth:
print("Dream Level: {0}, coming back up..".format(current_depth))
return inception(target_depth, current_depth, True)
elif current_depth == target_depth:
return inception(target_depth, current_depth, False)
elif going_deeper == False:
current_depth -= 1
if current_depth != 0:
print("Dream Level: {0}, coming back up..".format(current_depth))
return inception(target_depth, current_depth, False)
if current_depth == 0:
print("No dream level, back at reality.")
return
print("")
inception(4)
print("")
@ericdorsey
Copy link
Author

From the movie Inception, implemented in Python w/ recursion (recursion is the perfect mental model for the dream levels in the movie!)

Inception Movie:

@ericdorsey
Copy link
Author

Example output:

Dream Level: 1, going deeper..
Dream Level: 2, going deeper..
Dream Level: 3, going deeper..
Dream Level: 4, coming back up..
Dream Level: 3, coming back up..
Dream Level: 2, coming back up..
Dream Level: 1, coming back up..
No dream level, back at reality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment