-
Notifications
You must be signed in to change notification settings - Fork 3
/
Grid Challenge.swift
45 lines (34 loc) · 1.26 KB
/
Grid Challenge.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import Foundation
/*
* Complete the 'gridChallenge' function below.
*
* The function is expected to return a STRING.
* The function accepts STRING_ARRAY grid as parameter.
*/
func gridChallenge(grid: [String]) -> String {
let rowSortedGrid = grid.map{ $0.sorted() }
for index in 0..<rowSortedGrid.count {
if rowSortedGrid.map({ $0[index] }) != rowSortedGrid.map({ $0[index] }).sorted() {
return "NO"
}
}
return "YES"
}
let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!
guard let t = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
for tItr in 1...t {
guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }
var grid = [String]()
for _ in 1...n {
guard let gridItem = readLine() else { fatalError("Bad input") }
grid.append(gridItem)
}
guard grid.count == n else { fatalError("Bad input") }
let result = gridChallenge(grid: grid)
fileHandle.write(result.data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)
}