forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
snail.rs
148 lines (133 loc) · 3.68 KB
/
snail.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// ## Spiral Sorting
///
/// Given an n x m array, return the array elements arranged from outermost elements
/// to the middle element, traveling INWARD FROM TOP-LEFT, CLOCKWISE.
pub fn snail<T: Copy>(matrix: &[Vec<T>]) -> Vec<T> {
// break on empty matrix
if matrix.is_empty() || matrix[0].is_empty() {
return vec![];
}
let col_count = matrix[0].len();
let row_count = matrix.len();
// Initial maximum/minimum indices
let mut max_col = col_count - 1;
let mut min_col = 0;
let mut max_row = row_count - 1;
let mut min_row = 0;
// Initial direction is Right because
// we start from the top-left corner of the matrix at indices [0][0]
let mut dir = Direction::Right;
let mut row = 0;
let mut col = 0;
let mut result = Vec::new();
while result.len() < row_count * col_count {
result.push(matrix[row][col]);
dir.snail_move(
&mut col,
&mut row,
&mut min_col,
&mut max_col,
&mut min_row,
&mut max_row,
);
}
result
}
enum Direction {
Right,
Left,
Down,
Up,
}
impl Direction {
pub fn snail_move(
&mut self,
col: &mut usize,
row: &mut usize,
min_col: &mut usize,
max_col: &mut usize,
min_row: &mut usize,
max_row: &mut usize,
) {
match self {
Self::Right => {
*col = if *col < *max_col {
*col + 1
} else {
*self = Self::Down;
*min_row += 1;
*row = *min_row;
*col
};
}
Self::Down => {
*row = if *row < *max_row {
*row + 1
} else {
*self = Self::Left;
*max_col -= 1;
*col = *max_col;
*row
};
}
Self::Left => {
*col = if *col > usize::MIN && *col > *min_col {
*col - 1
} else {
*self = Self::Up;
*max_row -= 1;
*row = *max_row;
*col
};
}
Self::Up => {
*row = if *row > usize::MIN && *row > *min_row {
*row - 1
} else {
*self = Self::Right;
*min_col += 1;
*col = *min_col;
*row
};
}
};
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_empty() {
let empty: &[Vec<i32>] = &[vec![]];
assert_eq!(snail(&empty), vec![]);
}
#[test]
fn test_int() {
let square = &[vec![1, 2, 3], vec![4, 5, 6], vec![7, 8, 9]];
assert_eq!(snail(square), vec![1, 2, 3, 6, 9, 8, 7, 4, 5]);
}
#[test]
fn test_char() {
let square = &[
vec!['S', 'O', 'M'],
vec!['E', 'T', 'H'],
vec!['I', 'N', 'G'],
];
assert_eq!(
snail(square),
vec!['S', 'O', 'M', 'H', 'G', 'N', 'I', 'E', 'T']
);
}
#[test]
fn test_rect() {
let square = &[
vec!['H', 'E', 'L', 'L'],
vec!['O', ' ', 'W', 'O'],
vec!['R', 'L', 'D', ' '],
];
assert_eq!(
snail(square),
vec!['H', 'E', 'L', 'L', 'O', ' ', 'D', 'L', 'R', 'O', ' ', 'W']
);
}
}