diff --git a/DIRECTORY.md b/DIRECTORY.md index ee05a9a1f44..fddaca51318 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -40,11 +40,12 @@ * Compression * [Run Length Encoding](https://github.com/TheAlgorithms/Rust/blob/master/src/compression/run_length_encoding.rs) * Conversions - * [Binary To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs) - * [Binary To Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs) - * [Decimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs) - * [Hexadecimal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs) - * [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs) + * [Binary to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_decimal.rs) + * [Binary to Hexadecimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/binary_to_hexadecimal.rs) + * [Decimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/decimal_to_binary.rs) + * [Hexadecimal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_binary.rs) + * [Octal to Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs) + * [Octal to Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs) * Data Structures * [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs) * [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs) diff --git a/src/conversions/mod.rs b/src/conversions/mod.rs index fd39ed9d2a2..ef59aac2354 100644 --- a/src/conversions/mod.rs +++ b/src/conversions/mod.rs @@ -3,8 +3,10 @@ mod binary_to_hexadecimal; mod decimal_to_binary; mod hexadecimal_to_binary; mod octal_to_binary; +mod octal_to_decimal; pub use self::binary_to_decimal::binary_to_decimal; pub use self::binary_to_hexadecimal::binary_to_hexadecimal; pub use self::decimal_to_binary::decimal_to_binary; pub use self::hexadecimal_to_binary::hexadecimal_to_binary; pub use self::octal_to_binary::octal_to_binary; +pub use self::octal_to_decimal::octal_to_decimal; diff --git a/src/conversions/octal_to_decimal.rs b/src/conversions/octal_to_decimal.rs new file mode 100644 index 00000000000..18ab5076916 --- /dev/null +++ b/src/conversions/octal_to_decimal.rs @@ -0,0 +1,60 @@ +// Author: cyrixninja +// Octal to Decimal Converter: Converts Octal to Decimal +// Wikipedia References: +// 1. https://en.wikipedia.org/wiki/Octal +// 2. https://en.wikipedia.org/wiki/Decimal + +pub fn octal_to_decimal(octal_str: &str) -> Result { + let octal_str = octal_str.trim(); + + if octal_str.is_empty() { + return Err("Empty"); + } + + if !octal_str.chars().all(|c| ('0'..='7').contains(&c)) { + return Err("Non-octal Value"); + } + + // Convert octal to decimal and directly return the Result + u64::from_str_radix(octal_str, 8).map_err(|_| "Conversion error") +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_empty_string() { + let input = ""; + let expected = Err("Empty"); + assert_eq!(octal_to_decimal(input), expected); + } + + #[test] + fn test_invalid_octal() { + let input = "89"; + let expected = Err("Non-octal Value"); + assert_eq!(octal_to_decimal(input), expected); + } + + #[test] + fn test_valid_octal() { + let input = "123"; + let expected = Ok(83); + assert_eq!(octal_to_decimal(input), expected); + } + + #[test] + fn test_valid_octal2() { + let input = "1234"; + let expected = Ok(668); + assert_eq!(octal_to_decimal(input), expected); + } + + #[test] + fn test_valid_octal3() { + let input = "12345"; + let expected = Ok(5349); + assert_eq!(octal_to_decimal(input), expected); + } +}