Sha256: 8c4c6b6a1feba6b50c7f3f714c9ee55425c9c6a2a304a872e63f9e0e7c03ee30

Contents?: true

Size: 1.07 KB

Versions: 110

Compression:

Stored size: 1.07 KB

Contents

module SpiralMatrix
 
let array2DToList array = 
  [ for x in Array2D.base1 array .. Array2D.length1 array - 1 ->
    [ for y in Array2D.base2 array .. Array2D.length2 array - 1 -> array.[x, y] ] ]  

let spiralMatrix size = 
    let numbersToPlace = size * size

    let mutable spiral = Array2D.create size size 0
    let mutable currentSpiralValue = 1
    let mutable firstPivot = 0
    let mutable secondPivot = size - 1

    let setValue x y = 
        spiral.[x, y] <- currentSpiralValue
        currentSpiralValue <- currentSpiralValue + 1

    while currentSpiralValue <= numbersToPlace do
        [firstPivot .. secondPivot] 
        |> List.iter (fun i -> setValue firstPivot i)

        [firstPivot + 1.. secondPivot] 
        |> List.iter (fun i -> setValue i secondPivot)

        [secondPivot - 1 .. -1 .. firstPivot] 
        |> List.iter (fun i -> setValue secondPivot i)

        [secondPivot - 1 .. -1 .. firstPivot + 1] 
        |> List.iter (fun i -> setValue i firstPivot)

        firstPivot <- firstPivot + 1
        secondPivot <- secondPivot - 1

    array2DToList spiral

Version data entries

110 entries across 110 versions & 1 rubygems

Version Path
trackler-2.2.1.78 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.77 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.76 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.75 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.74 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.73 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.72 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.71 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.70 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.69 tracks/fsharp/exercises/spiral-matrix/Example.fs