LoginSignup
0
1

More than 3 years have passed since last update.

「世界で闘うプログラミング力を鍛える本」Pythonコード解答例 - 1.7 行列の回転

Last updated at Posted at 2020-02-01

「世界で闘うプログラミング力を鍛える本」Pythonコード解答例 - 1.7 行列の回転

目次

CHAP1. 配列と文字列

  1. 重複のない文字列
  2. 同じ文字の数を数える
  3. URLify
  4. 回文の順列
  5. 一発変換
  6. 文字列圧縮
  7. 行列の回転
  8. "0"の行列
  9. 文字列の回転

Pythonコード解答例


import numpy as np

def rotate(matrix):

    if matrix.ndim == 0 or matrix.shape[0] != matrix.shape[1]:
        return False

    n = matrix.shape[0]

    for layer in range(0,int(n/2)):

        first = layer
        last = n-1-layer

        for i in range(first,last):

            offset = i - first
            top = matrix[first,i]

            matrix[first,i] = matrix[last-offset,first]

            matrix[last-offset,first] = matrix[last,last-offset]

            matrix[last,last-offset] = matrix[i,last]

            matrix[i,last] = top

    return True

input_matrix_1 = np.matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])

print(input_matrix_1)

print(rotate(input_matrix_1))

print(input_matrix_1)

input_matrix_2 = np.matrix([[1,2,3],[4,5,6],[7,8,9]])

print(input_matrix_2)

print(rotate(input_matrix_2))

print(input_matrix_2)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1