Last active
April 7, 2017 01:52
-
-
Save jeffreybergier/25b445e5f2cc685ee99f32bb3720aab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Direction { | |
case right, left, up, down | |
} | |
func traverseClockwise(_ input: [[String]]) -> [String] { | |
// used to calculate how long the output needs to be | |
let countY = input.count | |
let countX = input.first?.count ?? 0 | |
// bail if either of the arrays are empty | |
guard countY > 0 else { return [] } | |
guard countX > 0 else { return [] } | |
// preconditions | |
for row in input { | |
precondition(row.count == countX, "Every row in the matrix must be the same length") | |
} | |
// used to stop the 'cursors' from reading values already read | |
var maxY = countY - 1 | |
var maxX = countX - 1 | |
var minY = 0 | |
var minX = 0 | |
// 'cursors' that read values out of the matrix | |
var currentY = 0 | |
var currentX = 0 | |
var currentDirection = Direction.right | |
// final output. Could also be a plain string that gets appended to | |
var output = [String]() | |
for _ in 0 ..< countY*countX { | |
let value = input[currentY][currentX] | |
output += [value] | |
switch currentDirection { | |
case .right: | |
currentX += currentX < maxX ? 1 : 0 // prevents going out of bounds on an 1xn matrix | |
if currentX == maxX { | |
currentDirection = .down | |
} | |
case .down: | |
currentY += 1 | |
if currentY == maxY { | |
currentDirection = .left | |
} | |
case .left: | |
currentX -= 1 | |
if currentX == minX { | |
currentDirection = .up | |
maxX -= 1 | |
minY += 1 | |
} | |
case .up: | |
currentY -= 1 | |
if currentY == minY { | |
currentDirection = .right | |
maxY -= 1 | |
minX += 1 | |
} | |
} | |
} | |
return output | |
} | |
let balancedMatrix1: [[String]] = [ | |
["A", "B", "C", "D"], | |
["L", "M", "N", "E"], | |
["K", "P", "O", "F"], | |
["J", "I", "H", "G"] | |
] | |
let balancedMatrix1T = traverseClockwise(balancedMatrix1) | |
print(balancedMatrix1T) | |
//["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"] | |
let balancedMatrix2: [[String]] = [ | |
["A", "B", "C", "D", "E"], | |
["P", "Q", "R", "S", "F"], | |
["O", "X", "Y", "T", "G"], | |
["N", "W", "V", "U", "H"], | |
["M", "L", "K", "J", "I"] | |
] | |
let balancedMatrix2T = traverseClockwise(balancedMatrix2) | |
print(balancedMatrix2T) | |
//["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y"] | |
let balancedMatrix3: [[String]] = [ | |
["A", "B", "C", "D", "E", "F"], | |
["T", "U", "V", "W", "X", "G"], | |
["S", "6", "7", "8", "Y", "H"], | |
["R", "5", "0", "9", "Z", "I"], | |
["Q", "4", "3", "2", "1", "J"], | |
["P", "O", "N", "M", "L", "K"] | |
] | |
let balancedMatrix3T = traverseClockwise(balancedMatrix3) | |
print(balancedMatrix3T) | |
//["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] | |
let unbalancedMatrix1: [[String]] = [ | |
["A", "B", "C", "D", "E", "F"], | |
["R", "S", "T", "U", "V", "G"], | |
["Q", "1", "2", "3", "W", "H"], | |
["P", "0", "Z", "Y", "X", "I"], | |
["O", "N", "M", "L", "K", "J"], | |
] | |
let unbalancedMatrix1T = traverseClockwise(unbalancedMatrix1) | |
print(unbalancedMatrix1T) | |
//["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3"] | |
let empty1: [[String]] = [] | |
let emptyT1 = traverseClockwise(empty1) | |
print(emptyT1) | |
//[] | |
let empty2: [[String]] = [[]] | |
let emptyT2 = traverseClockwise(empty2) | |
print(emptyT2) | |
//[] | |
let empty3: [[String]] = [[],[],[],[]] | |
let emptyT3 = traverseClockwise(empty3) | |
print(emptyT3) | |
//[] | |
let notAMatrix1: [[String]] = [["1"],["2"],["3"],["4"]] | |
let notAMatrix1T = traverseClockwise(notAMatrix1) | |
print(notAMatrix1T) | |
//["1", "1", "2", "3"] | |
let notAMatrix2: [[String]] = [["A", "B", "C"], ["1", "2", "3"]] | |
let notAMatrix2T = traverseClockwise(notAMatrix2) | |
print(notAMatrix2T) | |
//["A", "B", "C", "3", "2", "1"] | |
let preconditionCheck: [[String]] = [ | |
["A", "B", "C", "D"], | |
["L", "M", "N", "E"], | |
["K", "P", "O", "F"], | |
["J", "I", "H", "G", "CRASH"] | |
] | |
//let preconditionCheckT = traverseClockwise(preconditionCheck) | |
//print(preconditionCheckT) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment