diff --git a/README.md b/README.md index 2d98e4c..7d8142a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #8 | [Letter Combinations Of A Phone Number](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-08_Letter-Combinations-Of-A-Phone-Number) | :white_check_mark: | | Day #9 | [Trapping Rain Water](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-09_Trapping-Rain-Water) | :white_check_mark: | | Day #10 | [Unique Binary Search Tree](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-10_Unique-Binary-Search-Trees) | :white_check_mark: | -| Day #11 | [Restore IP Addresses](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-11_Restore-IP-Addresses) | :white_large_square: | +| Day #11 | [Restore IP Addresses](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-11_Restore-IP-Addresses) | :white_check_mark: | | Day #12 | [Mountains or Valleys](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-12_Mountains_And_Valleys) | :white_large_square: | | Day #13 | [Need Help With Your Packing](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-13_Need-Help-With-Packing) | :white_large_square: | | Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :white_large_square: | diff --git a/Week-02/Day-11_Restore-IP-Addresses/day11/Cargo.toml b/Week-02/Day-11_Restore-IP-Addresses/day11/Cargo.toml new file mode 100644 index 0000000..d3a547b --- /dev/null +++ b/Week-02/Day-11_Restore-IP-Addresses/day11/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day11" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-02/Day-11_Restore-IP-Addresses/day11/src/lib.rs b/Week-02/Day-11_Restore-IP-Addresses/day11/src/lib.rs new file mode 100644 index 0000000..7b98f4d --- /dev/null +++ b/Week-02/Day-11_Restore-IP-Addresses/day11/src/lib.rs @@ -0,0 +1,49 @@ +pub fn gen_valid_ips(s: &str) -> Vec { + let mut result = ip_dp(s, 0); + result.sort(); + result +} + +fn ip_dp(s: &str, count: u8) -> Vec { + if s.is_empty() { + return Vec::new(); + } + + if count == 3 { + let byte: usize = s + .parse() + .unwrap_or_else(|_| panic!("{} contains a character that is not a digit.", s)); + + if byte <= 255 && !(s.starts_with('0') && s.len() > 1) { + return vec![s.to_string()]; + } else { + return Vec::new(); + } + } + + let mut generated = Vec::new(); + + for i in 1..=3 { + if i == s.len() + 1 { + break; + } + + let left_str = s[..s.len() - i].to_string(); + let right_str = s[s.len() - i..].to_string(); + + let byte: usize = right_str + .parse() + .unwrap_or_else(|_| panic!("{} contains a character that is not a digit.", left_str)); + + if byte <= 255 && !(right_str.starts_with('0') && i > 1) { + generated.append( + &mut ip_dp(&left_str, count + 1) + .into_iter() + .map(|x| format!("{}.{}", x, &right_str)) + .collect(), + ); + } + } + + generated +} diff --git a/Week-02/Day-11_Restore-IP-Addresses/day11/src/main.rs b/Week-02/Day-11_Restore-IP-Addresses/day11/src/main.rs new file mode 100644 index 0000000..0593ace --- /dev/null +++ b/Week-02/Day-11_Restore-IP-Addresses/day11/src/main.rs @@ -0,0 +1,25 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day11::gen_valid_ips; + +fn main() { + let mut buffer = String::new(); + + print!("Insert the digits and I will generate all the possible IPs: "); + io::stdout().flush().expect("Failed to flush stdout."); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read from stdin."); + + if buffer.trim().parse::().is_err() { + eprintln!("The input is invalid."); + exit(1); + } + + let result = gen_valid_ips(buffer.trim()); + println!("All the possible IPs: {:?}", result); +} diff --git a/Week-02/Day-11_Restore-IP-Addresses/day11/tests/examples.rs b/Week-02/Day-11_Restore-IP-Addresses/day11/tests/examples.rs new file mode 100644 index 0000000..792d736 --- /dev/null +++ b/Week-02/Day-11_Restore-IP-Addresses/day11/tests/examples.rs @@ -0,0 +1,38 @@ +use day11::gen_valid_ips; + +#[test] +fn example1() { + assert_eq!( + gen_valid_ips("25525511135"), + ["255.255.11.135", "255.255.111.35"] + ); +} + +#[test] +fn example2() { + assert_eq!(gen_valid_ips("0000"), ["0.0.0.0"]); +} + +#[test] +fn example3() { + assert_eq!(gen_valid_ips("1111"), ["1.1.1.1"]); +} + +#[test] +fn example4() { + assert_eq!(gen_valid_ips("010010"), ["0.10.0.10", "0.100.1.0"]); +} + +#[test] +fn example5() { + assert_eq!( + gen_valid_ips("101023"), + [ + "1.0.10.23", + "1.0.102.3", + "10.1.0.23", + "10.10.2.3", + "101.0.2.3" + ] + ); +}