University of Arizona, Department of Computer Science

CSc 120: Assert: Evens and Odds 3

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 list_lengths_ok_after_loop(arglist, evens, odds)

    return (evens,odds)

Write a function list_lengths_ok_after_loop(arglist, evens, odds) that will be called by the assert after the the loop has finished execution and before the function returns. The purpose of this function is to check that the lengths of the two lists evens and odds are the right values.

Your function list_lengths_ok_after_loop(arglist, 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

  1. Call: list_lengths_ok_after_loop(['aa', 'bb'], ['uuu'], ['vvv'])
    Return value: True

  2. Call: list_lengths_ok_after_loop(['aa', 'bb', 'cc'], ['aa'], ['bb'])
    Return value: False

  3. Call: list_lengths_ok_after_loop([], [], [])
    Return value: True

  4. Call: list_lengths_ok_after_loop([123], ['xx'], [])
    Return value: True

  5. Call: list_lengths_ok_after_loop([123, 234, 345], ['aa', 'bb'], [234])
    Return value: True

  6. Call: list_lengths_ok_after_loop([123, 234, 345], [123, 345], [234, 234])
    Return value: False