Created
December 1, 2022 20:42
-
-
Save taylorjdawson/c5b3fb3f9e3ffb9fdd2b92a52ea1b5b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We need a function that will evaluate the order that a set of tasks will be completed in. | |
// When idle, the CPU will take the next task that has been queued with the lowest time to complete. | |
// Tasks are queued at the moment in time given by queued_at. | |
// Tasks will keep the CPU busy for execution_duration units of time. queued_at and execution_duration are not in a particular unit of time, | |
// but you may consider them in seconds, milliseconds, or whatever makes sense to you. | |
// The CPU can only execute 1 task at a time. | |
// You can use anything from Rust's std library. | |
pub struct Task { | |
pub id: u64, | |
pub queued_at: u32, | |
pub execution_duration: u32, | |
} | |
pub fn execution_order(mut tasks: Vec<Task>) -> Vec<u64> { | |
let num_tasks = tasks.len(); | |
let mut ordered_tasks = Vec::new(); | |
tasks.sort_by_key(|task| task.queued_at); | |
let mut last_task_queued = tasks.remove(0); | |
ordered_tasks.push(last_task_queued.id); | |
let mut time = last_task_queued.execution_duration; | |
while ordered_tasks.len() != num_tasks { | |
let filtered_tasks = tasks.iter().filter(| task | task.queued_at <= time); | |
let next_task = filtered_tasks.min_by_key(|task| task.execution_duration).unwrap(); | |
let next_task_index = tasks.iter().enumerate().find(| (_, task)| task.id == next_task.id ).unwrap().0; | |
last_task_queued = tasks.remove(next_task_index); | |
time += last_task_queued.execution_duration; | |
ordered_tasks.push(last_task_queued.id); | |
} | |
return ordered_tasks | |
} | |
fn main() { | |
println!("Hello, world!"); | |
} | |
#[cfg(test)] | |
mod tests { | |
use super::*; | |
#[test] | |
fn reverse_queue_order() { | |
let tasks = vec![ | |
Task { | |
id: 42, | |
queued_at: 5, | |
execution_duration: 3, | |
}, | |
Task { | |
id: 43, | |
queued_at: 2, | |
execution_duration: 3, | |
}, | |
Task { | |
id: 44, | |
queued_at: 0, | |
execution_duration: 2, | |
}, | |
]; | |
assert_eq!(execution_order(tasks), vec![44, 43, 42]); | |
} | |
#[test] | |
fn two_items_queued_at_once() { | |
// 0: #42 is queued | |
// 0: #42 is started | |
// 1: #43 is queued | |
// 2: #44 is queued | |
// 3: #42 is finished | |
// 3: #44 is started (it is queued and has a lower execution_duration than #43) | |
// 5: #44 is finished | |
// 5: #43 is started | |
// 8: #43 is finished | |
let tasks = vec![ | |
Task { | |
id: 42, | |
queued_at: 0, | |
execution_duration: 3, | |
}, | |
Task { | |
id: 43, | |
queued_at: 1, | |
execution_duration: 3, | |
}, | |
Task { | |
id: 44, | |
queued_at: 2, | |
execution_duration: 2, | |
}, | |
]; | |
assert_eq!(execution_order(tasks), vec![42, 44, 43]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment