Skip to content

Commit 7891f10

Browse files
authored
Include unreachable mods (TheAlgorithms#594)
1 parent 30ef8e0 commit 7891f10

26 files changed

+126
-83
lines changed

.github/pull_request_template.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Please delete options that are not relevant.
2020
- [ ] I ran `cargo clippy --all -- -D warnings` just before my last commit and fixed any issue that was found.
2121
- [ ] I ran `cargo fmt` just before my last commit.
2222
- [ ] I ran `cargo test` just before my last commit and all tests passed.
23+
- [ ] I added my algorithm to the corresponding `mod.rs` file within its own folder, and in any parent folder(s).
2324
- [ ] I added my algorithm to `DIRECTORY.md` with the correct link.
2425
- [ ] I checked `COUNTRIBUTING.md` and my code follows its guidelines.
2526

src/ciphers/baconian_cipher.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
// Bacon's cipher or the Baconian cipher is a method of steganographic message encoding devised by Francis Bacon in 1605.
55
// A message is concealed in the presentation of text, rather than its content. Bacon cipher is categorized as both a substitution cipher (in plain code) and a concealment cipher (using the two typefaces).
66

7-
87
// Encode Baconian Cipher
9-
fn baconian_encode(message: &str) -> String {
8+
pub fn baconian_encode(message: &str) -> String {
109
let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1110
let baconian = [
1211
"AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA", "AABAB", "AABBA", "AABBB", "ABAAA", "ABAAB",
@@ -18,17 +17,16 @@ fn baconian_encode(message: &str) -> String {
1817
.chars()
1918
.map(|c| {
2019
if let Some(index) = alphabet.find(c.to_ascii_uppercase()) {
21-
baconian[index as usize].to_string()
20+
baconian[index].to_string()
2221
} else {
2322
c.to_string()
2423
}
2524
})
2625
.collect()
2726
}
2827

29-
3028
// Decode Baconian Cipher
31-
fn baconian_decode(encoded: &str) -> String {
29+
pub fn baconian_decode(encoded: &str) -> String {
3230
let baconian = [
3331
"AAAAA", "AAAAB", "AAABA", "AAABB", "AABAA", "AABAB", "AABBA", "AABBB", "ABAAA", "ABAAB",
3432
"ABABA", "ABABB", "ABBAA", "ABBAB", "ABBBA", "ABBBB", "BAAAA", "BAAAB", "BAABA", "BAABB",
@@ -40,7 +38,10 @@ fn baconian_decode(encoded: &str) -> String {
4038
.as_bytes()
4139
.chunks(5)
4240
.map(|chunk| {
43-
if let Some(index) = baconian.iter().position(|&x| x == String::from_utf8_lossy(chunk)) {
41+
if let Some(index) = baconian
42+
.iter()
43+
.position(|&x| x == String::from_utf8_lossy(chunk))
44+
{
4445
alphabet.chars().nth(index).unwrap()
4546
} else {
4647
' '

src/ciphers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod aes;
22
mod another_rot13;
3+
mod baconian_cipher;
34
mod base64;
45
mod blake2b;
56
mod caesar;
@@ -21,6 +22,7 @@ mod vigenere;
2122
mod xor;
2223
pub use self::aes::{aes_decrypt, aes_encrypt, AesKey};
2324
pub use self::another_rot13::another_rot13;
25+
pub use self::baconian_cipher::{baconian_decode, baconian_encode};
2426
pub use self::base64::{base64_decode, base64_encode};
2527
pub use self::blake2b::blake2b;
2628
pub use self::caesar::caesar;

src/conversions/binary_to_hexadecimal.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static BITS_TO_HEX: &[(u8, &str)] = &[
2222
(0b1111, "f"),
2323
];
2424

25-
fn bin_to_hexadecimal(binary_str: &str) -> String {
25+
pub fn binary_to_hexadecimal(binary_str: &str) -> String {
2626
let binary_str = binary_str.trim();
2727

2828
if binary_str.is_empty() {
@@ -41,7 +41,11 @@ fn bin_to_hexadecimal(binary_str: &str) -> String {
4141
}
4242

4343
let padded_len = (4 - (binary_str.len() % 4)) % 4;
44-
let binary_str = format!("{:0width$}", binary_str, width = binary_str.len() + padded_len);
44+
let binary_str = format!(
45+
"{:0width$}",
46+
binary_str,
47+
width = binary_str.len() + padded_len
48+
);
4549

4650
// Convert binary to hexadecimal
4751
let mut hexadecimal = String::with_capacity(binary_str.len() / 4 + 2);
@@ -50,7 +54,7 @@ fn bin_to_hexadecimal(binary_str: &str) -> String {
5054
for chunk in binary_str.as_bytes().chunks(4) {
5155
let mut nibble = 0;
5256
for (i, &byte) in chunk.iter().enumerate() {
53-
nibble |= ((byte - b'0') as u8) << (3 - i);
57+
nibble |= (byte - b'0') << (3 - i);
5458
}
5559

5660
let hex_char = BITS_TO_HEX
@@ -76,27 +80,27 @@ mod tests {
7680
fn test_empty_string() {
7781
let input = "";
7882
let expected = "Invalid Input";
79-
assert_eq!(bin_to_hexadecimal(input), expected);
83+
assert_eq!(binary_to_hexadecimal(input), expected);
8084
}
8185

8286
#[test]
8387
fn test_invalid_binary() {
8488
let input = "a";
8589
let expected = "Invalid Input";
86-
assert_eq!(bin_to_hexadecimal(input), expected);
90+
assert_eq!(binary_to_hexadecimal(input), expected);
8791
}
8892

8993
#[test]
9094
fn test_binary() {
9195
let input = "00110110";
9296
let expected = "0x36";
93-
assert_eq!(bin_to_hexadecimal(input), expected);
97+
assert_eq!(binary_to_hexadecimal(input), expected);
9498
}
9599

96100
#[test]
97101
fn test_padded_binary() {
98102
let input = " 1010 ";
99103
let expected = "0xa";
100-
assert_eq!(bin_to_hexadecimal(input), expected);
104+
assert_eq!(binary_to_hexadecimal(input), expected);
101105
}
102106
}

src/conversions/hexadecimal_to_binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// 2. https://en.wikipedia.org/wiki/Binary_number
55
// Other References for Testing : https://www.rapidtables.com/convert/number/hex-to-binary.html
66

7-
fn hexadecimal_to_binary(hex_str: &str) -> Result<String, String> {
7+
pub fn hexadecimal_to_binary(hex_str: &str) -> Result<String, String> {
88
let hex_chars = hex_str.chars().collect::<Vec<char>>();
99
let mut binary = String::new();
1010

src/conversions/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
mod binary_to_decimal;
2+
mod binary_to_hexadecimal;
23
mod decimal_to_binary;
4+
mod hexadecimal_to_binary;
5+
mod octal_to_binary;
36
pub use self::binary_to_decimal::binary_to_decimal;
7+
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
48
pub use self::decimal_to_binary::decimal_to_binary;
9+
pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
10+
pub use self::octal_to_binary::octal_to_binary;

src/conversions/octal_to_binary.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
// Wikipedia References : 1. https://en.wikipedia.org/wiki/Octal
44
// 2. https://en.wikipedia.org/wiki/Binary_number
55

6-
fn octal_to_binary(octal_str: &str) -> Result<String, &'static str> {
6+
pub fn octal_to_binary(octal_str: &str) -> Result<String, &'static str> {
77
let octal_str = octal_str.trim();
88

99
if octal_str.is_empty() {
1010
return Err("Empty");
1111
}
1212

13-
if !octal_str.chars().all(|c| c >= '0' && c <= '7') {
13+
if !octal_str.chars().all(|c| ('0'..'7').contains(&c)) {
1414
return Err("Non-octal Value");
1515
}
1616

@@ -54,8 +54,7 @@ mod tests {
5454
#[test]
5555
fn test_valid_octal() {
5656
let input = "123";
57-
let expected = Ok("001010011".to_string());
57+
let expected = Ok("001010011".to_string());
5858
assert_eq!(octal_to_binary(input), expected);
5959
}
60-
6160
}

src/data_structures/floyds_algorithm.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,9 @@ pub fn has_cycle<T>(linked_list: &LinkedList<T>) -> bool {
4949
}
5050

5151
if slow == fast {
52-
5352
return true; // Cycle detected
5453
}
5554
}
56-
5755
}
5856
// println!("{}", flag);
5957
false // No cycle detected
@@ -70,7 +68,7 @@ mod tests {
7068
linked_list.insert_at_tail(2);
7169
linked_list.insert_at_tail(3);
7270

73-
assert_eq!(has_cycle(&linked_list), false);
71+
assert!(!has_cycle(&linked_list));
7472

7573
assert_eq!(detect_cycle(&mut linked_list), None);
7674
}
@@ -91,7 +89,7 @@ mod tests {
9189
}
9290
}
9391

94-
assert_eq!(has_cycle(&linked_list), true);
92+
assert!(has_cycle(&linked_list));
9593
assert_eq!(detect_cycle(&mut linked_list), Some(3));
9694
}
9795
}

src/data_structures/infix_to_postfix.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Function to convert infix expression to postfix expression
2-
fn infix_to_postfix(infix: &str) -> String {
2+
pub fn infix_to_postfix(infix: &str) -> String {
33
let mut postfix = String::new();
44
let mut stack: Vec<char> = Vec::new();
55

@@ -53,17 +53,13 @@ fn infix_to_postfix(infix: &str) -> String {
5353
postfix
5454
}
5555

56-
5756
#[cfg(test)]
5857
mod tests {
5958
use super::*;
6059

6160
#[test]
6261
fn test_infix_to_postfix() {
63-
assert_eq!(
64-
infix_to_postfix("a-b+c-d*e"),
65-
"ab-c+de*-".to_string()
66-
);
62+
assert_eq!(infix_to_postfix("a-b+c-d*e"), "ab-c+de*-".to_string());
6763
assert_eq!(
6864
infix_to_postfix("a*(b+c)+d/(e+f)"),
6965
"abc+*def+/+".to_string()

src/data_structures/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ mod avl_tree;
22
mod b_tree;
33
mod binary_search_tree;
44
mod fenwick_tree;
5+
mod floyds_algorithm;
56
mod graph;
67
mod heap;
8+
mod infix_to_postfix;
79
mod lazy_segment_tree;
810
mod linked_list;
11+
mod postfix_evaluation;
912
mod probabilistic;
1013
mod queue;
1114
mod rb_tree;
@@ -21,11 +24,14 @@ pub use self::avl_tree::AVLTree;
2124
pub use self::b_tree::BTree;
2225
pub use self::binary_search_tree::BinarySearchTree;
2326
pub use self::fenwick_tree::FenwickTree;
27+
pub use self::floyds_algorithm::{detect_cycle, has_cycle};
2428
pub use self::graph::DirectedGraph;
2529
pub use self::graph::UndirectedGraph;
2630
pub use self::heap::Heap;
31+
pub use self::infix_to_postfix::infix_to_postfix;
2732
pub use self::lazy_segment_tree::LazySegmentTree;
2833
pub use self::linked_list::LinkedList;
34+
pub use self::postfix_evaluation::evaluate_postfix;
2935
pub use self::probabilistic::bloom_filter;
3036
pub use self::probabilistic::count_min_sketch;
3137
pub use self::queue::Queue;

0 commit comments

Comments
 (0)