Skip to content

Instantly share code, notes, and snippets.

@dautermann
Created October 24, 2016 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dautermann/c2c39f5532490293030c04f0961cb20d to your computer and use it in GitHub Desktop.
Save dautermann/c2c39f5532490293030c04f0961cb20d to your computer and use it in GitHub Desktop.
Write a Swift playground that takes an n x n grid of integers. Each integer can be either 1 or 0. The playground then outputs an n x n grid where each block indicates the number of 1's around that block.
// Copyright © 2016 Envoy. All rights reserved.
/*
Write a Swift playground that takes an n x n grid of integers. Each integer can be either 1 or 0.
The playground then outputs an n x n grid where each block indicates the number of 1's around that block, (excluding the block itself) . For Block 0 on row 0, surrounding blocks are (0,1) (1,0) and (1,1). Similary for block (1,1) all the blocks around it are to be counted as surrounding blocks.
Requirements:
Make sure your solution works for any size grid.
Spend an hour and a half coding the logic and another hour cleaning your code (adding comments, cleaning variable and function names).
Optimize your functions to be O(n^2).
Your output lines should not have any trailing or leading whitespaces.
Please use hard coded example input constants below.
Examples:
Input Grid:
let sampleGrid = [[0,1,0], [0,0,0], [1,0,0]]
input:
0 1 0
0 0 0
1 0 0
Console Output:
1 0 1
2 2 1
0 1 0
/*
-1 +1 0 +1 +1 +1
-1 0 0 0 +1 0
-1 -1 0 -1 +1 -1
*/
/////////////////
Input Grid:
let sampleGrid = [[0,1,0,0], [0,0,0,1], [1,0,0,1],[0,1,0,1]]
Console Output:
1 0 2 1
2 2 3 1
1 2 4 2
2 1 3 1
*/
// each row is a row of Y
// and each column is a row of X
//
let sampleGrid = [[0,1,0,0], [0,0,0,1], [1,0,0,1],[0,1,0,1]]
//let sampleGrid = [[0,1,0], [0,0,0], [1,0,0]]
//let sampleGrid = [[0,0,0,0,0,0], [0,0,1,0,0,0], [0,0,0,1,0,0], [0,0,1,0,0,0], [0,0,0,0,0,0], [0,0,0,0,1,0]]
// this would probably be a great place for a rotation transform
// but I simply don't have the time (if I'm going to do this in 90 minutes)
var outputGrid : [[Int]] = []
var xCount : Int
var yCount = 0
let nCount = sampleGrid.count
print("Input Grid:\n")
for eachEntry in sampleGrid {
xCount = 0
for eachElement in eachEntry {
xCount=xCount+1
if(xCount >= nCount) {
print("\(sampleGrid[yCount][xCount-1])")
} else {
// blank terminator means we don't append a new line like
// the print up above...
print("\(sampleGrid[yCount][xCount-1]) ", terminator:"")
}
}
yCount = yCount+1
}
/* keep in mind the sampleGrid is effectively rotated, as each array entry in the overall array could be considered a row of Y
and each entry in each Y row is a column of X
so when we see:
XXXXX
Y0 1 0
Y0 0 0
Y1 0 0
in memory it's actually
YYYYY
X0 0 1
X1 0 0
X0 0 0
*/
print("\nConsole Output:\n")
yCount = 0
for eachArray in sampleGrid {
xCount = 0
/* Swift 2
var outputGrid = [[Int]](count:nCount, repeatedValue:[Int](count: nCount, repeatedValue:0))
*/
/* Swift 3! */
var outputGrid = [[Int]](repeating: [Int](repeating: 0, count: nCount), count: nCount)
for eachElement in eachArray {
let topLeftElement = (xCount - 1 < 0) ? 0 : (yCount - 1 < 0) ? 0 : sampleGrid[yCount-1][xCount-1]
let leftElement = (xCount - 1 < 0 ) ? 0 : sampleGrid[yCount][xCount - 1]
let bottomLeftElement = (xCount - 1 < 0 ) ? 0 : (yCount+1 >= nCount) ? 0 : sampleGrid[yCount+1][xCount-1]
let bottomElement = (yCount+1 >= nCount) ? 0 : sampleGrid[yCount+1][xCount]
let bottomRightElement = (xCount+1 >= nCount) ? 0 : (yCount+1 >= nCount) ? 0 : sampleGrid[yCount+1][xCount+1]
let rightElement = (xCount+1 >= nCount ) ? 0 : sampleGrid[yCount][xCount+1]
let topRightElement = (xCount+1 >= nCount) ? 0 : (yCount - 1 < 0) ? 0 : sampleGrid[yCount-1][xCount+1]
let topElement = (yCount - 1 < 0) ? 0 : sampleGrid[yCount-1][xCount]
var total = topLeftElement + leftElement + bottomLeftElement + bottomElement + bottomRightElement + rightElement + topRightElement + topElement
// thankfully the outputGrid isn't rotated like this so we can use normal X & Y coordinates instead of flipped
// coordinates... if we wanted to create an array like the input array, we'd have to enumerate X across each row of Y
outputGrid[xCount][yCount] = total
xCount = xCount+1
if(xCount >= nCount) {
print("\(outputGrid[xCount-1][yCount])")
} else {
print("\(outputGrid[xCount-1][yCount]) ", terminator:"")
}
}
yCount = yCount+1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment