"""Simplify a split square:

A simple square is already simplified::

    >>> simplify(0)
    0

A split square containing four simple filled squares can be
simplified to a simple filled square::

    >>> simplify([1, 1, 1, 1])
    1

A split square containing four simple empty squares can be
simplified to a simple empty square::

    >>> simplify([0, 0, 0, 0])
    0

A split square containing mixed squares cannot be simplified::

    >>> simplify([1, 0, 1, 0])
    [1, 0, 1, 0]

These can be simplified even when nested::

    >>> simplify([1, 0, 1, [1, 1, 1, 1]])
    [1, 0, 1, 1]

Simplification should nest, so if we can simplify one split square
into a simple square and now an outer split square can be
simplified, it should::

    >>> simplify([1, 1, 1, [1, 1, 1, 1]])
    1

    >>> simplify([[1, 1, 1, 1], [1, 1, 1, 1], 1, 1])
    1

    >>> simplify([1, 0, [1, [0, 0, 0, 0], 1, [1, 1, 1, 1]], 1])
    [1, 0, [1, 0, 1, 1], 1]

Be careful that we don't "simplify" a set of matching mixed squares:

    >>> simplify([[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]])
    [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
"""


def simplify(s):
    """Simplify a split square:"""

    # START SOLUTION

    # Base case: already a filled square, cannot be simplified

    if type(s) == int:
        return s

    # It's a split square.
    #
    # Recursively simplify each square in split square.

    # Recurse before simplify a split square, so that we can
    # simplify things "coming up" --- this catches the case of
    # ``[1, 1, 1, [1, 1, 1, 1]]`` => ``1``
    # and other times when need to "simplify twice".

    s = [simplify(s) for s in s]

    # Simplify a split square if possible

    if type(s[0]) == int and s[0] == s[1] == s[2] == s[3]:
        return s[0]

    else:
        return s

    # END SOLUTION


if __name__ == "__main__":
    import doctest
    if doctest.testmod().failed == 0:
        print "\n*** ALL TESTS PASS; YOU MADE THAT SEEM SIMPLE!!\n"

"""Simplify a split square:

A simple square is already simplified::

    >>> simplify(0)
    0

A split square containing four simple filled squares can be
simplified to a simple filled square::

    >>> simplify([1, 1, 1, 1])
    1

A split square containing four simple empty squares can be
simplified to a simple empty square::

    >>> simplify([0, 0, 0, 0])
    0

A split square containing mixed squares cannot be simplified::

    >>> simplify([1, 0, 1, 0])
    [1, 0, 1, 0]

These can be simplified even when nested::

    >>> simplify([1, 0, 1, [1, 1, 1, 1]])
    [1, 0, 1, 1]

Simplification should nest, so if we can simplify one split square
into a simple square and now an outer split square can be
simplified, it should::

    >>> simplify([1, 1, 1, [1, 1, 1, 1]])
    1

    >>> simplify([[1, 1, 1, 1], [1, 1, 1, 1], 1, 1])
    1

    >>> simplify([1, 0, [1, [0, 0, 0, 0], 1, [1, 1, 1, 1]], 1])
    [1, 0, [1, 0, 1, 1], 1]

Be careful that we don't "simplify" a set of matching mixed squares:

    >>> simplify([[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]])
    [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
"""


def simplify(s):
    """Simplify a split square:"""

    # START SOLUTION

    # Base case: already a filled square, cannot be simplified

    if type(s) == int:
        return s

    # It's a split square.
    #
    # Recursively simplify each square in split square.

    # Recurse before simplify a split square, so that we can
    # simplify things "coming up" --- this catches the case of
    # ``[1, 1, 1, [1, 1, 1, 1]]`` => ``1``
    # and other times when need to "simplify twice".

    s = [simplify(s) for s in s]

    # Simplify a split square if possible

    if type(s[0]) == int and s[0] == s[1] == s[2] == s[3]:
        return s[0]

    else:
        return s

    # END SOLUTION


if __name__ == "__main__":
    import doctest
    if doctest.testmod().failed == 0:
        print "\n*** ALL TESTS PASS; YOU MADE THAT SEEM SIMPLE!!\n"

Simplify a split square:  A simple square is already simplified::      >>> simplify(0)     0  A split square containing four simple filled squares can be simplified to a simple filled square::      >>> simplify([1, 1, 1, 1])     1  A split square containing four simple empty squares can be simplified to a simple empty square::      >>> simplify([0, 0, 0, 0])     0  A split square containing mixed squares cannot be simplified::      >>> simplify([1, 0, 1, 0])     [1, 0, 1, 0]  These can be simplified even when nested::      >>> simplify([1, 0, 1, [1, 1, 1, 1]])     [1, 0, 1, 1]  Simplification should nest, so if we can simplify one split square into a simple square and now an outer split square can be simplified, it should::      >>> simplify([1, 1, 1, [1, 1, 1, 1]])     1      >>> simplify([[1, 1, 1, 1], [1, 1, 1, 1], 1, 1])     1      >>> simplify([1, 0, [1, [0, 0, 0, 0], 1, [1, 1, 1, 1]], 1])     [1, 0, [1, 0, 1, 1], 1]  Be careful that we don't "simplify" a set of matching mixed squares:      >>> simplify([[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]])     [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]] Simplify a split square: # START SOLUTION # Base case: already a filled square, cannot be simplified # It's a split square. # # Recursively simplify each square in split square. # Recurse before simplify a split square, so that we can # simplify things "coming up" --- this catches the case of # ``[1, 1, 1, [1, 1, 1, 1]]`` => ``1`` # and other times when need to "simplify twice". # Simplify a split square if possible # END SOLUTION