Skip to content

Instantly share code, notes, and snippets.

@elkym
Last active November 24, 2020 00:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elkym/d3e71bc219c6e681a24932f5dfdd309c to your computer and use it in GitHub Desktop.
Save elkym/d3e71bc219c6e681a24932f5dfdd309c to your computer and use it in GitHub Desktop.
Python-thing-recursion-wonderings.py
l1 = [1,3,9,10,4,3,2,5,17,14,11]
def splitIt(x):
'''
This recursive function takes a list of positive integers
x as argument and returns a tuple of two lists: a list of the even integers
in x followed by a list of the odd integers in x.
E.g. x = [3,2,1,5,4,6] gives [1,3,5] [2,4,6]
x = [1,3,9] gives [1,3,9] []
'''
# shell function for setting up variables that can be called again.
even_list = []
odd_list = []
recursive_even_odd_lists_generator(x, even_list, odd_list)
return (even_list, odd_list)
def number_is_odd(num):
if num % 2 != 0:
return True
def recursive_even_odd_lists_generator(x, even_list, odd_list):
# check to see if position 1 (or list[0]) is even or odd. If even, append to even_list. Else append to odd_list.
x_first = x[0]
if number_is_odd(x_first):
# add to odd list
odd_list.append(x_first)
else:
print("item is even" + str(x))
# add to even list
even_list.append(x_first)
print("even list " + str(even_list))
if len(x) == 1:
return x
return (recursive_even_odd_lists_generator(x[1:], even_list, odd_list))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment