Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ These are for demonstration purposes only.
- [x] [Manacher](./src/string/manacher.rs)
- [x] [Rabin Carp](./src/string/rabin_karp.rs)
- [x] [Reverse](./src/string/reverse.rs)
- [x] [Run Length Encoding](.src/string/run_length_encoding.rs)
- [x] [Hamming Distance](./src/string/hamming_distance.rs)

## [General](./src/general)
Expand Down
5 changes: 5 additions & 0 deletions src/string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ to find an exact match of a pattern string in a text.
### [Hamming Distance](./hamming_distance.rs)
From [Wikipedia][hamming-distance-wiki]: In information theory, the Hamming distance between two strings of equal length is the number of positions at which the corresponding symbols are different. In other words, it measures the minimum number of substitutions required to change one string into the other, or the minimum number of errors that could have transformed one string into the other. In a more general context, the Hamming distance is one of several string metrics for measuring the edit distance between two sequences. It is named after the American mathematician Richard Hamming.

[run-length-encoding-wiki]: https://en.wikipedia.org/wiki/Run-length_encoding

### [Run Length Encoding](./run_lentgh_encoding.rs)
From [Wikipedia][run-length-encoding-wiki]: a form of lossless data compression in which runs of data (sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run.

[hamming-distance-wiki]: https://en.wikipedia.org/wiki/Hamming_distance
2 changes: 2 additions & 0 deletions src/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod knuth_morris_pratt;
mod manacher;
mod rabin_karp;
mod reverse;
mod run_length_encoding;
mod z_algorithm;

pub use self::aho_corasick::AhoCorasick;
Expand All @@ -16,5 +17,6 @@ pub use self::knuth_morris_pratt::knuth_morris_pratt;
pub use self::manacher::manacher;
pub use self::rabin_karp::rabin_karp;
pub use self::reverse::reverse;
pub use self::run_length_encoding::{run_length_decoding, run_length_encoding};
pub use self::z_algorithm::match_pattern;
pub use self::z_algorithm::z_array;
138 changes: 138 additions & 0 deletions src/string/run_length_encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
pub fn run_length_encoding(target: String) -> 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.as_str().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: String) -> String {
if target.trim().is_empty() {
return "String is Empty!".to_string();
}

let mut character_count: String = String::new();
let mut encoded_target = String::new();

for c in target.as_str().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();
encoded_target.push_str(
&pop_char
.to_string()
.repeat(character_count.parse().unwrap()),
);
character_count = "".to_string();
}
}

encoded_target
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn encode_empty() {
assert_eq!(
(run_length_encoding("".to_string())),
"String is Empty!".to_string()
)
}

#[test]
fn encode_identical_character() {
assert_eq!(
(run_length_encoding("aaaaaaaaaa".to_string())),
"10a".to_string()
)
}
#[test]
fn encode_continuous_character() {
assert_eq!(
(run_length_encoding("abcdefghijk".to_string())),
"1a1b1c1d1e1f1g1h1i1j1k".to_string()
)
}

#[test]
fn encode_random_character() {
assert_eq!(
(run_length_encoding("aaaaabbbcccccdddddddddd".to_string())),
"5a3b5c10d".to_string()
)
}

#[test]
fn encode_long_character() {
assert_eq!(
(run_length_encoding(
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd".to_string()
)),
"200a3b5c10d".to_string()
)
}
#[test]
fn decode_empty() {
assert_eq!(
(run_length_decoding("".to_string())),
"String is Empty!".to_string()
)
}

#[test]
fn decode_identical_character() {
assert_eq!(
(run_length_decoding("10a".to_string())),
"aaaaaaaaaa".to_string()
)
}
#[test]
fn decode_continuous_character() {
assert_eq!(
(run_length_decoding("1a1b1c1d1e1f1g1h1i1j1k".to_string())),
"abcdefghijk".to_string()
)
}

#[test]
fn decode_random_character() {
assert_eq!(
(run_length_decoding("5a3b5c10d".to_string())),
"aaaaabbbcccccdddddddddd".to_string()
)
}

#[test]
fn decode_long_character() {
assert_eq!(
(run_length_decoding(
"200a3b5c10d".to_string()
)),
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbcccccdddddddddd".to_string()
)
}
}