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.159 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.158 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.157 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.156 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.155 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.154 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.153 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.152 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.151 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.150 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.149 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.148 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.147 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.146 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.145 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.144 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.143 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.142 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.141 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.140 tracks/fsharp/exercises/spiral-matrix/Example.fs