mirror of
https://github.com/rzmk/ladderz.git
synced 2025-12-19 06:59:25 +00:00
feat(pre-algebra): refactor to get_multiples_in_range & add to rust lib
This commit is contained in:
parent
8ed5716a83
commit
a8261965a6
2 changed files with 66 additions and 7 deletions
|
|
@ -183,6 +183,54 @@ pub fn is_multiple(x: u32, y: u32) -> bool {
|
||||||
x % y == 0
|
x % y == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a HashSet containing all the multiples of a positive integer `n` in the range [n, end].
|
||||||
|
///
|
||||||
|
/// # Challenge
|
||||||
|
///
|
||||||
|
/// Write a program that finds all the multiples of a positive integer `n` in a given range.
|
||||||
|
///
|
||||||
|
/// # Description
|
||||||
|
///
|
||||||
|
/// Returns a HashSet containing all the multiples of a positive integer `n` in the range [n, end].
|
||||||
|
///
|
||||||
|
/// A multiple of `n` is a positive integer `num` where `num` is evenly divisible by `n` (i.e., `num % n == 0`).
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
///
|
||||||
|
/// * `n` - The positive integer for which multiples are to be found.
|
||||||
|
/// * `end` - The upper limit of the range for finding multiples.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// A HashSet containing all the multiples of `n` in the range [n, end].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// use ladderz::pre_algebra::unit1::get_multiples_in_range;
|
||||||
|
/// use std::collections::HashSet;
|
||||||
|
///
|
||||||
|
/// fn main() {
|
||||||
|
/// let result: HashSet<u32> = get_multiples_in_range(2, 10);
|
||||||
|
/// let expected: HashSet<u32> = [2, 4, 6, 8, 10].into();
|
||||||
|
/// assert_eq!(result, expected);
|
||||||
|
///
|
||||||
|
/// let result: HashSet<u32> = get_multiples_in_range(3, 15);
|
||||||
|
/// let expected: HashSet<u32> = [3, 6, 9, 12, 15].into();
|
||||||
|
/// assert_eq!(result, expected);
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
pub fn get_multiples_in_range(n: u32, end: u32) -> HashSet<u32> {
|
||||||
|
let mut multiples: HashSet<u32> = HashSet::new();
|
||||||
|
|
||||||
|
for num in n..end+1 {
|
||||||
|
if num % n == 0 {
|
||||||
|
multiples.insert(num);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
multiples
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -230,4 +278,15 @@ mod tests {
|
||||||
let expected_2: bool = is_multiple(11, 2);
|
let expected_2: bool = is_multiple(11, 2);
|
||||||
assert_eq!(result_2, expected_2);
|
assert_eq!(result_2, expected_2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_multiples_in_range() {
|
||||||
|
let result: HashSet<u32> = get_multiples_in_range(2, 20);
|
||||||
|
let expected: HashSet<u32> = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20].into();
|
||||||
|
assert_eq!(result, expected);
|
||||||
|
|
||||||
|
let result_2: HashSet<u32> = get_multiples_in_range(5, 34);
|
||||||
|
let expected_2: HashSet<u32> = [5, 10, 15, 20, 25, 30].into();
|
||||||
|
assert_eq!(result_2, expected_2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -173,11 +173,11 @@
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"**Write a program that finds all multiples of a given number $n$ up to a given limit $l$.**\n",
|
"**Write a program that finds all multiples of a positive integer `n` in the range `[n, end]`.**\n",
|
||||||
"\n",
|
"\n",
|
||||||
"- Assume that $n$ and $l$ are positive integers greater than or equal to 1 ($n, l \\in \\mathbb{Z}^+$).\n",
|
"- Assume that `n` and `end` are positive integers greater than or equal to 1.\n",
|
||||||
"\n",
|
"\n",
|
||||||
"For example for $n = 3$ and $l = 20$, the output of `get_multiples(3, 20)` may be:\n",
|
"For example for `n = 3` and `end = 20`, the output of `get_multiples_in_range(3, 20)` may be:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"```\n",
|
"```\n",
|
||||||
"{3, 6, 9, 12, 15, 18}\n",
|
"{3, 6, 9, 12, 15, 18}\n",
|
||||||
|
|
@ -190,15 +190,15 @@
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"def get_multiples(n: int, l: int) -> set:\n",
|
"def get_multiples_in_range(n: int, end: int) -> set:\n",
|
||||||
" multiples = set()\n",
|
" multiples = set()\n",
|
||||||
" for num in range(n, l + 1):\n",
|
" for num in range(n, end + 1):\n",
|
||||||
" if num % n == 0:\n",
|
" if num % n == 0:\n",
|
||||||
" multiples.add(num)\n",
|
" multiples.add(num)\n",
|
||||||
" return multiples\n",
|
" return multiples\n",
|
||||||
"\n",
|
"\n",
|
||||||
"assert get_multiples(3, 20) == {3, 6, 9, 12, 15, 18}\n",
|
"assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n",
|
||||||
"assert get_multiples(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}"
|
"assert get_multiples_in_range(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue