In: Computer Science
# Finds and returns only the even elements in the list u. # find_evens([1, 2, 3, 4] returns [2, 4] # find_evens([1, 2, 3, 4, 5, 6, 7, 8, 9, 10] returns [2, 4, 6, 8, 10] # u = [1, 2, 3, 4] v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def find_evens(u): return None # Replace this with your implementation
print('Testing find_evens') print(' find_evens(u): ' + str(find_evens(u))) print(' find_evens(v): ' + str(find_evens(v))) print()
Making code using recursion function
In Python
Here, I' m giving you the python code which contains two functions. Once is a recrussive function to check whether a element in list is even or not. And the next one is to pass the entire list from the list data and to give each digit for recrussive function. The code is given below;
#difining two strings for finding even numbers
u = [1, 2, 3, 4]
v = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
#defining recrusion function to check even number or not
def isEven(num):
if(num<2):
return (num%2 == 0)
return (isEven(num-2))
#defining find_evens function
def find_evens(list1):
#calling resrussive function isEven (using list comprehension)
even_nos = [num for num in list1 if (isEven(num)==True)]
return even_nos
print('Testing find_evens')
print('find_evens(u): ' + str(find_evens(u)))
print('find_evens(v): ' + str(find_evens(v)))
print()
OUTPUT: