Sha256: 8093aba240e77432d3ba95e8e215648412bba81c31b32ae18842bb221cb56ac5

Contents?: true

Size: 897 Bytes

Versions: 35

Compression:

Stored size: 897 Bytes

Contents

pub fn spiral_matrix(size: usize) -> Vec<Vec<usize>> {
    let mut matrix: Vec<Vec<usize>> = vec![vec![0; size]; size];
    let num_concentric_squares: usize = (size as f64 / 2.0).ceil() as usize;
    let mut counter: usize = 1;
    let mut sidelen = size;
    for i in 0..num_concentric_squares {

        for j in 0..sidelen {
            matrix[i][i + j] = counter;
            counter += 1;
        }

        for j in 1..sidelen {
            matrix[i + j][size - 1 - i] = counter;
            counter += 1;
        }

        for j in (0..sidelen - 1).rev() {
            matrix[size - 1 - i][i + j] = counter;
            counter += 1;
        }

        for j in (1..sidelen - 1).rev() {
            matrix[i + j][i] = counter;

            counter += 1;
        }
        if sidelen >= 2 {
            sidelen -= 2;
        } else {
            sidelen -= 1;
        }
    }
    matrix
}

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
trackler-2.2.1.158 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.157 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.156 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.155 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.154 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.153 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.152 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.151 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.150 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.149 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.148 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.147 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.146 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.145 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.144 tracks/rust/exercises/spiral-matrix/example.rs