File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Grid (object ):
2
+ def __init__ (self , rows ):
3
+ self .rows = rows
4
+ def canBeSorted (self ):
5
+ temp = []
6
+ for row in self .rows :
7
+ temp .append ("" .join (sorted (row )))
8
+ # check columns
9
+ for j in xrange (len (temp [0 ])):
10
+ col = [temp [i ][j ] for i in xrange (len (self .rows ))]
11
+ if col != sorted (col ):
12
+ return False
13
+ return True
14
+
15
+ def getInput ():
16
+ numTests = input ()
17
+ testcases = []
18
+ for t in xrange (numTests ):
19
+ numRows = input ()
20
+ rows = []
21
+ for row in xrange (numRows ):
22
+ rows .append (raw_input ())
23
+ testcases .append (Grid (rows ))
24
+ return testcases
25
+
26
+ if __name__ == "__main__" :
27
+ testcases = getInput ()
28
+ for testcase in testcases :
29
+ if testcase .canBeSorted ():
30
+ print "YES"
31
+ else :
32
+ print "NO"
You can’t perform that action at this time.
0 commit comments