University of Arizona, Department of Computer Science

CSc 120: Assert: Column to list

Expected Behavior

The function shown below is a solution to the column to list problem from Assignment 1:

def column2list(grid, n):
    col = []
    for i in range(len(grid)):
        assert col_end_ok_at_iteration_i(grid, i, n, col)
        col.append(grid[i][n])
        
    return col

Write a function col_end_ok_at_iteration_i(grid, i, n, col) that will be called by the assert at the beginning of each iteration of the loop shown above. The purpose of this function is to check the relationship that the last element of the list col should have with the appropriate element of the list-of-lists grid given the values of i and n. You have to figure out what this relationship must be.

Your function col_end_ok_at_iteration_i(grid, i, n, col) should return True if this relationship is in fact satisfied given the values of grid, i, n, and col at the point where the assert is made; and False if it is not.

Programming Requirements

The function specified above should be implemented without using an if statement or looping control structures. You may use the Boolean operators and, or, and not.

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

Examples

In the examples below, grid is the following list of lists:

      [ [ 'aa', 'bb', 'cc', 'dd' ],
        [ 'ee', 'ff', 'gg', 'hh', 'ii', 'jj' ],
        [ 'kk', 'll', 'mm', 'nn' ] ]
    

  1. Call: col_end_ok_at_iteration_i(grid, 0, 0, [])
    Return value: True

  2. Call: col_end_ok_at_iteration_i(grid, 0, 0, ['aa'])
    Return value: False

  3. Call: col_end_ok_at_iteration_i(grid, 2, 1, ['ff'])
    Return value: True

  4. Call: col_end_ok_at_iteration_i(grid, 1, 1, ['ff'])
    Return value: False

  5. Call: col_end_ok_at_iteration_i(grid, 1, 0, ['ff','aa'])
    Return value: True

  6. Call: col_end_ok_at_iteration_i(grid, 2, 2, ['ff','ll'])
    Return value: False

  7. Call: col_end_ok_at_iteration_i(grid, 2, 2, ['ff','gg'])
    Return value: True

  8. Call: col_end_ok_at_iteration_i(grid, 2, 3, ['dd','hh'])
    Return value: True