Question: The following function takes a list arglist and returns a pair of lists, evens and odds , such that evens consists of the elements at

The following function takes a list arglist and returns a pair of lists, evens and odds, such thatevens consists of the elements at even-numbered positions of arglist, i.e., arglist[0],arglist[2], etc.; and odds consists of the elements at odd-numbered positions of arglist, i.e., arglist[1], arglist[3], etc.

def odds_and_evens(arglist): evens = [] odds = [] for i in range(len(arglist)): ith_element = arglist[i] if i % 2 == 0: evens.append(ith_element) else: odds.append(ith_element) assert ith_element_is_in_correct_list(arglist, i, evens, odds) return (evens,odds) 

Write a function ith_element_is_in_correct_list(arglist, i, evens, odds) that will be called by the assert at the end of each iteration of the loop shown above. The purpose of this function is to check that the ith element has been inserted into the right one of the two lists odds and evens.

Your function ith_element_is_in_correct_list(arglist, i, evens, odds) should behave as follows. Given:

arglist: the list being split into even- and odd-position lists;

i: the current value of the loop index;

evens and odds: the lists of even- and odd-position elements of arglist, respectively, up to (and including) the current iteration of the loop;

returns:

True if the ith element of arglist has been inserted into the end of the correct one of the two lists evens and oddsdepending on whether the value of i is even or odd.

False otherwise.

Programming Requirements

The function specified above should be implemented without using an if statement; also, you must not use a while or for loop. You can use the Boolean operators and, or, and not.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!