forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_length_encoding.rs
More file actions
115 lines (100 loc) · 3.27 KB
/
Copy pathrun_length_encoding.rs
File metadata and controls
115 lines (100 loc) · 3.27 KB
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
pub fn run_length_encoding(target: &str) -> String {
if target.trim().is_empty() {
return "String is Empty!".to_string();
}
let mut count: i32 = 0;
let mut base_character: String = "".to_string();
let mut encoded_target = String::new();
for c in target.chars() {
if base_character == *"" {
base_character = c.to_string();
}
if c.to_string() == base_character {
count += 1;
} else {
encoded_target.push_str(&count.to_string());
count = 1;
encoded_target.push_str(&base_character);
base_character = c.to_string();
}
}
encoded_target.push_str(&count.to_string());
encoded_target.push_str(&base_character);
encoded_target
}
pub fn run_length_decoding(target: &str) -> String {
if target.trim().is_empty() {
return "String is Empty!".to_string();
}
let mut character_count: String = String::new();
let mut decoded_target = String::new();
for c in target.chars() {
character_count.push(c);
let is_numeric: bool = character_count.parse::<i32>().is_ok();
if !is_numeric {
let pop_char: char = character_count.pop().unwrap();
decoded_target.push_str(
&pop_char
.to_string()
.repeat(character_count.parse().unwrap()),
);
character_count = "".to_string();
}
}
decoded_target
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_empty() {
assert_eq!(run_length_encoding(""), "String is Empty!")
}
#[test]
fn encode_identical_character() {
assert_eq!(run_length_encoding("aaaaaaaaaa"), "10a")
}
#[test]
fn encode_continuous_character() {
assert_eq!(run_length_encoding("abcdefghijk"), "1a1b1c1d1e1f1g1h1i1j1k")
}
#[test]
fn encode_random_character() {
assert_eq!(run_length_encoding("aaaaabbbcccccdddddddddd"), "5a3b5c10d")
}
#[test]
fn encode_long_character() {
assert_eq!(
run_length_encoding(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd"
),
"200a3b5c10d"
)
}
#[test]
fn decode_empty() {
assert_eq!(run_length_decoding(""), "String is Empty!")
}
#[test]
fn decode_identical_character() {
assert_eq!(run_length_decoding("10a"), "aaaaaaaaaa")
}
#[test]
fn decode_continuous_character() {
assert_eq!(run_length_decoding("1a1b1c1d1e1f1g1h1i1j1k"), "abcdefghijk")
}
#[test]
fn decode_random_character() {
assert_eq!(
run_length_decoding("5a3b5c10d").to_string(),
"aaaaabbbcccccdddddddddd"
)
}
#[test]
fn decode_long_character() {
assert_eq!(
run_length_decoding("200a3b5c10d"),
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd"
)
}
}