Exercise 3
write a python algorithm as function that takes as parameter a list l and returns a tuple of two lists (l_old , l_even) where l_even is make up by the elements of l of even index and l_old is make up by the list element of odd index. Example: if:
L = ["Python" , "Java" , "C++" , "C#" , "VB.Net" , "Javascript"]
the algorithm return :
(['Python', 'C++', 'VB.Net'], ['Java', 'C#', 'Javascript'])
Solution
def odd_event(L):
# getting the length of list
n = len(L)
# initializing the even an
l_odd = []
l_even = []
# build the even and odd list
for i in range(0 , n):
if( i%2 == 0):
l_even.append(L[i])
else:
l_odd.append(L[i])
return (l_even , l_odd)
# Example
L = ["Python" , "Java" , "C++" , "C#" , "VB.Net" , "Javascript"]
print(odd_event(L))
# The output is : (['Python', 'C++', 'VB.Net'], ['Java', 'C#', 'Javascript'])
Younes Derfoufi
my-courses.net
my-courses.net