-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlib.rs
35 lines (34 loc) · 863 Bytes
/
lib.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
pub fn encode_direct<T: Copy + PartialEq>(li: &Vec<T>) -> Vec<(usize, T)> {
if li.is_empty() {
vec![]
} else {
let mut res = vec![];
let mut i = 0usize;
while i < li.len() {
let mut j = i;
while j < li.len() {
j += 1;
if j == li.len() || li[i] != li[j] {
res.push((j - i, li[i]));
i = j;
break;
}
}
}
res
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encode_direct() {
let li = vec![
'a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e',
];
assert_eq!(
encode_direct(&li),
vec![(4, 'a'), (1, 'b'), (2, 'c'), (2, 'a'), (1, 'd'), (4, 'e')]
);
}
}