Wrote program for Day 40

This commit is contained in:
Mariano Riefolo 2024-09-03 11:26:54 +02:00
parent 311260e8a1
commit a7e7dab83e
5 changed files with 92 additions and 1 deletions

View File

@ -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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day40"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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<usize>) -> Result<bool, String> {
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)
}

View File

@ -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);
}
};
}

View File

@ -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));
}
}