def state(row, i):
    """This function takes in the current row and the index of the item,
       and returns its state as and 0 =< integer =< 7 """
    length = len(row)
    return row[i - 1] * 4 + row[i] * 2 + row[(i + 1) % length]