From a7e7dab83ebb5a30a5ee7f2f9021ca72d90499e2 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Tue, 3 Sep 2024 11:26:54 +0200 Subject: [PATCH] Wrote program for Day 40 --- README.md | 2 +- Week-06/Day-40_Larrys-Array/day40/Cargo.toml | 6 +++ Week-06/Day-40_Larrys-Array/day40/src/lib.rs | 35 ++++++++++++++++ Week-06/Day-40_Larrys-Array/day40/src/main.rs | 41 +++++++++++++++++++ .../day40/tests/example.rs | 9 ++++ 5 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 Week-06/Day-40_Larrys-Array/day40/Cargo.toml create mode 100644 Week-06/Day-40_Larrys-Array/day40/src/lib.rs create mode 100644 Week-06/Day-40_Larrys-Array/day40/src/main.rs create mode 100644 Week-06/Day-40_Larrys-Array/day40/tests/example.rs diff --git a/README.md b/README.md index 4687673..e9682ef 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_check_mark: | | Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_check_mark: | | Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_check_mark: | -| Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_large_square: | +| Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_check_mark: | | Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_large_square: | | Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: | | Day #43 | [Area Of A Triangle](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-07/Day-43_Area-Of-A-Triangle) | :white_large_square: | diff --git a/Week-06/Day-40_Larrys-Array/day40/Cargo.toml b/Week-06/Day-40_Larrys-Array/day40/Cargo.toml new file mode 100644 index 0000000..b63e9e8 --- /dev/null +++ b/Week-06/Day-40_Larrys-Array/day40/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day40" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-06/Day-40_Larrys-Array/day40/src/lib.rs b/Week-06/Day-40_Larrys-Array/day40/src/lib.rs new file mode 100644 index 0000000..eea4887 --- /dev/null +++ b/Week-06/Day-40_Larrys-Array/day40/src/lib.rs @@ -0,0 +1,35 @@ +fn rotate(array: &mut [usize], start: usize) { + array.swap(start, start + 1); + array.swap(start + 1, start + 2); +} + +pub fn larry_array(mut array: Vec) -> Result { + let mut next = 1; + + while next <= array.len() { + let mut index = match array.iter().position(|&e| e == next) { + Some(x) => x, + None => { + return Err( + "The array must contains all the numbers from 1 to the array's length" + .to_owned(), + ); + } + }; + + if index != next - 1 && next == array.len() - 1 { + return Ok(false); + } + + while index != next - 1 { + let start = std::cmp::max(index - 2, next - 1); + let end = start + 2; + rotate(&mut array, start); + index = (end + index - 1) % end; + } + + next += 1; + } + + Ok(true) +} diff --git a/Week-06/Day-40_Larrys-Array/day40/src/main.rs b/Week-06/Day-40_Larrys-Array/day40/src/main.rs new file mode 100644 index 0000000..a9f75dd --- /dev/null +++ b/Week-06/Day-40_Larrys-Array/day40/src/main.rs @@ -0,0 +1,41 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day40::larry_array; + +fn main() { + print!("Insert the Larry's array (space separated numbers from 1 to the #numbers): "); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut buffer = String::new(); + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let array = buffer + .split_whitespace() + .map(|x| match x.parse() { + Ok(x) => x, + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }) + .collect(); + + match larry_array(array) { + Ok(x) => { + if x { + println!("The array can be sorted!"); + } else { + println!("The array cannot be sorted!"); + } + } + Err(e) => { + eprintln!("{e}"); + exit(1); + } + }; +} diff --git a/Week-06/Day-40_Larrys-Array/day40/tests/example.rs b/Week-06/Day-40_Larrys-Array/day40/tests/example.rs new file mode 100644 index 0000000..ae52841 --- /dev/null +++ b/Week-06/Day-40_Larrys-Array/day40/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day40::larry_array; + + #[test] + fn example() { + assert_eq!(larry_array(vec![1, 6, 5, 2, 4, 3]), Ok(true)); + } +}