University of Arizona, Department of Computer Science

CSc 120: Assert: Evens and Odds 1

Expected Behavior

This programming problem involves identifying and checking invariants.

The following function takes a list arglist and returns a pair of lists, evens and odds, such that evens 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:

returns:

Programming Requirements

The function specified above should be implemented without using an if statement. You can use the Boolean operators and, or, and not if necessary.

Note that solutions that "accidentally" pass test cases will not get credit.

Examples

In the examples below, arglist = ['aa','bb','cc','dd','ee','ff','gg','hh','ii'].

  1. Call: ith_element_is_in_correct_list(arglist, 0, ['aa'], [])
    Return value: True

  2. Call: ith_element_is_in_correct_list(arglist, 0, ['aa'], [123,456,789])
    Return value: True

  3. Call: ith_element_is_in_correct_list(arglist, 1, [], [123,456,'bb'])
    Return value: True

  4. Call: ith_element_is_in_correct_list(arglist, 2, ['aa','cc','ee'], [123,456,'bb'])
    Return value: False

  5. Call: ith_element_is_in_correct_list(arglist, 5, ['aa','cc','ee'], ['ff'])
    Return value: True

  6. Call: ith_element_is_in_correct_list(arglist, 5, ['aa','cc'], ['ff', 'ee'])
    Return value: False