-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_encode_decode.rs
More file actions
44 lines (42 loc) · 968 Bytes
/
binary_encode_decode.rs
File metadata and controls
44 lines (42 loc) · 968 Bytes
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
use std::collections::HashMap;
fn code(s: &str) -> String {
let d = HashMap::from([
('0', "10"),
('1', "11"),
('2', "0110"),
('3', "0111"),
('4', "001100"),
('5', "001101"),
('6', "001110"),
('7', "001111"),
('8', "00011000"),
('9', "00011001"),
]);
s.chars().map(|k| d[&k]).collect()
}
fn decode(s: &str) -> String {
let d = HashMap::from([
("10", "0"),
("11", "1"),
("0110", "2"),
("0111", "3"),
("001100", "4"),
("001101", "5"),
("001110", "6"),
("001111", "7"),
("00011000", "8"),
("00011001", "9"),
]);
let mut r = String::new();
let mut t = s.to_string();
while t.len()>0 {
for (k,v) in d.iter() {
if t.starts_with(k){
r += v;
t = t[k.len()..].to_string();
break;
}
}
}
r
}