Last active
January 21, 2021 10:51
-
-
Save urpylka/af26b643825f3d3148bc8e87e92fad1d to your computer and use it in GitHub Desktop.
My implementation of rotating a quad matrix (90° clockwise)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
def rotate(matrix): | |
""" | |
Do not return anything, modify matrix in-place instead. | |
""" | |
n = len(matrix) | |
x = 0 | |
y = 0 | |
med = 0 | |
if n % 2 == 0: | |
med = int(n / 2) | |
else: | |
med = int(n / 2) + 1 | |
print("med " + str(med)) | |
for y in range(med): | |
x_max = n - y - 1 | |
if n % 2 != 0 and x_max == y: | |
x_max += 1 | |
for x in range(y, x_max): | |
print("y:" + str(y) + " x:" + str(x)) | |
y1 = y | |
x1 = x | |
new_val = matrix[y1][x1] | |
for i in range(4): | |
old_val = new_val | |
print(y1, x1) | |
y2 = y1 | |
y1 = x1 | |
x1 = n - y2 - 1 | |
print(n) | |
print(y1, x1) | |
print("---") | |
new_val = matrix[y1][x1] | |
matrix[y1][x1] = old_val | |
# a = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] | |
# m = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] | |
# e = [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] | |
# a = [[1,2,3],[4,5,6],[7,8,9]] | |
# m = [[1,2,3],[4,5,6],[7,8,9]] | |
# e = [[7,4,1],[8,5,2],[9,6,3]] | |
# a = [[2,29,20,26,16,28],[12,27,9,25,13,21],[32,33,32,2,28,14],[13,14,32,27,22,26],[33,1,20,7,21,7],[4,24,1,6,32,34]] | |
# m = [[2,29,20,26,16,28],[12,27,9,25,13,21],[32,33,32,2,28,14],[13,14,32,27,22,26],[33,1,20,7,21,7],[4,24,1,6,32,34]] | |
# e = [[4,33,13,32,12,2],[24,1,14,33,27,29],[1,20,32,32,9,20],[6,7,27,2,25,26],[32,21,22,28,13,16],[34,7,26,14,21,28]] | |
a = [[43,39,3,33,37,20,14],[9,18,9,-1,40,22,38],[14,42,3,23,12,14,32],[18,31,45,11,8,-1,31],[28,44,14,23,40,24,13],[29,45,33,45,20,0,45],[12,23,35,32,22,39,8]] | |
m = [[43,39,3,33,37,20,14],[9,18,9,-1,40,22,38],[14,42,3,23,12,14,32],[18,31,45,11,8,-1,31],[28,44,14,23,40,24,13],[29,45,33,45,20,0,45],[12,23,35,32,22,39,8]] | |
e = [[12,29,28,18,14,9,43],[23,45,44,31,42,18,39],[35,33,14,45,3,9,3],[32,45,23,11,23,-1,33],[22,20,40,8,12,40,37],[39,0,24,-1,14,22,20],[8,45,13,31,32,38,14]] | |
rotate(m) | |
print(a) | |
print(m) | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment