From 6bbf891052985cb089d557095572872a2eb3f0b0 Mon Sep 17 00:00:00 2001 From: rzmk Date: Fri, 22 Sep 2023 10:16:17 -0400 Subject: [PATCH] feat: use add method for get_multiples_in_range, simplify docs + no fn main --- ladderz/src/lib.rs | 91 ++++--------------------------- ladderz/src/pre_algebra/unit1.rs | 51 +++++++---------- notebooks/pre-algebra/unit1.ipynb | 7 +-- 3 files changed, 35 insertions(+), 114 deletions(-) diff --git a/ladderz/src/lib.rs b/ladderz/src/lib.rs index 9366bcc..4bab4c8 100644 --- a/ladderz/src/lib.rs +++ b/ladderz/src/lib.rs @@ -2,95 +2,30 @@ //! //! Implementations of mathematical and technical concepts in Rust. //! -//! # Subjects +//! # Installing the crate //! -//! The modules for currently supported subjects are: -//! -//! - [pre_algebra] - Various pre-algebra implementations including factor pairs, factors, multiples, and more. -//! -//! # Example -//! -//! Here's an example of using the `ladderz` crate to get the factors and factor pairs of a positive integer in sorted order. -//! We'll assume you're using Bash as your terminal. -//! -//! First let's create a new Rust project and change into the project directory: -//! -//! ```bash -//! cargo new my_ladderz_project -//! cd my_ladderz_project -//! ``` -//! -//! Then let's add the following to `Cargo.toml` under the `[dependencies]` section: +//! To add the crate to your project, add the following dependency under your `[dependencies]` section in your `Cargo.toml`: //! //! ```toml //! ladderz = { git = "https://github.com/rzmk/ladderz", branch = "main" } //! ``` //! -//! Now in `src/main.rs` let's replace the contents with the following code: +//! # Example //! //! ```rust -//! use ladderz::pre_algebra::{get_factors, get_factor_pairs}; -//! use std::env; -//! -//! fn main() { -//! // Get user input as a Vec -//! let args: Vec = env::args().collect(); -//! -//! // Check if input was provided -//! match args.get(1) { -//! Some(_) => { -//! match args[1].parse::() { -//! // Handle input that can be parsed as a u32 -//! Ok(x) => { -//! // Convert the HashSet of factors of input x to a sorted Vec -//! let mut factors: Vec = get_factors(x).into_iter().collect::>(); -//! factors.sort(); -//! -//! // Convert the HashSet of factor pairs of input x to a sorted Vec -//! let mut factor_pairs: Vec<(u32, u32)> = -//! get_factor_pairs(x).into_iter().collect::>(); -//! factor_pairs.sort(); -//! -//! // Print the results -//! println!("List of factors of {:?}: {:?}", x, factors); -//! println!("List of factor pairs of {:?}: {:?}", x, factor_pairs); -//! } -//! // Handle input that can't be parsed as a u32 -//! Err(e) => println!("Error parsing input: {e}"), -//! } -//! } -//! None => println!("No input provided."), -//! } -//! } +//!use ladderz::pre_algebra::get_factors; //! +//!let x: u32 = 10; +//!println!("The factors of {x} are {:?}.", get_factors(x)); //! ``` //! -//! Now let's build the project's binary file so we can run it from the command line: -//! -//! ```bash -//! cargo build --release -//! ``` -//! -//! Our runnable binary file should be located at the local path `./target/release/my_ladders_project` (or `./target/release/my_ladders_project.exe` for Windows). Let's run it with the positive integer `12` as input: -//! -//! ```bash -//! ./target/release/my_ladderz_project 12 -//! ``` -//! -//! If you have a `.exe` file instead, you can run it with: -//! -//! ```bash -//! ./target/release/my_ladderz_project.exe 12 -//! ``` -//! -//! The printed output should be: -//! //! ```console -//! List of factors of 12: [1, 2, 3, 4, 6, 12] -//! List of factor pairs of 12: [(1, 12), (2, 6), (3, 4)] +//! The factors of 10 are {1, 5, 2, 10}. //! ``` //! -//! Great! We've successfully used the `ladderz` crate to get the factors and factor pairs of a positive integer in sorted order. +//! For a more detailed example of how to use the `ladderz` crate, please see the [example on GitHub](https://github.com/rzmk/ladderz#example). +//! +//! Choose a module to view its available functions. /// Various pre-algebra implementations including factor pairs, factors, multiples, and more. /// @@ -99,10 +34,8 @@ /// ```rust /// use ladderz::pre_algebra::get_factors; /// -/// fn main() { -/// let x: u32 = 10; -/// println!("The factors of {x} are {:?}.", get_factors(x)); -/// } +/// let x: u32 = 10; +/// println!("The factors of {x} are {:?}.", get_factors(x)); /// ``` /// /// ```console diff --git a/ladderz/src/pre_algebra/unit1.rs b/ladderz/src/pre_algebra/unit1.rs index fbcfa14..d813d24 100644 --- a/ladderz/src/pre_algebra/unit1.rs +++ b/ladderz/src/pre_algebra/unit1.rs @@ -14,11 +14,9 @@ use std::collections::HashSet; /// use std::collections::HashSet; /// use ladderz::pre_algebra::get_factor_pairs; /// -/// fn main() { -/// let result_pairs = get_factor_pairs(12); -/// let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into(); -/// assert_eq!(result_pairs, expected_pairs); -/// } +/// let result_pairs = get_factor_pairs(12); +/// let expected_pairs: HashSet<(u32, u32)> = [(1, 12), (2, 6), (3, 4)].into(); +/// assert_eq!(result_pairs, expected_pairs); /// ``` /// /// # Note @@ -57,11 +55,9 @@ pub fn get_factor_pairs(n: u32) -> HashSet<(u32, u32)> { /// use std::collections::HashSet; /// use ladderz::pre_algebra::get_factors; /// -/// fn main() { -/// let result_factors = get_factors(16); -/// let expected_factors: HashSet = [1, 2, 4, 8, 16].into(); -/// assert_eq!(result_factors, expected_factors); -/// } +/// let result_factors = get_factors(16); +/// let expected_factors: HashSet = [1, 2, 4, 8, 16].into(); +/// assert_eq!(result_factors, expected_factors); /// ``` /// /// # Note @@ -89,10 +85,8 @@ pub fn get_factors(n: u32) -> HashSet { /// ```rust /// use ladderz::pre_algebra::is_factor; /// -/// fn main() { -/// assert!(is_factor(2, 16)); // 2 is a factor of 16 -/// assert!(!is_factor(3, 16)); // 3 is not a factor of 16 -/// } +/// assert!(is_factor(2, 16)); // 2 is a factor of 16 +/// assert!(!is_factor(3, 16)); // 3 is not a factor of 16 /// ``` /// /// # Note @@ -112,10 +106,8 @@ pub fn is_factor(x: u32, y: u32) -> bool { /// ```rust /// use ladderz::pre_algebra::is_multiple; /// -/// fn main() { -/// assert!(is_multiple(16, 2)); // 16 is a multiple of 2 -/// assert!(!is_multiple(16, 3)); // 16 is not a multiple of 3 -/// } +/// assert!(is_multiple(16, 2)); // 16 is a multiple of 2 +/// assert!(!is_multiple(16, 3)); // 16 is not a multiple of 3 /// ``` pub fn is_multiple(x: u32, y: u32) -> bool { x % y == 0 @@ -131,23 +123,15 @@ pub fn is_multiple(x: u32, y: u32) -> bool { /// use ladderz::pre_algebra::get_multiples_in_range; /// use std::collections::HashSet; /// -/// fn main() { -/// let result: HashSet = get_multiples_in_range(2, 10); -/// let expected: HashSet = [2, 4, 6, 8, 10].into(); -/// assert_eq!(result, expected); -/// -/// let result: HashSet = get_multiples_in_range(3, 15); -/// let expected: HashSet = [3, 6, 9, 12, 15].into(); -/// assert_eq!(result, expected); -/// } +/// let result: HashSet = get_multiples_in_range(2, 10); +/// let expected: HashSet = [2, 4, 6, 8, 10].into(); +/// assert_eq!(result, expected); /// ``` pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet { let mut multiples: HashSet = HashSet::new(); - for num in n..end + 1 { - if num % n == 0 { - multiples.insert(num); - } + for num in (n..end + 1).step_by(n as usize) { + multiples.insert(num); } multiples } @@ -209,5 +193,10 @@ mod tests { let result_2: HashSet = get_multiples_in_range(5, 34); let expected_2: HashSet = [5, 10, 15, 20, 25, 30].into(); assert_eq!(result_2, expected_2); + + // Test when the range has no multiples + let result_3: HashSet = get_multiples_in_range(7, 11); + let expected_3: HashSet = [7].into(); + assert_eq!(expected_3, result_3); } } diff --git a/notebooks/pre-algebra/unit1.ipynb b/notebooks/pre-algebra/unit1.ipynb index a20384c..83d653d 100644 --- a/notebooks/pre-algebra/unit1.ipynb +++ b/notebooks/pre-algebra/unit1.ipynb @@ -186,15 +186,14 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def get_multiples_in_range(n: int, end: int) -> set:\n", " multiples = set()\n", - " for num in range(n, end + 1):\n", - " if num % n == 0:\n", - " multiples.add(num)\n", + " for num in range(n, end + 1, n):\n", + " multiples.add(num)\n", " return multiples\n", "\n", "assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n",