Skip to content

Instantly share code, notes, and snippets.

@RaminMammadzada
Last active September 8, 2022 08:34
Show Gist options
  • Select an option

  • Save RaminMammadzada/f7e45e3724b997446a93306a155197b7 to your computer and use it in GitHub Desktop.

Select an option

Save RaminMammadzada/f7e45e3724b997446a93306a155197b7 to your computer and use it in GitHub Desktop.
CodeSignal - rotateImage.py
Note: Try to solve this task in-place (with O(1) additional memory), since this is what you'll be asked to do during an interview.
You are given an n x n 2D matrix that represents an image. Rotate the image by 90 degrees (clockwise).
Example
For
a = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
the output should be
solution(a) =
[[7, 4, 1],
[8, 5, 2],
[9, 6, 3]]
Input/Output
[execution time limit] 4 seconds (py3)
[input] array.array.integer a
Guaranteed constraints:
1 ≤ a.length ≤ 100,
a[i].length = a.length,
1 ≤ a[i][j] ≤ 104.
[output] array.array.integer
[Python 3] Syntax Tips
# Prints help message to the console
# Returns a string
def helloWorld(name):
print("This prints to the console when you Run Tests")
return "Hello, " + name
@RaminMammadzada

Copy link
Copy Markdown
Author
def solution(a):
    row_len = len(a)
    col_len = len(a[0])
    
    new_matrix = []
    for ri in range(row_len):
        new_row = []
        for ci in range(col_len):
            new_row.insert(0, a[ci][ri])
        
        new_matrix.append(new_row)
        
    return new_matrix

@samyumobi

Copy link
Copy Markdown

def solution(a):
a.reverse()
for i in range(len(a)):
for j in range(i):
a[i][j], a[j][i] = a[j][i],a[i][j]
return a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment