Wrote program for Day 27
This commit is contained in:
parent
b9b2d1d67c
commit
4f35f76454
@ -73,7 +73,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: |
|
| Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: |
|
||||||
| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: |
|
| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: |
|
||||||
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: |
|
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_check_mark: |
|
||||||
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: |
|
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_check_mark: |
|
||||||
| Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: |
|
| Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: |
|
||||||
| Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_large_square: |
|
| Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_large_square: |
|
||||||
| Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_large_square: |
|
| Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_large_square: |
|
||||||
|
6
Week-04/Day-27_Task-Scheduler/day27/Cargo.toml
Normal file
6
Week-04/Day-27_Task-Scheduler/day27/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day27"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
35
Week-04/Day-27_Task-Scheduler/day27/src/lib.rs
Normal file
35
Week-04/Day-27_Task-Scheduler/day27/src/lib.rs
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
pub fn task_scheduler(tasks: &[char], coldown: u8) -> u16 {
|
||||||
|
let mut num_tasks = HashMap::new();
|
||||||
|
for task in tasks {
|
||||||
|
*num_tasks.entry(task).or_insert(0) += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut num_tasks: Vec<(&&char, &i32)> = num_tasks.iter().collect();
|
||||||
|
num_tasks.sort_by(|a, b| b.1.cmp(a.1));
|
||||||
|
|
||||||
|
let mut result = 0;
|
||||||
|
let mut pos = 0;
|
||||||
|
let mut range = 1;
|
||||||
|
|
||||||
|
for _ in 0..tasks.len() {
|
||||||
|
if num_tasks.get(pos as usize).is_none() {
|
||||||
|
result += if pos > coldown as u16 {
|
||||||
|
0
|
||||||
|
} else {
|
||||||
|
coldown as u16 - pos + 1
|
||||||
|
};
|
||||||
|
pos = 0;
|
||||||
|
range += 1;
|
||||||
|
}
|
||||||
|
if *num_tasks.get(pos as usize).unwrap().1 < range {
|
||||||
|
result += coldown as u16 - pos;
|
||||||
|
pos = 0;
|
||||||
|
range += 1;
|
||||||
|
}
|
||||||
|
result += 1;
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}
|
33
Week-04/Day-27_Task-Scheduler/day27/src/main.rs
Normal file
33
Week-04/Day-27_Task-Scheduler/day27/src/main.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use std::io::{self, Write};
|
||||||
|
|
||||||
|
use day27::task_scheduler;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
print!("Insert the task as a unique string: ");
|
||||||
|
io::stdout().flush().expect("Failed to flush");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read line");
|
||||||
|
|
||||||
|
let tasks: Vec<char> = buffer.trim().chars().collect();
|
||||||
|
|
||||||
|
buffer = String::new();
|
||||||
|
|
||||||
|
print!("Insert the length of the coldown: ");
|
||||||
|
io::stdout().flush().expect("Failed to flush");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read line");
|
||||||
|
|
||||||
|
let coldown = buffer
|
||||||
|
.trim()
|
||||||
|
.parse()
|
||||||
|
.expect("Coldown must be a valid unsigned integer");
|
||||||
|
|
||||||
|
let result = task_scheduler(&tasks, coldown);
|
||||||
|
println!("The least number of units of times that the CPU will take to finish all the given tasks is {}", result);
|
||||||
|
}
|
25
Week-04/Day-27_Task-Scheduler/day27/tests/examples.rs
Normal file
25
Week-04/Day-27_Task-Scheduler/day27/tests/examples.rs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod examples {
|
||||||
|
use day27::task_scheduler;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example1() {
|
||||||
|
assert_eq!(task_scheduler(&['A', 'A', 'A', 'B', 'B', 'B'], 2), 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example2() {
|
||||||
|
assert_eq!(task_scheduler(&['A', 'A', 'A', 'B', 'B', 'B'], 0), 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example3() {
|
||||||
|
assert_eq!(
|
||||||
|
task_scheduler(
|
||||||
|
&['A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'],
|
||||||
|
2
|
||||||
|
),
|
||||||
|
16
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user