Skip to content

Instantly share code, notes, and snippets.

@acegreen
Last active July 11, 2018 19:26
Show Gist options
  • Select an option

  • Save acegreen/e16a2259a93dab880a7f to your computer and use it in GitHub Desktop.

Select an option

Save acegreen/e16a2259a93dab880a7f to your computer and use it in GitHub Desktop.

Revisions

  1. acegreen renamed this gist Sep 20, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. acegreen revised this gist Mar 9, 2016. 1 changed file with 0 additions and 17 deletions.
    17 changes: 0 additions & 17 deletions Groupon Interview Question 2
    Original file line number Diff line number Diff line change
    @@ -16,23 +16,6 @@ extension Array {
    }
    }

    extension String {

    subscript (i: Int) -> Character {
    return self[self.startIndex.advancedBy(i)]
    }

    subscript (i: Int) -> String {
    return String(self[i] as Character)
    }

    subscript (r: Range<Int>) -> String {
    let start = startIndex.advancedBy(r.startIndex)
    let end = start.advancedBy(r.endIndex - r.startIndex)
    return self[Range(start: start, end: end)]
    }
    }

    // Groupon Interview Q2

    /*
  3. acegreen revised this gist Mar 9, 2016. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions Groupon Interview Question 2
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    import UIKit
    import Foundation
    import XCPlayground
    import SwiftyJSON

    extension Array {

  4. acegreen revised this gist Mar 9, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Groupon Interview Question 2
    Original file line number Diff line number Diff line change
    @@ -199,3 +199,6 @@ func game (array: [[Int]]) -> [[Int]] {

    print(game(gameGrid))

    /*
    Output = [[0, 1, 0], [0, 0, 0], [0, 1, 0]]
    */
  5. acegreen created this gist Mar 9, 2016.
    201 changes: 201 additions & 0 deletions Groupon Interview Question 2
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,201 @@
    import UIKit
    import Foundation
    import XCPlayground
    import SwiftyJSON

    extension Array {

    func get(index: Int) -> Element? {
    if 0 <= index && index < count {
    return self[index]
    } else {
    return nil
    }
    }

    mutating func moveItem(fromIndex oldIndex: Index, toIndex newIndex: Index) {
    insert(removeAtIndex(oldIndex), atIndex: newIndex)
    }
    }

    extension String {

    subscript (i: Int) -> Character {
    return self[self.startIndex.advancedBy(i)]
    }

    subscript (i: Int) -> String {
    return String(self[i] as Character)
    }

    subscript (r: Range<Int>) -> String {
    let start = startIndex.advancedBy(r.startIndex)
    let end = start.advancedBy(r.endIndex - r.startIndex)
    return self[Range(start: start, end: end)]
    }
    }

    // Groupon Interview Q2

    /*

    1 1 1 0,0 0,1 0,2
    0 0 0 1,0 1,1 1,2
    1 1 1 2,0 2,1 2,2

    0 1 0
    0 0 0
    0 1 0


    1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
    2. Any live cell with two or three live neighbours lives on to the next generation.
    3. Any live cell with more than three live neighbours dies, as if by over-population.
    4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

    */

    var gameGrid = [[1,1,1], [0,0,0], [1,1,1]]

    func game (array: [[Int]]) -> [[Int]] {

    let arrayRowsIndex = array.count - 1
    let arrayColumnIndex = array[0].count - 1
    var output = array

    var liveNeighbours: Int = 0
    var deadNeighbours: Int = 0

    for (rowIndex, rowElement) in output.enumerate() {

    for (colIndex, colElement) in rowElement.enumerate() {

    print(rowIndex, colIndex)
    print(array[rowIndex][colIndex])

    // above of element
    if (colIndex - 1) >= 0 {

    switch array[rowIndex][colIndex - 1] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // below of element
    if (colIndex + 1) <= arrayColumnIndex {

    switch array[rowIndex][colIndex + 1] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // left of element
    if (rowIndex - 1) >= 0 {

    switch array[rowIndex - 1][colIndex] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // right of element
    if (rowIndex + 1) <= arrayRowsIndex {

    switch array[rowIndex + 1][colIndex] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // below + right of element
    if (rowIndex + 1) <= arrayRowsIndex && (colIndex + 1) <= arrayColumnIndex {

    switch array[rowIndex + 1][colIndex + 1] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // above + left of element
    if (rowIndex - 1) >= 0 && (colIndex - 1) >= 0 {

    switch array[rowIndex - 1][colIndex - 1] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // below + left of element
    if (rowIndex + 1) <= arrayRowsIndex && (colIndex - 1) >= 0 {

    switch array[rowIndex + 1][colIndex - 1] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    // above + right of element
    if (rowIndex - 1) >= 0 && (colIndex + 1) <= arrayColumnIndex {

    switch array[rowIndex - 1][colIndex + 1] {
    case 1:
    liveNeighbours += 1
    default:
    deadNeighbours += 1
    }
    }

    print("liveNeighbours", liveNeighbours)
    print("deadNeighbours", deadNeighbours)

    switch (liveNeighbours, deadNeighbours) {

    case (let liveNeighbours, let deadNeighbours) where liveNeighbours < 2:
    // fewer than 2 live neighbours dies
    output[rowIndex][colIndex] = 0

    case (let liveNeighbours, let deadNeighbours) where liveNeighbours >= 2:

    // 2 or more neighbours lives on
    break

    case (let liveNeighbours, let deadNeighbours) where liveNeighbours > 3:

    // with more than 3 live neighbours dies
    output[rowIndex][colIndex] = 0

    case (let liveNeighbours, let deadNeighbours) where liveNeighbours == 3:

    // exactly 3 live neighbours comes back to life
    output[rowIndex][colIndex] = 1

    default:
    break
    }

    liveNeighbours = 0
    deadNeighbours = 0
    }
    }

    return output
    }

    print(game(gameGrid))