-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathrun_length_encoding.rs
74 lines (62 loc) · 1.79 KB
/
run_length_encoding.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
// https://en.wikipedia.org/wiki/Run-length_encoding
pub fn run_length_encode(text: &str) -> Vec<(char, i32)> {
let mut count = 1;
let mut encoded: Vec<(char, i32)> = vec![];
for (i, c) in text.chars().enumerate() {
if i + 1 < text.len() && c == text.chars().nth(i + 1).unwrap() {
count += 1;
} else {
encoded.push((c, count));
count = 1;
}
}
encoded
}
pub fn run_length_decode(encoded: &[(char, i32)]) -> String {
let res = encoded
.iter()
.map(|x| (x.0).to_string().repeat(x.1 as usize))
.collect::<String>();
res
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_run_length_decode() {
let res = run_length_decode(&[('A', 0)]);
assert_eq!(res, "");
let res = run_length_decode(&[('B', 1)]);
assert_eq!(res, "B");
let res = run_length_decode(&[('A', 5), ('z', 3), ('B', 1)]);
assert_eq!(res, "AAAAAzzzB");
}
#[test]
fn test_run_length_encode() {
let res = run_length_encode("");
assert_eq!(res, []);
let res = run_length_encode("A");
assert_eq!(res, [('A', 1)]);
let res = run_length_encode("AA");
assert_eq!(res, [('A', 2)]);
let res = run_length_encode("AAAABBBCCDAA");
assert_eq!(res, [('A', 4), ('B', 3), ('C', 2), ('D', 1), ('A', 2)]);
let res = run_length_encode("Rust-Trends");
assert_eq!(
res,
[
('R', 1),
('u', 1),
('s', 1),
('t', 1),
('-', 1),
('T', 1),
('r', 1),
('e', 1),
('n', 1),
('d', 1),
('s', 1)
]
);
}
}