Search This Blog

Saturday, January 18, 2014

Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place?

################# Start of the Program

#!/usr/bin/python

a=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]
num_of_rows=len(a)
num_of_col=len(a[0])

def rotate_image():
    b=[ [ None for i in range(num_of_col) ] for j in range(num_of_rows) ]
    for i in range(num_of_col):
        for j in range(num_of_rows):
            b[i][j]=a[(num_of_rows - 1) - j][i]
    print "Given Matrix : ", a
    print "90 Degree Rotated Matrix : ", b
       
if __name__=='__main__':
    rotate_image()

################## End of the Program

No comments:

My Profile

My photo
can be reached at 09916017317