From 9e66334c27e97277fc654c3a29c53680b4100e1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Ortega=20Froysa?= Date: Fri, 1 Jul 2022 22:12:30 +0200 Subject: [PATCH] Verbose commenting of code. --- src/main.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index cd57182..4567231 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,40 +25,54 @@ extern crate rand; fn main() { + // set number of prisoners in experiment let size = 100; - // initialize the boxes with randomly assigned numbers + // initialize the boxes with capacity for `size` number of boxes let mut boxes:Vec = Vec::with_capacity(size as usize); { + // create vector of numbers to select randomly from let mut nums:Vec = (0..size).collect(); while nums.len() > 0 { + // get a random index for nums let rand_i:usize = rand::random::() % nums.len(); + // add that element to the boxes vector boxes.push(nums[rand_i]); + // remove the entry from the nums vector nums.remove(rand_i); } } - // have prisoners find their number + // assume that all the prisoners have found their number let mut all_found = true; + // for every prisoner in the jail for i in 0..size { + // the next (first) box the prisoner is to open is the one with his number (`i`) let mut next_box:usize = i as usize; + // assume the prisoner does not find his box let mut found = false; + // for every attempt the prisoner has (i.e. `size/2`) for _j in 0..(size/2) { + // if the number inside the box is his number if boxes[next_box] == i { + // assign `found` to true found = true; + // stop looking through more boxes. break; } else { + // if not found set the next_box to the number contained in the current box next_box = boxes[next_box] as usize; } } + // if a prisoner has not found his number in `size/2` attempts if !found { all_found = false;