University of Arizona, Department of Computer Science

CSc 120: Assert: List to string v1

Expected Behavior

The function shown below is a solution to the list to string (version 1) problem from Assignment 1:

def concat_elements(arglist, startpos, stoppos):
    result=""
    if startpos < 0:
        startpos = 0
    if stoppos >= len(arglist):
        stoppos = len(arglist)-1
    idx = startpos
    while idx <= stoppos:
        result += arglist[idx]
        idx += 1
        assert tail_of_result_is_ok(arglist, idx, result)
    return result

Write a function tail_of_result_is_ok(arglist, i, result) 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 the relationship that the tail of the string result should have with the ith element of arglist. You have to figure out what this relationship must be.

Think about what relationship the idxth element of arglist must have with the string result at the point immediately after the line of code

result += arglist[idx]

Now consider what you can say about this same relationship at the point in the code where the assert is shown (above).

Your function tail_of_result_is_ok(arglist, idx, result) should return True if this relationship is in fact satisfied given the values of arglist, idx, and result 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, arglist = ['aa','bb','cc','dd','ee','ff','gg','hh','ii'], startpos = 1 and stoppos = 6.

  1. Call: tail_of_result_is_ok(arglist, 2, 'bb')
    Return value: True

  2. Call: tail_of_result_is_ok(arglist, 2, 'cc')
    Return value: False

  3. Call: tail_of_result_is_ok(arglist, 3, 'cc')
    Return value: True

  4. Call: tail_of_result_is_ok(arglist, 3, 'dd')
    Return value: False

  5. Call: tail_of_result_is_ok(arglist, 3, 'cccccccccc')
    Return value: True

  6. Call: tail_of_result_is_ok(arglist, 5, 'abcdee')
    Return value: True

  7. Call: tail_of_result_is_ok(arglist, 5, 'aabbccddeeff')
    Return value: False

  8. Call: tail_of_result_is_ok(arglist, 5, 'eecc')
    Return value: False