Just practicing some assignments in an algorithms textbook using python3. This function is supposed to read a list/array of integers and return a new list/array of only the even integers. I feel like my implementation is correct but it is not returning the list as I expect.
Here is my function, and a quick test for it. I am printing out the length of the array as it is returned (6), and then printing the length immediately when the function is called (None)
def even_array(array1, new_array=[], index=0):
if index >= len(array1):
print(len(new_array))
return new_array
else:
if(array1[index] % 2) == 0: #this is an even number
new_array.append(array1[index])
even_array(array1, new_array, index + 1)
int_array = [1,2,3,4,5,5,6,99,102,104,22]
new_array = even_array(int_array)
print(len(new_array))
new_array = even_array(int_array)
for element in new_array:
print(element)
This is my first post on here, so any feedback on formatting or how much info I should include is appreciated :)