Skip to content

Instantly share code, notes, and snippets.

@ZenulAbidin
Created September 5, 2022 22:04
Show Gist options
  • Save ZenulAbidin/8de122b9a0dd3f2f638ed36d6f7898ae to your computer and use it in GitHub Desktop.
Save ZenulAbidin/8de122b9a0dd3f2f638ed36d6f7898ae to your computer and use it in GitHub Desktop.
non-recursive functions in Python
# Credits: https://medium.com/@ujjawalsinhacool16021998/easily-convert-recursive-solutions-to-non-recursive-alternatives-4fa6e1acf702
# Simple function, two branches which does not return a value
def simple2_noreturn(inputs):
CALL, HANDLE = 0, 1
call_stack = [(CALL, inputs)]
while call_stack:
action, data = call_stack.pop()
if action == CALL:
call_stack.append((HANDLE, some_data))
call_stack.append((CALL, some_other_data))
call_stack.append((CALL, some_data))
else: # HANDLE
# do something with data
# Simple function, 2 branches, which returns a value
def simple2_return(inputs):
CALL, HANDLE = 0, 1
call_stack = [(CALL, inputs)]
return_stack = []
while call_stack:
action, data = call_stack.pop()
if action == CALL:
# place some condition here to prevent
# endless looping, and to cut the tree
# branches somewhere.
call_stack.append((HANDLE, some_data))
call_stack.append((CALL, some_other_data))
call_stack.append((CALL, some_data))
else: # HANDLE
# pop items from return_stack
# use them to calculate something
# and push that something to return_stack
return return_stack[-1] # return top value from the return_stack
# Simple function, 3 branches, third one conditional, which returns a value.
def simple2_return(inputs):
CALL, HANDLE = 0, 1
call_stack = [(CALL, inputs)]
return_stack = []
while call_stack:
action, data = call_stack.pop()
if action == CALL:
# place some condition here to prevent
# endless looping, and to cut the tree
# branches somewhere.
call_stack.append((HANDLE, some_data))
call_stack.append((CALL, some_other_data))
call_stack.append((CALL, some_data))
if some condition passes:
call_stack.append((CALL, some_other_data))
else: # HANDLE
# pop items from return_stack
# use them to calculate something
# and push that something to return_stack
return return_stack[-1] # return top value from the return_stack
# Simple function, 3 branches, third one looping, which returns a value.
def simple2_return(inputs):
CALL, HANDLE = 0, 1
call_stack = [(CALL, inputs)]
return_stack = []
while call_stack:
action, data = call_stack.pop()
if action == CALL:
# place some condition here to prevent
# endless looping, and to cut the tree
# branches somewhere.
call_stack.append((HANDLE, some_data))
call_stack.append((CALL, some_other_data))
call_stack.append((CALL, some_data))
# Preprocessing of while condition
while some condition passes:
call_stack.append((CALL, some_other_data))
# Postprocessing of while condiion
else: # HANDLE
# pop items from return_stack
# use them to calculate something
# and push that something to return_stack
return return_stack[-1] # return top value from the return_stack
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment