From 96b17663448e0890496d8f9e1b5f9378a81954be Mon Sep 17 00:00:00 2001 From: Eyob Date: Tue, 18 Feb 2025 13:47:31 +0300 Subject: [PATCH 01/11] ---------RADU ASSIGNMENT ANSWER(in_progress)--------- --- .gitignore | 1 + Cargo.lock | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 8 ++ src/main.rs | 51 +++++++++++ 4 files changed, 304 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d8a0059 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,244 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + +[[package]] +name = "libc" +version = "0.2.169" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" + +[[package]] +name = "noob" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy 0.7.35", +] + +[[package]] +name = "proc-macro2" +version = "1.0.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rand" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3779b94aeb87e8bd4e834cee3650289ee9e0d5677f976ecdb6d219e5f4f6cd94" +dependencies = [ + "rand_chacha", + "rand_core", + "zerocopy 0.8.17", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b08f3c9802962f7e1b25113931d94f43ed9725bebc59db9d0c3e9a23b67e15ff" +dependencies = [ + "getrandom", + "zerocopy 0.8.17", +] + +[[package]] +name = "syn" +version = "2.0.98" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" + +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive 0.7.35", +] + +[[package]] +name = "zerocopy" +version = "0.8.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa91407dacce3a68c56de03abe2760159582b846c6a4acd2f456618087f12713" +dependencies = [ + "zerocopy-derive 0.8.17", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06718a168365cad3d5ff0bb133aad346959a2074bd4a85c121255a11304a8626" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c9e5a79 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "noob" +version = "0.1.0" +edition = "2021" + +[dependencies] +rand = "0.9.0" + diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..67e8038 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,51 @@ +use core::f64; +use std::io; + +fn main() { + // taking input from the user + let mut user_input = String::new(); + io::stdin() + .read_line(&mut user_input) + .expect("unable to read user input"); + //parsing the input into a unsigned int + let user_input = user_input.trim().parse::().unwrap(); + //Instructing only valid inputs are between 1-10_000 + if user_input < 1 { println!("user input is less than one")} else if user_input > 10_000 {println!("user input is greater that 10_000")} + // here the user generates userinput(N) amount of random float numbers between 0 and 1 and + // subsequently push them into an array + else { + //initalizing the array + let mut float_array:[f64;1_000] = [0.0;1_000]; + //doubles as a counter and an index for the array + let mut array_index:u16 = 0; + let mut loop_amount:u16 = 0; + + //the loop to fill the array + loop { + //initalizing random float with in range + let generated_float:f64 = rand::random_range(0.0..=1.0); + + //pushing random float into array + float_array[array_index as usize] = generated_float; + + array_index += 1; + if array_index == user_input || array_index == 1000 {break} + } + println!("this is the array {:?}",float_array); + //loop to see how much it takes to regenerate the same numbers, in the new array + loop { + //initalizing random float with in range + let generated_float:f64 = rand::random_range(0.0..=1.0); + //initalizing the counter + let mut counter = 0; + + loop_amount += 1; + counter +=1; + if counter == user_input {break} + } + + } + + + +} From 56860ae6a14dfac53da17d9bb719c399f1bba8f6 Mon Sep 17 00:00:00 2001 From: Eyob Date: Thu, 20 Feb 2025 21:52:54 +0300 Subject: [PATCH 02/11] getting there... --- src/main.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 67e8038..7d79194 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use core::f64; use std::io; + fn main() { // taking input from the user let mut user_input = String::new(); @@ -15,7 +16,7 @@ fn main() { // subsequently push them into an array else { //initalizing the array - let mut float_array:[f64;1_000] = [0.0;1_000]; + let mut float_array:[f64;1_0] = [0.0;1_0]; //doubles as a counter and an index for the array let mut array_index:u16 = 0; let mut loop_amount:u16 = 0; @@ -37,12 +38,16 @@ fn main() { //initalizing random float with in range let generated_float:f64 = rand::random_range(0.0..=1.0); //initalizing the counter - let mut counter = 0; + let mut counter:u16 = 0; + + let this = float_array.get(2).unwrap(); + println!("well >{this}"); loop_amount += 1; counter +=1; if counter == user_input {break} } + println!("This is your loop counter {loop_amount}"); } From e4310f0eac2cbd8783541ef3ec5da24263449ff8 Mon Sep 17 00:00:00 2001 From: Eyob Date: Fri, 21 Feb 2025 02:19:27 +0300 Subject: [PATCH 03/11] finially there --- src/main.rs | 68 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7d79194..caf35a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ -use core::f64; use std::io; fn main() { // taking input from the user + println!("Please enter the amount of random generated floats you want in your array \n-------------------------------------"); let mut user_input = String::new(); io::stdin() .read_line(&mut user_input) @@ -11,46 +11,70 @@ fn main() { //parsing the input into a unsigned int let user_input = user_input.trim().parse::().unwrap(); //Instructing only valid inputs are between 1-10_000 - if user_input < 1 { println!("user input is less than one")} else if user_input > 10_000 {println!("user input is greater that 10_000")} + if user_input < 1 { println!("user input is less than one")} else if user_input > 1_00 {println!("user input is greater that 100")} // here the user generates userinput(N) amount of random float numbers between 0 and 1 and // subsequently push them into an array else { //initalizing the array - let mut float_array:[f64;1_0] = [0.0;1_0]; + let mut float_array:[f32;1_00] = [0.0;1_00]; //doubles as a counter and an index for the array let mut array_index:u16 = 0; - let mut loop_amount:u16 = 0; + //the loop to fill the array loop { //initalizing random float with in range - let generated_float:f64 = rand::random_range(0.0..=1.0); + let generated_float:f32 = rand::random_range(0.0..=1.0); //pushing random float into array float_array[array_index as usize] = generated_float; array_index += 1; - if array_index == user_input || array_index == 1000 {break} + if array_index == user_input || array_index == 100 {break} } - println!("this is the array {:?}",float_array); - //loop to see how much it takes to regenerate the same numbers, in the new array - loop { - //initalizing random float with in range - let generated_float:f64 = rand::random_range(0.0..=1.0); - //initalizing the counter - let mut counter:u16 = 0; + //we are extracting the an array of the random generated numbers only since rust doesnt + //allow dynamically sized arrays + let extracted_array = &mut float_array[0..user_input as usize]; + println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_array); + let mut total_loop = 0; + let mut loop_array:[u64;100] = [0;100]; + //looping hundred times + //initalizing the loop counter + loop { + let mut matched = [false; 100]; // Fixed-size array to track matched elements + let mut success = 0; + let mut loop_amount = 0; + println!("---------------------------------- Loop({})",total_loop + 1); - let this = float_array.get(2).unwrap(); - println!("well >{this}"); + // Loop to see how much it takes to regenerate the same numbers + loop { + // Generate a random float + let generated_float: f32 = rand::random_range(0.0..=1.0); - loop_amount += 1; - counter +=1; - if counter == user_input {break} + // Iterate over the array to find a match + for i in 0..extracted_array.len() { + if !matched[i] && extracted_array[i] == generated_float { + println!("Match for index {} found",success); + success += 1; + // checking for matched random numbers so it doesnt match it again + matched[i] = true; + break; + } + } + + loop_amount += 1; + if success == user_input {break} + } + loop_array[total_loop] = loop_amount; + total_loop += 1; + if total_loop == 100 {break} } - println!("This is your loop counter {loop_amount}"); + loop_array.sort(); + //for debugging purposes + //println!("Array {:#?}",loop_array); + println!("This is the Minimum loop needed {}",loop_array[0]); + println!("This is the Average loop needed {}",loop_array[49]); + println!("This is the Maximum loop needed {}",loop_array[99]); } - - - } From 17f566dbe5636bffcba1c07c066b8732ee780b31 Mon Sep 17 00:00:00 2001 From: Eyob Date: Fri, 21 Feb 2025 12:03:10 +0300 Subject: [PATCH 04/11] added time as well --- src/main.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index caf35a1..4b163da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::io; +use std::{io,time::Instant}; fn main() { @@ -38,6 +38,7 @@ fn main() { println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_array); let mut total_loop = 0; let mut loop_array:[u64;100] = [0;100]; + let mut time_array:[f32;100] = [0.0;100]; //looping hundred times //initalizing the loop counter loop { @@ -47,6 +48,7 @@ fn main() { println!("---------------------------------- Loop({})",total_loop + 1); // Loop to see how much it takes to regenerate the same numbers + let now = Instant::now(); loop { // Generate a random float let generated_float: f32 = rand::random_range(0.0..=1.0); @@ -63,18 +65,24 @@ fn main() { } loop_amount += 1; + if success == user_input {break} } + let time_taken = now.elapsed().as_secs_f32(); loop_array[total_loop] = loop_amount; + time_array[total_loop] = time_taken; total_loop += 1; if total_loop == 100 {break} } + loop_array.sort(); + time_array.sort_by(|a, b| a.partial_cmp(b).unwrap()); //for debugging purposes - //println!("Array {:#?}",loop_array); - println!("This is the Minimum loop needed {}",loop_array[0]); - println!("This is the Average loop needed {}",loop_array[49]); - println!("This is the Maximum loop needed {}",loop_array[99]); + println!("Array {:#?}",loop_array); + println!("Array {:#?}",time_array); + println!("This is the Minimum loop {} and time {} needed",loop_array[0] ,time_array[0]); + println!("This is the Minimum loop {} and time {} needed",loop_array[49] ,time_array[49]); + println!("This is the Minimum loop {} and time {} needed",loop_array[99] ,time_array[99]); } } From 4b42df887aa7517c3839c00beff077247802848b Mon Sep 17 00:00:00 2001 From: Eyob Date: Fri, 28 Feb 2025 17:56:47 +0300 Subject: [PATCH 05/11] added the advanced homework as well --- .gitignore | 1 - Chapter_1-3/.gitignore | 3 + Cargo.lock => Chapter_1-3/Cargo.lock | 0 Cargo.toml => Chapter_1-3/Cargo.toml | 0 LICENSE => Chapter_1-3/LICENSE | 0 README.md => Chapter_1-3/README.md | 0 Chapter_1-3/benches/my_benchmark/main.rs | 16 ++++ {docs => Chapter_1-3/docs}/mentors.md | 0 {docs => Chapter_1-3/docs}/students.md | 0 Chapter_1-3/src/main.rs | 117 +++++++++++++++++++++++ Chapter_1-3/src/uniform.rs | 1 + Chapter_1-3/src/uniform/uni_dist.rs | 27 ++++++ Chapter_1-3/src/weighted.rs | 1 + Chapter_1-3/src/weighted/wei_dist.rs | 46 +++++++++ src/main.rs | 88 ----------------- 15 files changed, 211 insertions(+), 89 deletions(-) delete mode 100644 .gitignore create mode 100644 Chapter_1-3/.gitignore rename Cargo.lock => Chapter_1-3/Cargo.lock (100%) rename Cargo.toml => Chapter_1-3/Cargo.toml (100%) rename LICENSE => Chapter_1-3/LICENSE (100%) rename README.md => Chapter_1-3/README.md (100%) create mode 100644 Chapter_1-3/benches/my_benchmark/main.rs rename {docs => Chapter_1-3/docs}/mentors.md (100%) rename {docs => Chapter_1-3/docs}/students.md (100%) create mode 100644 Chapter_1-3/src/main.rs create mode 100644 Chapter_1-3/src/uniform.rs create mode 100644 Chapter_1-3/src/uniform/uni_dist.rs create mode 100644 Chapter_1-3/src/weighted.rs create mode 100644 Chapter_1-3/src/weighted/wei_dist.rs delete mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ea8c4bf..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target diff --git a/Chapter_1-3/.gitignore b/Chapter_1-3/.gitignore new file mode 100644 index 0000000..e60b9ec --- /dev/null +++ b/Chapter_1-3/.gitignore @@ -0,0 +1,3 @@ +/target +./LICENSE +./docs/ diff --git a/Cargo.lock b/Chapter_1-3/Cargo.lock similarity index 100% rename from Cargo.lock rename to Chapter_1-3/Cargo.lock diff --git a/Cargo.toml b/Chapter_1-3/Cargo.toml similarity index 100% rename from Cargo.toml rename to Chapter_1-3/Cargo.toml diff --git a/LICENSE b/Chapter_1-3/LICENSE similarity index 100% rename from LICENSE rename to Chapter_1-3/LICENSE diff --git a/README.md b/Chapter_1-3/README.md similarity index 100% rename from README.md rename to Chapter_1-3/README.md diff --git a/Chapter_1-3/benches/my_benchmark/main.rs b/Chapter_1-3/benches/my_benchmark/main.rs new file mode 100644 index 0000000..e459ec1 --- /dev/null +++ b/Chapter_1-3/benches/my_benchmark/main.rs @@ -0,0 +1,16 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; + +fn fibonacci(n: u64) -> u64 { + match n { + 0 => 1, + 1 => 1, + n => fibonacci(n-1) + fibonacci(n-2), + } +} + +fn criterion_benchmark(c: &mut Criterion) { + c.bench_function("Benchmarking Loops: ", |b| b.iter(|| fibonacci(black_box(20)))); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); diff --git a/docs/mentors.md b/Chapter_1-3/docs/mentors.md similarity index 100% rename from docs/mentors.md rename to Chapter_1-3/docs/mentors.md diff --git a/docs/students.md b/Chapter_1-3/docs/students.md similarity index 100% rename from docs/students.md rename to Chapter_1-3/docs/students.md diff --git a/Chapter_1-3/src/main.rs b/Chapter_1-3/src/main.rs new file mode 100644 index 0000000..8c5f5d8 --- /dev/null +++ b/Chapter_1-3/src/main.rs @@ -0,0 +1,117 @@ +use std::{io,time::Instant}; +use rand::random_range; + +mod uniform; +use uniform::uni_dist; + +mod weighted; +use weighted::wei_dist; + +fn main() { + // taking input from the user + println!("Please enter the amount of random generated floats you want in your array \n-------------------------------------"); + let mut user_input = String::new(); + io::stdin() + .read_line(&mut user_input) + .expect("unable to read user input"); + //parsing the input into a unsigned int + let user_input = user_input.trim().parse::().unwrap(); + //Instructing only valid inputs are between 1-10_000 + if user_input < 1 { println!("user input is less than one")} else if user_input > 1_00 {println!("user input is greater that 100")} + // here the user generates userinput(N) amount of random float numbers between 0 and 1 and + // subsequently push them into an array + else { + const _BILLION:u64 = 1000000000; + + //initalizing the arrays + let mut float_array:[f32;1_00] = [0.0;1_00]; + let mut int_array:[u64;100] = [0;100]; + + //doubles as a counter and an index for the array + let mut array_index:u16 = 0; + + + //the loop to fill the array + loop { + //initalizing random float with in range + let generated_float:f32 = random_range(0.0..=1.0); + + //initalizing random float with in range + let generated_int:u64 = random_range(0..=1000); + + //pushing random float into array + float_array[array_index as usize] = generated_float; + + //pushing random float into array + int_array[array_index as usize] = generated_int; + + array_index += 1; + if array_index == user_input || array_index == 100 {break} + } + //we are extracting the an array of the random generated numbers only since rust doesnt + //allow dynamically sized arrays + let extracted_float = &mut float_array[0..user_input as usize]; + let extracted_int = &mut int_array[0..user_input as usize]; + + println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_float); + println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_int); + let mut uniform_total_loop = 0; + let mut weighted_total_loop = 0; + + let mut uniform_loop_array:[u64;7] = [0;7]; + let mut uniform_time_array:[f32;7] = [0.0;7]; + + let mut weighted_loop_array:[u64;7] = [0;7]; + let mut weighted_time_array:[f32;7] = [0.0;7]; + //looping hundred times + //initalizing the loop counter + '_uniform_distribution:loop { + let matched = [false; 100]; // Fixed-size array to track matched elements + let success = 0; + let loop_amount = 0; + println!("---------------------------------- Loop({})-UNIFORM_DISTRIBUTION",uniform_total_loop + 1); + + // Loop to see how much it takes to regenerate the same numbers + let now = Instant::now(); + let loop_amount = uni_dist::uniform_distribution(loop_amount, user_input, extracted_float, success, matched); + let time_taken = now.elapsed().as_secs_f32(); + uniform_loop_array[uniform_total_loop] = loop_amount; + uniform_time_array[uniform_total_loop] = time_taken; + uniform_total_loop += 1; + if uniform_total_loop == 7 {break} + } + + '_weighted_distribution:loop { + let matched = [false; 100]; // Fixed-size array to track matched elements + let loop_amount = 0; + println!("---------------------------------- Loop({})-WEIGHTED_DISTRIBUTION",weighted_total_loop + 1); + + // Loop to see how much it takes to regenerate the same numbers + let now = Instant::now(); + let loop_amount = wei_dist::weighted_distribution(loop_amount,extracted_int, matched); + let time_taken = now.elapsed().as_secs_f32(); + weighted_loop_array[weighted_total_loop] = loop_amount; + weighted_time_array[weighted_total_loop] = time_taken; + weighted_total_loop += 1; + if weighted_total_loop == 7 {break} + } + + uniform_loop_array.sort(); + uniform_time_array.sort_by(|a, b| a.partial_cmp(b).unwrap()); + + weighted_loop_array.sort(); + weighted_time_array.sort_by(|a, b| a.partial_cmp(b).unwrap()); + //for debugging purposes + //println!("Array {:#?}",loop_array); + //println!("Array {:#?}",time_array); + println!("\n------------FOR THE UNIFORM DISTRIBUTION------------"); + println!("This is the Minimum loop {} and time {} needed",uniform_loop_array[0] ,uniform_time_array[0]); + println!("This is the Average loop {} and time {} needed",uniform_loop_array[3] ,uniform_time_array[3]); + println!("This is the Maxmimum loop {} and time {} needed",uniform_loop_array[6] ,uniform_time_array[6]); + println!("\n------------FOR THE WEIGHTED DISTRIBUTION------------"); + println!("This is the Minimum loop {} and time {} needed",weighted_loop_array[0] ,weighted_time_array[0]); + println!("This is the Average loop {} and time {} needed",weighted_loop_array[3] ,weighted_time_array[3]); + println!("This is the Maxmimum loop {} and time {} needed",weighted_loop_array[6] ,weighted_time_array[6]); + + } +} diff --git a/Chapter_1-3/src/uniform.rs b/Chapter_1-3/src/uniform.rs new file mode 100644 index 0000000..12af91f --- /dev/null +++ b/Chapter_1-3/src/uniform.rs @@ -0,0 +1 @@ +pub mod uni_dist; diff --git a/Chapter_1-3/src/uniform/uni_dist.rs b/Chapter_1-3/src/uniform/uni_dist.rs new file mode 100644 index 0000000..a6fed1b --- /dev/null +++ b/Chapter_1-3/src/uniform/uni_dist.rs @@ -0,0 +1,27 @@ +pub fn uniform_distribution(loop_amount:u64, user_input:u16, extracted_array:&mut [f32], success:u16, matched:[bool;100]) -> u64 { + let mut loop_amount = loop_amount; + let mut success = success; + let mut matched = matched; + + // Loop to see how much it takes to regenerate the same numbers + loop { + // Generate a random float + let generated_float: f32 = rand::random_range(0.0..=1.0); + + // Iterate over the array to find a match + for i in 0..extracted_array.len() { + if !matched[i] && extracted_array[i] == generated_float { + println!("Match for index {} found",success); + success += 1; + // checking for matched random numbers so it doesnt match it again + matched[i] = true; + break; + } + } + + loop_amount += 1; + + if success == user_input {break} + } + loop_amount +} diff --git a/Chapter_1-3/src/weighted.rs b/Chapter_1-3/src/weighted.rs new file mode 100644 index 0000000..011546d --- /dev/null +++ b/Chapter_1-3/src/weighted.rs @@ -0,0 +1 @@ +pub mod wei_dist; diff --git a/Chapter_1-3/src/weighted/wei_dist.rs b/Chapter_1-3/src/weighted/wei_dist.rs new file mode 100644 index 0000000..b299ef1 --- /dev/null +++ b/Chapter_1-3/src/weighted/wei_dist.rs @@ -0,0 +1,46 @@ +use rand::random_range; + +pub fn weighted_distribution(loop_amount:u64, extracted_array:&mut [u64], matched:[bool;100]) -> u64 { + //initalizing variables + const ONE_HUNDRED:u64 = 100; + let mut loop_amount = loop_amount; + let mut matched = matched; + + let mut weight_interval = 0; + let mut weight_array:[(u64,u64,u64);10] = [(0,ONE_HUNDRED,0);10]; + for value in weight_array.iter_mut() { + value.0 += weight_interval; + value.1 += weight_interval; + for i in extracted_array.iter() { + if value.0 < *i && *i < value.1 { value.2+=1 } + } + weight_interval += ONE_HUNDRED; + } + //sorting based on they're weights + weight_array.sort_by(|b, a| a.2.cmp(&b.2)); + println!("WEIGHT MAP \n {:?}",weight_array); + + // Loop to generate the same numbers based on they're weights + loop { + // Generate a random float + let generated_num = random_range(weight_array[0].0..weight_array[0].1); + + // Iterate over the array to find a match + for (i,value) in extracted_array.iter().enumerate() { + if !matched[i] && *value == generated_num { + println!("HIT, match found for interval {} - {}",weight_array[0].0,weight_array[0].1); + //making sure the weight decreases for each hit + weight_array[0].2 -= 1; + // checking for matched random numbers so it doesnt match it again + matched[i] = true; + } + } + + // re-sorting the arrays so that we always generate numbers for the highest weight interval + weight_array.sort_by(|b, a| a.2.cmp(&b.2)); + loop_amount += 1; + + if weight_array[0].2 == 0 {break} + } + loop_amount +} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 4b163da..0000000 --- a/src/main.rs +++ /dev/null @@ -1,88 +0,0 @@ -use std::{io,time::Instant}; - - -fn main() { - // taking input from the user - println!("Please enter the amount of random generated floats you want in your array \n-------------------------------------"); - let mut user_input = String::new(); - io::stdin() - .read_line(&mut user_input) - .expect("unable to read user input"); - //parsing the input into a unsigned int - let user_input = user_input.trim().parse::().unwrap(); - //Instructing only valid inputs are between 1-10_000 - if user_input < 1 { println!("user input is less than one")} else if user_input > 1_00 {println!("user input is greater that 100")} - // here the user generates userinput(N) amount of random float numbers between 0 and 1 and - // subsequently push them into an array - else { - //initalizing the array - let mut float_array:[f32;1_00] = [0.0;1_00]; - //doubles as a counter and an index for the array - let mut array_index:u16 = 0; - - - //the loop to fill the array - loop { - //initalizing random float with in range - let generated_float:f32 = rand::random_range(0.0..=1.0); - - //pushing random float into array - float_array[array_index as usize] = generated_float; - - array_index += 1; - if array_index == user_input || array_index == 100 {break} - } - //we are extracting the an array of the random generated numbers only since rust doesnt - //allow dynamically sized arrays - let extracted_array = &mut float_array[0..user_input as usize]; - println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_array); - let mut total_loop = 0; - let mut loop_array:[u64;100] = [0;100]; - let mut time_array:[f32;100] = [0.0;100]; - //looping hundred times - //initalizing the loop counter - loop { - let mut matched = [false; 100]; // Fixed-size array to track matched elements - let mut success = 0; - let mut loop_amount = 0; - println!("---------------------------------- Loop({})",total_loop + 1); - - // Loop to see how much it takes to regenerate the same numbers - let now = Instant::now(); - loop { - // Generate a random float - let generated_float: f32 = rand::random_range(0.0..=1.0); - - // Iterate over the array to find a match - for i in 0..extracted_array.len() { - if !matched[i] && extracted_array[i] == generated_float { - println!("Match for index {} found",success); - success += 1; - // checking for matched random numbers so it doesnt match it again - matched[i] = true; - break; - } - } - - loop_amount += 1; - - if success == user_input {break} - } - let time_taken = now.elapsed().as_secs_f32(); - loop_array[total_loop] = loop_amount; - time_array[total_loop] = time_taken; - total_loop += 1; - if total_loop == 100 {break} - } - - loop_array.sort(); - time_array.sort_by(|a, b| a.partial_cmp(b).unwrap()); - //for debugging purposes - println!("Array {:#?}",loop_array); - println!("Array {:#?}",time_array); - println!("This is the Minimum loop {} and time {} needed",loop_array[0] ,time_array[0]); - println!("This is the Minimum loop {} and time {} needed",loop_array[49] ,time_array[49]); - println!("This is the Minimum loop {} and time {} needed",loop_array[99] ,time_array[99]); - - } -} From b9888473e7c840277b37194690557aa481fd97a1 Mon Sep 17 00:00:00 2001 From: Eyob Date: Fri, 28 Feb 2025 17:56:47 +0300 Subject: [PATCH 06/11] added the advanced homework as well --- .gitignore | 4 ++++ Chapter_1-3/.gitignore | 3 --- Chapter_1-3/LICENSE => LICENSE | 0 {Chapter_1-3/docs => docs}/mentors.md | 0 {Chapter_1-3/docs => docs}/students.md | 0 5 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 .gitignore delete mode 100644 Chapter_1-3/.gitignore rename Chapter_1-3/LICENSE => LICENSE (100%) rename {Chapter_1-3/docs => docs}/mentors.md (100%) rename {Chapter_1-3/docs => docs}/students.md (100%) diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d114c91 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +LICENSE +Chapter_1-3/target/ +docs/ + diff --git a/Chapter_1-3/.gitignore b/Chapter_1-3/.gitignore deleted file mode 100644 index e60b9ec..0000000 --- a/Chapter_1-3/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/target -./LICENSE -./docs/ diff --git a/Chapter_1-3/LICENSE b/LICENSE similarity index 100% rename from Chapter_1-3/LICENSE rename to LICENSE diff --git a/Chapter_1-3/docs/mentors.md b/docs/mentors.md similarity index 100% rename from Chapter_1-3/docs/mentors.md rename to docs/mentors.md diff --git a/Chapter_1-3/docs/students.md b/docs/students.md similarity index 100% rename from Chapter_1-3/docs/students.md rename to docs/students.md From 60d8798036fb8d4f665313f32add35d10606615a Mon Sep 17 00:00:00 2001 From: Eyob Date: Fri, 28 Feb 2025 18:32:41 +0300 Subject: [PATCH 07/11] added advanced homework too --- .gitignore | 2 + Chapter_1-3/src/main.rs | 1 + LICENSE | 201 ---------------------------------------- docs/mentors.md | 42 --------- docs/students.md | 28 ------ 5 files changed, 3 insertions(+), 271 deletions(-) delete mode 100644 LICENSE delete mode 100644 docs/mentors.md delete mode 100644 docs/students.md diff --git a/.gitignore b/.gitignore index d114c91..f4ab2e3 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ LICENSE Chapter_1-3/target/ docs/ +LICENSE +docs/ diff --git a/Chapter_1-3/src/main.rs b/Chapter_1-3/src/main.rs index 8c5f5d8..07619e4 100644 --- a/Chapter_1-3/src/main.rs +++ b/Chapter_1-3/src/main.rs @@ -53,6 +53,7 @@ fn main() { let extracted_float = &mut float_array[0..user_input as usize]; let extracted_int = &mut int_array[0..user_input as usize]; + //For the uniform and the weighted distribution println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_float); println!("-------------------------------------\nThis is the extracted array with the random generated numbers:\n{:#?}", extracted_int); let mut uniform_total_loop = 0; diff --git a/LICENSE b/LICENSE deleted file mode 100644 index 261eeb9..0000000 --- a/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright [yyyy] [name of copyright owner] - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/docs/mentors.md b/docs/mentors.md deleted file mode 100644 index cb2ae72..0000000 --- a/docs/mentors.md +++ /dev/null @@ -1,42 +0,0 @@ -# For the mentors to follow - -## Course setup - -1. Make sure you're on the chat or other communication channels with the rest of the team -2. At the beginning of each course, make sure the material is ready in [/docs](.) and [/assignments](../assignments) -3. [/assignments](../assignments) structure (usually this exists already. Make sure it's valid and change as needed): - A folder for each chapters group, like `Chapter_1-3` with the following structure: - - `requirements` folder: Containing a file called `Chapter_N-M_assignment.md` with the requirements and any other needed resources - - `solutions` folder: This folder will contain folders with each student's GitHub account ID, each containing submissions from each student, each with an associated PR. - Make sure this folder is empty so it will contain the solutions from this session -4. Create a new project called `course-`, where `` is the course's year, mostly the current year - - Create GitHub issues for each chapter and assignment, adding all needed docs with relevant links to any resources. All issue descriptions should have a link to [students.md] - - For the final project, create an assignment issue titled `Final project` and have the requirements in - -## Into the course, for each week - -1. Create a GH issue for the theory part for the given chapters for the next week session, like `Course 4` and include the table of contents of the chapters -2. Create a GH issue for the home assignment for the given chapters for the last week session, like `Course 1-3` and include the requirements and any needed files in there -3. Have the weekly call of about 2h to discuss: - - Review last week's assignment (this corresponds to the chapter from 2 weeks back); let's call it `Chapter N-2: Assignment`. After this is done, mark the issue as `Done` - - Discuss this week's chapters, `Chapter 1-3` clarifying any questions - - New home assignment for this week's chapters. Show them the GH issue for it and instruct them to create PR in there and ask their questions - - Intro to ds and show the GH issue for it, instructing the students to add their questions in there - - live coding or present building part of the course main app related to last week's chapters knowledge -4. -5. -6. After this is done, mark the issue as `Done` -7. Give the assignment for last week's chapter (this is called this week's assignment), discuss any questions, and set the issue's status to `In Progress` -8. Move to this week's chapter, `Chapter 3: `, and do a short intro. Mark the status `In Progress` for the current chapter. -9. This is where the call ends -10. Over the week, resolve any comments and questions in the week's chapter and assignment issues and on Slack -11. Record everything and save videos in `videos/Chapter_N` - -## Final project - -Follow similar assignment steps to `Into the course, for each week`, ignoring the chapter steps. - -## After the course - -- After all issues and discussions have been resolved, create a new branch called `course-<year>`, where `<year>` is the course's year -- Close the project for this year from project settings diff --git a/docs/students.md b/docs/students.md deleted file mode 100644 index 4730d7c..0000000 --- a/docs/students.md +++ /dev/null @@ -1,28 +0,0 @@ -# For students to follow - -1. Fork this repo and do all your commits in there -2. Make sure you're on the chat or other communication channels with the rest of the team - -## Reading the weekly chapter - -- Locate the chapter GitHub issue in the course [project](https://github.com/orgs/xoriors/projects/3) or in [Issues](https://github.com/xoriors/course-rust/issues). Let's say it's the [Chapter 1-3](#4) -- Read the corresponding week number chapter from [The Rust Book](https://doc.rust-lang.org/book/) -- Ask any questions in the GH issue or on the chat - -## How to submit your weekly home assignments - -Fork the repo or create a new branch in this repo. - -- Locate the chapter assignment for the GitHub issue in the course [project](https://github.com/orgs/xoriors/projects/3) or in [Issues](https://github.com/xoriors/course-rust/issues). Let's say it's [Chapter 1-3: Assignment](#5) -- Create a new folder with your GitHub account ID in [/assignments/Chapter_1-3/solutions](../assignments/Chapter_1-3/solutions), replacing `Chapter_1-3` with the corresponding week's chapters -- Add the needed files to complete the assignment -- Create a PR back to this repo for the `main` branch, titled with the assignment issue title and including `#ID`. Also, add the assignment issue ID as `#ID` in the PR's description. - In this case, the title is `Chapter 1-3: Assignment #5 `, and the id to add to the description is `#5` (this will be converted to a link to the issue by GitHub on view) -- Assign the PR to mentors for review -- Follow up on any comments and questions, pushing additional changes to the PR's branch until the PR is merged - -## Final project - -- After the course is finished, the course project will have a final project assignment. Please complete that based on your availability and ask any questions in the issue or on chat -- While working on it, please follow the steps from [How to submit your weekly home assignments](#How-to-submit-your-weekly-home-assignments) - From e287c42e5fb5223d2587532279420b0b511bd1ca Mon Sep 17 00:00:00 2001 From: Eyob <eyobIborgI@gmail.com> Date: Fri, 28 Feb 2025 18:41:03 +0300 Subject: [PATCH 08/11] Removed LICENSE and docs/ from tracking --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index f4ab2e3..aa84651 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ docs/ LICENSE docs/ +LICENSE +docs/ From d04586c9b59c5caab92504d4e2be79e34a428ada Mon Sep 17 00:00:00 2001 From: Eyob <eyobIborgI@gmail.com> Date: Fri, 28 Feb 2025 18:44:23 +0300 Subject: [PATCH 09/11] tidying up... --- .gitignore | 7 -- LICENSE | 201 +++++++++++++++++++++++++++++++++++++++++++++++ docs/mentors.md | 42 ++++++++++ docs/students.md | 28 +++++++ 4 files changed, 271 insertions(+), 7 deletions(-) create mode 100644 LICENSE create mode 100644 docs/mentors.md create mode 100644 docs/students.md diff --git a/.gitignore b/.gitignore index aa84651..7457c1e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1 @@ -LICENSE Chapter_1-3/target/ -docs/ - -LICENSE -docs/ -LICENSE -docs/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..261eeb9 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/docs/mentors.md b/docs/mentors.md new file mode 100644 index 0000000..cb2ae72 --- /dev/null +++ b/docs/mentors.md @@ -0,0 +1,42 @@ +# For the mentors to follow + +## Course setup + +1. Make sure you're on the chat or other communication channels with the rest of the team +2. At the beginning of each course, make sure the material is ready in [/docs](.) and [/assignments](../assignments) +3. [/assignments](../assignments) structure (usually this exists already. Make sure it's valid and change as needed): + A folder for each chapters group, like `Chapter_1-3` with the following structure: + - `requirements` folder: Containing a file called `Chapter_N-M_assignment.md` with the requirements and any other needed resources + - `solutions` folder: This folder will contain folders with each student's GitHub account ID, each containing submissions from each student, each with an associated PR. + Make sure this folder is empty so it will contain the solutions from this session +4. Create a new project called `course-<year>`, where `<year>` is the course's year, mostly the current year + - Create GitHub issues for each chapter and assignment, adding all needed docs with relevant links to any resources. All issue descriptions should have a link to [students.md] + - For the final project, create an assignment issue titled `Final project` and have the requirements in + +## Into the course, for each week + +1. Create a GH issue for the theory part for the given chapters for the next week session, like `Course 4` and include the table of contents of the chapters +2. Create a GH issue for the home assignment for the given chapters for the last week session, like `Course 1-3` and include the requirements and any needed files in there +3. Have the weekly call of about 2h to discuss: + - Review last week's assignment (this corresponds to the chapter from 2 weeks back); let's call it `Chapter N-2: Assignment`. After this is done, mark the issue as `Done` + - Discuss this week's chapters, `Chapter 1-3` clarifying any questions + - New home assignment for this week's chapters. Show them the GH issue for it and instruct them to create PR in there and ask their questions + - Intro to ds and show the GH issue for it, instructing the students to add their questions in there + - live coding or present building part of the course main app related to last week's chapters knowledge +4. +5. +6. After this is done, mark the issue as `Done` +7. Give the assignment for last week's chapter (this is called this week's assignment), discuss any questions, and set the issue's status to `In Progress` +8. Move to this week's chapter, `Chapter 3: <Title>`, and do a short intro. Mark the status `In Progress` for the current chapter. +9. This is where the call ends +10. Over the week, resolve any comments and questions in the week's chapter and assignment issues and on Slack +11. Record everything and save videos in `videos/Chapter_N` + +## Final project + +Follow similar assignment steps to `Into the course, for each week`, ignoring the chapter steps. + +## After the course + +- After all issues and discussions have been resolved, create a new branch called `course-<year>`, where `<year>` is the course's year +- Close the project for this year from project settings diff --git a/docs/students.md b/docs/students.md new file mode 100644 index 0000000..4730d7c --- /dev/null +++ b/docs/students.md @@ -0,0 +1,28 @@ +# For students to follow + +1. Fork this repo and do all your commits in there +2. Make sure you're on the chat or other communication channels with the rest of the team + +## Reading the weekly chapter + +- Locate the chapter GitHub issue in the course [project](https://github.com/orgs/xoriors/projects/3) or in [Issues](https://github.com/xoriors/course-rust/issues). Let's say it's the [Chapter 1-3](#4) +- Read the corresponding week number chapter from [The Rust Book](https://doc.rust-lang.org/book/) +- Ask any questions in the GH issue or on the chat + +## How to submit your weekly home assignments + +Fork the repo or create a new branch in this repo. + +- Locate the chapter assignment for the GitHub issue in the course [project](https://github.com/orgs/xoriors/projects/3) or in [Issues](https://github.com/xoriors/course-rust/issues). Let's say it's [Chapter 1-3: Assignment](#5) +- Create a new folder with your GitHub account ID in [/assignments/Chapter_1-3/solutions](../assignments/Chapter_1-3/solutions), replacing `Chapter_1-3` with the corresponding week's chapters +- Add the needed files to complete the assignment +- Create a PR back to this repo for the `main` branch, titled with the assignment issue title and including `#ID`. Also, add the assignment issue ID as `#ID` in the PR's description. + In this case, the title is `Chapter 1-3: Assignment #5 `, and the id to add to the description is `#5` (this will be converted to a link to the issue by GitHub on view) +- Assign the PR to mentors for review +- Follow up on any comments and questions, pushing additional changes to the PR's branch until the PR is merged + +## Final project + +- After the course is finished, the course project will have a final project assignment. Please complete that based on your availability and ask any questions in the issue or on chat +- While working on it, please follow the steps from [How to submit your weekly home assignments](#How-to-submit-your-weekly-home-assignments) + From cbf30cee14e5d69f89c57fe042dcd148d19cd6cc Mon Sep 17 00:00:00 2001 From: Eyob <eyobIborgI@gmail.com> Date: Fri, 28 Feb 2025 23:05:22 +0300 Subject: [PATCH 10/11] Added criterion benchmarks for both and generated graphs comparing the two --- Chapter_1-3/Cargo.lock | 549 ++++++++++++++++++++++- Chapter_1-3/Cargo.toml | 8 +- Chapter_1-3/benches/my_benchmark/main.rs | 40 +- Chapter_1-3/src/lib.rs | 3 + 4 files changed, 585 insertions(+), 15 deletions(-) create mode 100644 Chapter_1-3/src/lib.rs diff --git a/Chapter_1-3/Cargo.lock b/Chapter_1-3/Cargo.lock index d8a0059..1657d0a 100644 --- a/Chapter_1-3/Cargo.lock +++ b/Chapter_1-3/Cargo.lock @@ -2,24 +2,203 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +[[package]] +name = "bumpalo" +version = "3.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" + [[package]] name = "byteorder" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "cast" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" + [[package]] name = "cfg-if" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chapter_1-3" +version = "0.1.0" +dependencies = [ + "criterion", + "rand", +] + +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "bitflags 1.3.2", + "clap_lex", + "indexmap", + "textwrap", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "criterion" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb" +dependencies = [ + "anes", + "atty", + "cast", + "ciborium", + "clap", + "criterion-plot", + "itertools", + "lazy_static", + "num-traits", + "oorandom", + "plotters", + "rayon", + "regex", + "serde", + "serde_derive", + "serde_json", + "tinytemplate", + "walkdir", +] + +[[package]] +name = "criterion-plot" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1" +dependencies = [ + "cast", + "itertools", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" + +[[package]] +name = "crunchy" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" + +[[package]] +name = "either" +version = "1.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7914353092ddf589ad78f25c5c1c21b7f80b0ff8621e7c814c3485b5306da9d" + [[package]] name = "getrandom" version = "0.3.1" @@ -32,6 +211,72 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "half" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dd08c532ae367adf81c312a4580bc67f1d0fe8bc9c460520283f4c0ff277888" +dependencies = [ + "cfg-if", + "crunchy", +] + +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" + +[[package]] +name = "js-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f" +dependencies = [ + "once_cell", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.169" @@ -39,10 +284,70 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] -name = "noob" -version = "0.1.0" +name = "log" +version = "0.4.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ - "rand", + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + +[[package]] +name = "oorandom" +version = "11.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" + +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + +[[package]] +name = "plotters" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747" +dependencies = [ + "num-traits", + "plotters-backend", + "plotters-svg", + "wasm-bindgen", + "web-sys", +] + +[[package]] +name = "plotters-backend" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a" + +[[package]] +name = "plotters-svg" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670" +dependencies = [ + "plotters-backend", ] [[package]] @@ -103,6 +408,108 @@ dependencies = [ "zerocopy 0.8.17", ] +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" + +[[package]] +name = "rustversion" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" + +[[package]] +name = "ryu" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.218" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.218" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + [[package]] name = "syn" version = "2.0.98" @@ -114,12 +521,38 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "textwrap" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" + +[[package]] +name = "tinytemplate" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc" +dependencies = [ + "serde", + "serde_json", +] + [[package]] name = "unicode-ident" version = "1.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "wasi" version = "0.13.3+wasi-0.2.2" @@ -129,6 +562,114 @@ dependencies = [ "wit-bindgen-rt", ] +[[package]] +name = "wasm-bindgen" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6" +dependencies = [ + "bumpalo", + "log", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.77" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.59.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -199,7 +740,7 @@ version = "0.33.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" dependencies = [ - "bitflags", + "bitflags 2.8.0", ] [[package]] diff --git a/Chapter_1-3/Cargo.toml b/Chapter_1-3/Cargo.toml index c9e5a79..3d3be2e 100644 --- a/Chapter_1-3/Cargo.toml +++ b/Chapter_1-3/Cargo.toml @@ -1,8 +1,14 @@ [package] -name = "noob" +name = "chapter_1-3" version = "0.1.0" edition = "2021" [dependencies] rand = "0.9.0" +[dev-dependencies] +criterion = { version = "0.4", features = ["html_reports"] } + +[[bench]] +name = "my_benchmark" +harness = false diff --git a/Chapter_1-3/benches/my_benchmark/main.rs b/Chapter_1-3/benches/my_benchmark/main.rs index e459ec1..2edef20 100644 --- a/Chapter_1-3/benches/my_benchmark/main.rs +++ b/Chapter_1-3/benches/my_benchmark/main.rs @@ -1,16 +1,36 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId}; +use chapter_1_3::weighted::wei_dist::weighted_distribution; +use chapter_1_3::uniform::uni_dist::uniform_distribution; -fn fibonacci(n: u64) -> u64 { - match n { - 0 => 1, - 1 => 1, - n => fibonacci(n-1) + fibonacci(n-2), +fn bench_distributions(c: &mut Criterion) { + let mut group = c.benchmark_group("Distribution Comparison"); + + let mut extracted_array_u64 = [123, 235, 633, 677, 888, 324]; + let mut extracted_array_f32 = [ 0.54633904, 0.506454, 0.10968602, 0.24843967, 0.4981829, 0.43807256]; + let matched = [false; 100]; + let success = 0; + let user_input = 6; + + for &loop_amount in &[10u64, 100u64, 1000u64] { + group.bench_with_input( + BenchmarkId::new("WEIGHTED-DISTRIBUTION", loop_amount), + &loop_amount, + |b, &loop_amount| { + b.iter(|| weighted_distribution(loop_amount, &mut extracted_array_u64, matched)) + }, + ); + + group.bench_with_input( + BenchmarkId::new("UNIFORM-DISTRIBUTION", loop_amount), + &loop_amount, + |b, &loop_amount| { + b.iter(|| uniform_distribution(loop_amount, user_input, &mut extracted_array_f32, success, matched)) + }, + ); } -} -fn criterion_benchmark(c: &mut Criterion) { - c.bench_function("Benchmarking Loops: ", |b| b.iter(|| fibonacci(black_box(20)))); + group.finish(); } -criterion_group!(benches, criterion_benchmark); +criterion_group!(benches, bench_distributions); criterion_main!(benches); diff --git a/Chapter_1-3/src/lib.rs b/Chapter_1-3/src/lib.rs new file mode 100644 index 0000000..ecf9846 --- /dev/null +++ b/Chapter_1-3/src/lib.rs @@ -0,0 +1,3 @@ +pub mod uniform; +pub mod weighted; + From 844b40aa3145c9842feff171827d333c5282a294 Mon Sep 17 00:00:00 2001 From: Eyob <eyobIborgI@gmail.com> Date: Fri, 28 Feb 2025 23:50:42 +0300 Subject: [PATCH 11/11] Added the benchmarks using criterion crate --- Chapter_1-3/benches/my_benchmark/main.rs | 31 +++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/Chapter_1-3/benches/my_benchmark/main.rs b/Chapter_1-3/benches/my_benchmark/main.rs index 2edef20..8578f26 100644 --- a/Chapter_1-3/benches/my_benchmark/main.rs +++ b/Chapter_1-3/benches/my_benchmark/main.rs @@ -1,30 +1,39 @@ use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId}; use chapter_1_3::weighted::wei_dist::weighted_distribution; use chapter_1_3::uniform::uni_dist::uniform_distribution; +use rand::random_range; fn bench_distributions(c: &mut Criterion) { let mut group = c.benchmark_group("Distribution Comparison"); - let mut extracted_array_u64 = [123, 235, 633, 677, 888, 324]; - let mut extracted_array_f32 = [ 0.54633904, 0.506454, 0.10968602, 0.24843967, 0.4981829, 0.43807256]; let matched = [false; 100]; let success = 0; - let user_input = 6; + let loop_amount = 0; - for &loop_amount in &[10u64, 100u64, 1000u64] { + for &array_size in &[4, 8, 12] { + let mut extracted_array_u64 = vec![0; array_size]; // Adjust size + let mut extracted_array_f32 = vec![0.0; array_size]; // Adjust size + for i in extracted_array_u64.iter_mut() { + let generated_u64 = random_range(0..=1000); + *i = generated_u64 + }; + for i in extracted_array_f32.iter_mut() { + let generated_f32 = random_range(0.0..=1.0); + *i = generated_f32 + }; group.bench_with_input( - BenchmarkId::new("WEIGHTED-DISTRIBUTION", loop_amount), - &loop_amount, - |b, &loop_amount| { + BenchmarkId::new("WEIGHTED-DISTRIBUTION", array_size), + &array_size, + |b, &_| { b.iter(|| weighted_distribution(loop_amount, &mut extracted_array_u64, matched)) }, ); group.bench_with_input( - BenchmarkId::new("UNIFORM-DISTRIBUTION", loop_amount), - &loop_amount, - |b, &loop_amount| { - b.iter(|| uniform_distribution(loop_amount, user_input, &mut extracted_array_f32, success, matched)) + BenchmarkId::new("UNIFORM-DISTRIBUTION", array_size), + &array_size, + |b, &array_size| { + b.iter(|| uniform_distribution(loop_amount, array_size as u16, &mut extracted_array_f32, success, matched)) }, ); }