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.180 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.179 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.178 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.177 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.176 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.175 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.174 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.173 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.172 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.171 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.170 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.169 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.167 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.166 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.165 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.164 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.163 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.162 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.161 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.160 tracks/fsharp/exercises/spiral-matrix/Example.fs