Sunday, July 16, 2017

Roy and Symmetric Logos- Practice Problem from HackerEarth and solution using python 2.7.6

Problem Statement:

Roy likes Symmetric Logos.
How to check whether a logo is symmetric?
Align the center of logo with the origin of Cartesian plane. Now if the colored pixels of the logo are symmetric about both X-axis and Y-axis, then the logo is symmetric.
You are given a binary matrix of size N x N which represents the pixels of a logo.
1 indicates that the pixel is colored and 0 indicates no color.
For instance: Take a 5x5 matrix as follows:








Graphically it is represented as follows:




















Observe that it is symmetric about both X-axis and Y-axis.
Let's take another example of 5x5 matrix:


Graphically it is represented as follows:



















Now this logo is symmetric about Y-axis but it is not symmetric about X-axis.
Your task is to output YES if the logo is symmetric else output NO.
Input:
First line contains T - number of test cases.
T test cases follow.
First line of each test case contains the N - size of matrix.
Next N lines contains binary strings of length N.
Output:
Print YES or NO in a new line for each test case
Constraints:
1 ≤ T ≤ 10
2 ≤ N ≤ 32
Note: There will always be at least 1 colored pixel in input data.

Programming using python 2.7.6:

The logo can be symmetric if it satisfies below condition:
  • If all the row values are palindrome and column values are palindrome. 
Take an example of below matrix:
1001
0000
0000
1001
Here all row entries for the given matrix are palindrome,i.e 1001,0000,0000,1001 
at the same time if you look at the column values also they all are palindrome, i.e 1001,0000,0000,1001. Hence this is a symmetric matrix.

Let's take another example of 4x4 matrix:
0101
0110
0110
0101
Here if you look at the first row ,i.e 0101 is not palindrome, though all column values are palindrome. Hence this is not a symmetric matrix.

With above approach below is the code:

Code in Python 2.7.6:

BinaryString=[]
result =[]
flag = 0
T = input()
while T:
    N = input()
    while N:
        BinaryString.append(raw_input())
        N -=1
    for val in BinaryString: #Check each row whether it's palindrome
        if val != val[::-1]: # If not then it's not symmetric anyways.
            flag = 1
            result.append('NO') # Result is No and break
            break
       
    if flag == 0: # if all row values are palindrome then check whether all column values are palindrome
            temp = ''
            for j in zip(*BinaryString): # check column values are palindrome
                for k in range(len(j)):
                    temp +=j[k]
                if temp != temp[::-1]: # If not then it's not symmetric anyways.
                    flag = 1
                    result.append('NO') # Result is No
                    break
                temp =''
            else:
                result.append('YES')
    flag = 0
    BinaryString =[]   
    T -=1
          
for res in result: print res 

1 comment: