Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created March 3, 2023 12:58
Show Gist options
  • Save rust-play/6d49285fa7b079ad16ea79f7305a44e5 to your computer and use it in GitHub Desktop.
Save rust-play/6d49285fa7b079ad16ea79f7305a44e5 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
// https://www.codewars.com/kata/57b06f90e298a7b53d000a86/train/rust
use std::collections::VecDeque;
// A double-ended queue implemented with a growable ring buffer.
use std::iter::FromIterator;
fn queue_time(customers: &[u32], n: u32) -> u32 {
let mut deq = VecDeque::from_iter(customers);
let clen = customers.len(); // 3
println!("number of customers = {}", clen);
let tills = n;
println!("number of tills = {}\n", tills); // 2
// ---------------------------------------------------------------------------------
let mut outer_vec: Vec<Vec<usize>> = Vec::new();
let mut inner_vec: Vec<usize> = Vec::new();
for _ in 0..n {
inner_vec.push(2);
}
// inner_vec and outer_vec are now in the same scope, so this is fine:
outer_vec.push(inner_vec);
println!("\nouter vec = {:?}", outer_vec);
// ---------------------------------------------------------------------------------
for (i, x) in customers.iter().enumerate() {
println!("In position {} we have value {}", i, x);
}
if tills == 1 {
return customers.iter().sum();
};
if tills >= clen as u32 {
let max = customers.iter().max().unwrap();
return *max;
}
if tills < clen as u32 {
let remainder = customers[1..].iter().sum();
println!("\nremainder = {}\n ", &remainder);
if deq[0] > &remainder {
println!("Deque 1 {}", deq[0]);
println!("Deque 2 {}", deq[1]);
}
let ret = deq.pop_front().unwrap();
return *ret;
}
0
}
fn main() {
let res = queue_time(&[96, 22, 33, 40], 2);
println!("\n{res}");
}
/*queueTime([5,3,4], 1)
// should return 12
// because when there is 1 till, the total time is just the sum of the times
queueTime([10,2,3,3], 2)
// should return 10
// because here n=2 and the 2nd, 3rd, and 4th people in the
// queue finish before the 1st person has finished.
queueTime([2,3,10], 2)
// should return 12
*/
// fn fixed_tests() {
// dotest(&[], 1, 0);
// dotest(&[5], 1, 5);
// dotest(&[2], 5, 2);
// dotest(&[1, 2, 3, 4, 5], 1, 15);
// dotest(&[1, 2, 3, 4, 5], 100, 5);
// dotest(&[2, 2, 3, 3, 4, 4], 2, 9);
// }
/*
There is a queue for the self-checkout tills at the supermarket. Your task is write a function to calculate the total time required for all the customers to check out!
input
customers: an array of positive integers representing the queue. Each integer represents a customer, and its value is the amount of time they require to check out.
n: a positive integer, the number of checkout tills.
output
The function should return an integer, the total time required.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment