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.98 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.97 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.96 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.95 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.94 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.93 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.92 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.91 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.90 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.89 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.88 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.87 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.86 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.85 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.84 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.83 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.82 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.81 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.80 tracks/fsharp/exercises/spiral-matrix/Example.fs
trackler-2.2.1.79 tracks/fsharp/exercises/spiral-matrix/Example.fs