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.179 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.178 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.177 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.176 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.175 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.174 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.173 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.172 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.171 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.170 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.169 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.167 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.166 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.165 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.164 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.163 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.162 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.161 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.160 tracks/rust/exercises/spiral-matrix/example.rs
trackler-2.2.1.159 tracks/rust/exercises/spiral-matrix/example.rs