Wrote program for Day 40
This commit is contained in:
parent
311260e8a1
commit
a7e7dab83e
@ -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 #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 #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 #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 #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 #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: |
|
| 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: |
|
||||||
|
6
Week-06/Day-40_Larrys-Array/day40/Cargo.toml
Normal file
6
Week-06/Day-40_Larrys-Array/day40/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day40"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
35
Week-06/Day-40_Larrys-Array/day40/src/lib.rs
Normal file
35
Week-06/Day-40_Larrys-Array/day40/src/lib.rs
Normal 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)
|
||||||
|
}
|
41
Week-06/Day-40_Larrys-Array/day40/src/main.rs
Normal file
41
Week-06/Day-40_Larrys-Array/day40/src/main.rs
Normal 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);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
9
Week-06/Day-40_Larrys-Array/day40/tests/example.rs
Normal file
9
Week-06/Day-40_Larrys-Array/day40/tests/example.rs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user