Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
print("item is even" + str(x))
# add to even list
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