Wrote program for Day 11
This commit is contained in:
parent
a4cc6e2129
commit
759f1bb3a1
@ -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 #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 #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 #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 #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 #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: |
|
| 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: |
|
||||||
|
6
Week-02/Day-11_Restore-IP-Addresses/day11/Cargo.toml
Normal file
6
Week-02/Day-11_Restore-IP-Addresses/day11/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day11"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
49
Week-02/Day-11_Restore-IP-Addresses/day11/src/lib.rs
Normal file
49
Week-02/Day-11_Restore-IP-Addresses/day11/src/lib.rs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
pub fn gen_valid_ips(s: &str) -> Vec<String> {
|
||||||
|
let mut result = ip_dp(s, 0);
|
||||||
|
result.sort();
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ip_dp(s: &str, count: u8) -> Vec<String> {
|
||||||
|
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
|
||||||
|
}
|
25
Week-02/Day-11_Restore-IP-Addresses/day11/src/main.rs
Normal file
25
Week-02/Day-11_Restore-IP-Addresses/day11/src/main.rs
Normal file
@ -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::<usize>().is_err() {
|
||||||
|
eprintln!("The input is invalid.");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = gen_valid_ips(buffer.trim());
|
||||||
|
println!("All the possible IPs: {:?}", result);
|
||||||
|
}
|
38
Week-02/Day-11_Restore-IP-Addresses/day11/tests/examples.rs
Normal file
38
Week-02/Day-11_Restore-IP-Addresses/day11/tests/examples.rs
Normal file
@ -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"
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user