行列の回転

ひたすら手続き的な記述をさけてみる。

def rotater(matrix):
    """
    rotate matrix

    >>> rotater([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
    """
    return [list(row) for row in zip(*reversed(matrix))]

NUMBER_CLASSES = [int, float, long]
def isnumber(obj):
    return any(isinstance(obj, cls) for cls in NUMBER_CLASSES)

def is_square_matrix(matrix):
    """
    return if matrix is sauare matrix and includes integer only.

    >>> is_square_matrix([[1, 2], [3, 4]])
    True

    >>> is_square_matrix([[1, 2], [3, 4, 5]])
    False

    >>> is_square_matrix([[1, 2], [3, 4], [5, 6]])
    False

    >>> is_square_matrix([[1], [2, 3]])
    False

    >>> is_square_matrix([[1]])
    True

    >>> is_square_matrix([['1', 2], [3, 4]])
    False

    >>> is_square_matrix([[1.0, 2], [3, 4.0]])
    True
    """
    n = len(matrix)
    return all(len(row) == n for row in matrix) and \
           all(all(isnumber(e) for e in row) for row in matrix)

def _test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    _test()