mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-06-07 22:52:44 -04:00
cleaning up and organizing old problems (builtin)
This commit is contained in:
parent
6afe96fa4d
commit
3fdbc2a605
106 changed files with 480 additions and 1472 deletions
src/builtin_structures
30
src/builtin_structures/rotate_NxN.py
Normal file
30
src/builtin_structures/rotate_NxN.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
def rotate_NxN(m):
|
||||
n = len(m)
|
||||
for layer in range(n//2):
|
||||
first = layer
|
||||
last = n - 1 - layer
|
||||
for i in range(first, last):
|
||||
offset = i - first
|
||||
top = m[first][i]
|
||||
m[first][i] = m[last-offset][first]
|
||||
m[last-offset][first] = m[last][last-offset]
|
||||
m[last][last-offset] = m[i][last]
|
||||
m[i][last] = top
|
||||
return m
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
m = [[1,2],[3,4]]
|
||||
mr = [[3,1],[4,2]]
|
||||
assert(rotate_NxN(m) == mr)
|
||||
m2 = [[1,2,3],[4,5,6],[7,8,9]]
|
||||
print(rotate_NxN(m2))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue