From 4759b3673a50099ca1dde5662833fa4f8679b653 Mon Sep 17 00:00:00 2001 From: omi Date: Sun, 5 Oct 2025 04:12:39 +0530 Subject: [PATCH 1/7] added job sequening --- src/greedy/job_sequencing.rs | 88 ++++++++++++++++++++++++++++++++++++ src/greedy/mod.rs | 2 + 2 files changed, 90 insertions(+) create mode 100644 src/greedy/job_sequencing.rs diff --git a/src/greedy/job_sequencing.rs b/src/greedy/job_sequencing.rs new file mode 100644 index 00000000000..1b35ddbdbe2 --- /dev/null +++ b/src/greedy/job_sequencing.rs @@ -0,0 +1,88 @@ +use std::collections::HashMap; + +#[derive(Debug, Clone)] +pub struct Job { + pub id: String, + pub deadline: usize, + pub profit: i32, +} + +pub fn job_sequencing(mut jobs: Vec) -> (Vec, i32) { + if jobs.is_empty() { + return (Vec::new(), 0); + } + + // Sort by profit (descending) + jobs.sort_by(|a, b| b.profit.cmp(&a.profit)); + + // Find the maximum deadline + let max_deadline = jobs.iter().map(|j| j.deadline).max().unwrap(); + + // Track time slots that are filled + let mut slots = vec![false; max_deadline]; + let mut scheduled = Vec::new(); + let mut total_profit = 0; + + // Schedule each job in the latest available slot before its deadline is reached + for job in jobs { + for slot in (0..job.deadline.min(max_deadline)).rev() { + if !slots[slot] { + slots[slot] = true; + scheduled.push(job.clone()); + total_profit += job.profit; + break; + } + } + } + + (scheduled, total_profit) +} + +// test algorithm +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basic() { + let jobs = vec![ + Job { + id: "J1".to_string(), + deadline: 2, + profit: 100, + }, + Job { + id: "J2".to_string(), + deadline: 1, + profit: 19, + }, + Job { + id: "J3".to_string(), + deadline: 2, + profit: 27, + }, + Job { + id: "J4".to_string(), + deadline: 1, + profit: 25, + }, + Job { + id: "J5".to_string(), + deadline: 3, + profit: 15, + }, + ]; + + let (scheduled, profit) = job_sequencing(jobs); + + assert_eq!(profit, 142); + assert_eq!(scheduled.len(), 3); + } + + #[test] + fn test_empty() { + let (scheduled, profit) = job_sequencing(vec![]); + assert_eq!(profit, 0); + assert!(scheduled.is_empty()); + } +} diff --git a/src/greedy/mod.rs b/src/greedy/mod.rs index e718c149f42..f8bbb655119 100644 --- a/src/greedy/mod.rs +++ b/src/greedy/mod.rs @@ -1,3 +1,5 @@ +mod job_sequencing; mod stable_matching; +pub use self::job_sequencing::job_sequencing; pub use self::stable_matching::stable_matching; From 1cfe1b9ef5a37037ab71577de87a1615b813a551 Mon Sep 17 00:00:00 2001 From: omi Date: Sun, 5 Oct 2025 04:16:34 +0530 Subject: [PATCH 2/7] added job sequencing implementation and removed cargo clippy warnings --- src/greedy/job_sequencing.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/greedy/job_sequencing.rs b/src/greedy/job_sequencing.rs index 1b35ddbdbe2..942bbb4f639 100644 --- a/src/greedy/job_sequencing.rs +++ b/src/greedy/job_sequencing.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - #[derive(Debug, Clone)] pub struct Job { pub id: String, From ff40535dd67b527ebbf3a8a8cf36177a75180160 Mon Sep 17 00:00:00 2001 From: omi Date: Sun, 5 Oct 2025 04:27:44 +0530 Subject: [PATCH 3/7] added 'Job Sequencing' to DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 564a7813807..5195a9cdc86 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -157,6 +157,7 @@ * [Two Satisfiability](https://github.com/TheAlgorithms/Rust/blob/master/src/graph/two_satisfiability.rs) * Greedy * [Stable Matching](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/stable_matching.rs) + * [Job Sequencing](https://github.com/TheAlgorithms/Rust/blob/master/src/greedy/job_sequencing.rs) * [Lib](https://github.com/TheAlgorithms/Rust/blob/master/src/lib.rs) * Machine Learning * [Cholesky](https://github.com/TheAlgorithms/Rust/blob/master/src/machine_learning/cholesky.rs) From 23ac809d33117f78513ccfd708ec205eac3e6a87 Mon Sep 17 00:00:00 2001 From: omi Date: Mon, 6 Oct 2025 22:00:03 +0530 Subject: [PATCH 4/7] Changed all '%' operations to is_multiple_of() to remove clippy warnings --- src/ciphers/aes.rs | 4 ++-- src/ciphers/blake2b.rs | 2 +- src/ciphers/sha3.rs | 2 +- src/data_structures/probabilistic/bloom_filter.rs | 3 ++- src/general/permutations/heap.rs | 3 ++- src/math/aliquot_sum.rs | 4 ++-- src/math/collatz_sequence.rs | 2 +- src/math/factors.rs | 3 ++- src/math/interquartile_range.rs | 6 ++++-- src/math/perfect_numbers.rs | 3 ++- src/math/pollard_rho.rs | 6 ++++-- src/math/prime_check.rs | 3 ++- src/math/prime_factors.rs | 3 ++- src/math/quadratic_residue.rs | 3 ++- src/number_theory/euler_totient.rs | 6 ++++-- 15 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/ciphers/aes.rs b/src/ciphers/aes.rs index 5d2eb98ece0..4616966d5e4 100644 --- a/src/ciphers/aes.rs +++ b/src/ciphers/aes.rs @@ -318,7 +318,7 @@ fn key_expansion(init_key: &[Byte], num_rounds: usize) -> Vec { } fn add_round_key(data: &mut [Byte], round_key: &[Byte]) { - assert!(data.len() % AES_BLOCK_SIZE == 0 && round_key.len() == AES_BLOCK_SIZE); + assert!(data.len().is_multiple_of(AES_BLOCK_SIZE) && round_key.len() == AES_BLOCK_SIZE); let num_blocks = data.len() / AES_BLOCK_SIZE; data.iter_mut() .zip(round_key.repeat(num_blocks)) @@ -348,7 +348,7 @@ fn mix_column_blocks(data: &mut [Byte], mode: AesMode) { } fn padding(data: &[T], block_size: usize) -> Vec { - if data.len() % block_size == 0 { + if data.len().is_multiple_of(block_size) { Vec::from(data) } else { let num_blocks = data.len() / block_size + 1; diff --git a/src/ciphers/blake2b.rs b/src/ciphers/blake2b.rs index c28486489d6..ab51fdfddcc 100644 --- a/src/ciphers/blake2b.rs +++ b/src/ciphers/blake2b.rs @@ -55,7 +55,7 @@ fn add(a: &mut Word, b: Word) { #[inline] const fn ceil(dividend: usize, divisor: usize) -> usize { - (dividend / divisor) + ((dividend % divisor != 0) as usize) + (dividend / divisor) + (!(dividend.is_multiple_of(divisor)) as usize) } fn g(v: &mut [Word; 16], a: usize, b: usize, c: usize, d: usize, x: Word, y: Word) { diff --git a/src/ciphers/sha3.rs b/src/ciphers/sha3.rs index f3791214f3f..ca827ac3550 100644 --- a/src/ciphers/sha3.rs +++ b/src/ciphers/sha3.rs @@ -271,7 +271,7 @@ fn h2b(h: &[u8], n: usize) -> Vec { } fn b2h(s: &[bool]) -> Vec { - let m = if s.len() % U8BITS != 0 { + let m = if !(s.len().is_multiple_of(U8BITS)) { (s.len() / 8) + 1 } else { s.len() / 8 diff --git a/src/data_structures/probabilistic/bloom_filter.rs b/src/data_structures/probabilistic/bloom_filter.rs index cbafa7f3cc8..c32b980cf6f 100644 --- a/src/data_structures/probabilistic/bloom_filter.rs +++ b/src/data_structures/probabilistic/bloom_filter.rs @@ -109,7 +109,8 @@ pub struct MultiBinaryBloomFilter { impl MultiBinaryBloomFilter { pub fn with_dimensions(filter_size: usize, hash_count: usize) -> Self { - let bytes_count = filter_size / 8 + usize::from(filter_size % 8 > 0); // we need 8 times less entries in the array, since we are using bytes. Careful that we have at least one element though + // changed the % operation to .is_multiple_of() + let bytes_count = filter_size / 8 + usize::from(!(filter_size.is_multiple_of(8))); // we need 8 times less entries in the array, since we are using bytes. Careful that we have at least one element though Self { filter_size, bytes: vec![0; bytes_count], diff --git a/src/general/permutations/heap.rs b/src/general/permutations/heap.rs index b1c3b38d198..4b60e3f1ec9 100644 --- a/src/general/permutations/heap.rs +++ b/src/general/permutations/heap.rs @@ -23,7 +23,8 @@ fn heap_recurse(arr: &mut [T], k: usize, collector: &mut Vec u64 { if number == 0 { panic!("Input has to be positive.") } - - (1..=number / 2).filter(|&d| number % d == 0).sum() + // changed % operator to .is_multiple_of() + (1..=number / 2).filter(|&d| number.is_multiple_of(d)).sum() } #[cfg(test)] diff --git a/src/math/collatz_sequence.rs b/src/math/collatz_sequence.rs index 32400cc5baa..b307d66469a 100644 --- a/src/math/collatz_sequence.rs +++ b/src/math/collatz_sequence.rs @@ -6,7 +6,7 @@ pub fn sequence(mut n: usize) -> Option> { let mut list: Vec = vec![]; while n != 1 { list.push(n); - if n % 2 == 0 { + if n.is_multiple_of(2) { n /= 2; } else { n = 3 * n + 1; diff --git a/src/math/factors.rs b/src/math/factors.rs index 5131642dffa..91721f6772d 100644 --- a/src/math/factors.rs +++ b/src/math/factors.rs @@ -7,7 +7,8 @@ pub fn factors(number: u64) -> Vec { let mut factors: Vec = Vec::new(); for i in 1..=((number as f64).sqrt() as u64) { - if number % i == 0 { + // changed % operation to .is_multiple_of() + if number.is_multiple_of(i) { factors.push(i); if i != number / i { factors.push(number / i); diff --git a/src/math/interquartile_range.rs b/src/math/interquartile_range.rs index fed92b77709..fe6585a70a2 100644 --- a/src/math/interquartile_range.rs +++ b/src/math/interquartile_range.rs @@ -12,7 +12,8 @@ pub fn find_median(numbers: &[f64]) -> f64 { let length = numbers.len(); let mid = length / 2; - if length % 2 == 0 { + // changed % operation to .is_multiple_of() + if length.is_multiple_of(2) { f64::midpoint(numbers[mid - 1], numbers[mid]) } else { numbers[mid] @@ -29,7 +30,8 @@ pub fn interquartile_range(numbers: &[f64]) -> f64 { let length = numbers.len(); let mid = length / 2; - let (q1, q3) = if length % 2 == 0 { + // changed % operation to is_multiple_of() + let (q1, q3) = if length.is_multiple_of(2) { let first_half = &numbers[0..mid]; let second_half = &numbers[mid..length]; (find_median(first_half), find_median(second_half)) diff --git a/src/math/perfect_numbers.rs b/src/math/perfect_numbers.rs index 0d819d2b2f1..99ad576dbe9 100644 --- a/src/math/perfect_numbers.rs +++ b/src/math/perfect_numbers.rs @@ -2,7 +2,8 @@ pub fn is_perfect_number(num: usize) -> bool { let mut sum = 0; for i in 1..num - 1 { - if num % i == 0 { + // changed % operation to is_multiple_of() + if num.is_multiple_of(i) { sum += i; } } diff --git a/src/math/pollard_rho.rs b/src/math/pollard_rho.rs index 1ba7481e989..c6a8a27bd2f 100644 --- a/src/math/pollard_rho.rs +++ b/src/math/pollard_rho.rs @@ -137,7 +137,8 @@ pub fn pollard_rho_get_one_factor(number: u64, seed: &mut u32, check_is_prime: b fn get_small_factors(mut number: u64, primes: &[usize]) -> (u64, Vec) { let mut result: Vec = Vec::new(); for p in primes { - while (number % *p as u64) == 0 { + // changed % operation to is_multiple_of() + while number.is_multiple_of(*p as u64) { number /= *p as u64; result.push(*p as u64); } @@ -201,7 +202,8 @@ mod test { use super::*; fn check_is_proper_factor(number: u64, factor: u64) -> bool { - factor > 1 && factor < number && ((number % factor) == 0) + // changed % operation to is_multiple_of() + factor > 1 && factor < number && (number.is_multiple_of(factor)) } fn check_factorization(number: u64, factors: &[u64]) -> bool { diff --git a/src/math/prime_check.rs b/src/math/prime_check.rs index 4902a65dbf7..3e04f2e22a9 100644 --- a/src/math/prime_check.rs +++ b/src/math/prime_check.rs @@ -1,7 +1,8 @@ pub fn prime_check(num: usize) -> bool { if (num > 1) & (num < 4) { return true; - } else if (num < 2) || (num % 2 == 0) { + // changed % operation to is_multiple_of() + } else if (num < 2) || (num.is_multiple_of(2)) { return false; } diff --git a/src/math/prime_factors.rs b/src/math/prime_factors.rs index 7b89b09c9b8..09cabc41a57 100644 --- a/src/math/prime_factors.rs +++ b/src/math/prime_factors.rs @@ -5,7 +5,8 @@ pub fn prime_factors(n: u64) -> Vec { let mut n = n; let mut factors = Vec::new(); while i * i <= n { - if n % i != 0 { + // changed % operation to is_multiple_of() + if !(n.is_multiple_of(i)) { if i != 2 { i += 1; } diff --git a/src/math/quadratic_residue.rs b/src/math/quadratic_residue.rs index e3f2e6b819b..2f20740348a 100644 --- a/src/math/quadratic_residue.rs +++ b/src/math/quadratic_residue.rs @@ -78,7 +78,8 @@ fn is_residue(x: u64, modulus: u64) -> bool { /// /// pub fn legendre_symbol(a: u64, odd_prime: u64) -> i64 { - debug_assert!(odd_prime % 2 != 0, "prime must be odd"); + // changed % operation to is_multiple_of() + debug_assert!(!(odd_prime.is_multiple_of(2)), "prime must be odd"); if a == 0 { 0 } else if is_residue(a, odd_prime) { diff --git a/src/number_theory/euler_totient.rs b/src/number_theory/euler_totient.rs index 69c0694a335..1944b98a128 100644 --- a/src/number_theory/euler_totient.rs +++ b/src/number_theory/euler_totient.rs @@ -6,10 +6,12 @@ pub fn euler_totient(n: u64) -> u64 { // Find all prime factors and apply formula while p * p <= num { // Check if p is a divisor of n - if num % p == 0 { + // Changed % operation to is_multiple_of() + if num.is_multiple_of(p) { // If yes, then it is a prime factor // Apply the formula: result = result * (1 - 1/p) - while num % p == 0 { + // Changed % operation to is_multiple_of() + while num.is_multiple_of(p) { num /= p; } result -= result / p; From d46dddced5cb6e57fd3f9c46a5afe1fd7ad24ca5 Mon Sep 17 00:00:00 2001 From: omi Date: Mon, 6 Oct 2025 22:03:31 +0530 Subject: [PATCH 5/7] removed the redundant paren from kl_diverengence function --- src/machine_learning/loss_function/kl_divergence_loss.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/machine_learning/loss_function/kl_divergence_loss.rs b/src/machine_learning/loss_function/kl_divergence_loss.rs index f477607b20f..918bdc338e6 100644 --- a/src/machine_learning/loss_function/kl_divergence_loss.rs +++ b/src/machine_learning/loss_function/kl_divergence_loss.rs @@ -16,7 +16,7 @@ pub fn kld_loss(actual: &[f64], predicted: &[f64]) -> f64 { let loss: f64 = actual .iter() .zip(predicted.iter()) - .map(|(&a, &p)| ((a + eps) * ((a + eps) / (p + eps)).ln())) + .map(|(&a, &p)| (a + eps) * ((a + eps) / (p + eps)).ln()) .sum(); loss } From 106fdd7ea9ade35e303c6b0f128a1aa5d72fad16 Mon Sep 17 00:00:00 2001 From: omi Date: Mon, 6 Oct 2025 22:20:12 +0530 Subject: [PATCH 6/7] Some additional changes to ignore clippy warnings --- src/ciphers/sha3.rs | 7 ++++--- src/math/prime_check.rs | 3 ++- src/math/prime_factors.rs | 9 +++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/ciphers/sha3.rs b/src/ciphers/sha3.rs index ca827ac3550..bdb92ea009d 100644 --- a/src/ciphers/sha3.rs +++ b/src/ciphers/sha3.rs @@ -271,10 +271,11 @@ fn h2b(h: &[u8], n: usize) -> Vec { } fn b2h(s: &[bool]) -> Vec { - let m = if !(s.len().is_multiple_of(U8BITS)) { - (s.len() / 8) + 1 - } else { + // changed this function a bit to stop the clippy warnings + let m = if s.len().is_multiple_of(U8BITS) { s.len() / 8 + } else { + (s.len() / 8) + 1 }; let mut bytes = vec![0u8; m]; diff --git a/src/math/prime_check.rs b/src/math/prime_check.rs index 3e04f2e22a9..71b58bff2b8 100644 --- a/src/math/prime_check.rs +++ b/src/math/prime_check.rs @@ -8,7 +8,8 @@ pub fn prime_check(num: usize) -> bool { let stop: usize = (num as f64).sqrt() as usize + 1; for i in (3..stop).step_by(2) { - if num % i == 0 { + // changed % operation to is_multiple_of() + if num.is_multiple_of(i) { return false; } } diff --git a/src/math/prime_factors.rs b/src/math/prime_factors.rs index 09cabc41a57..7e85d44103e 100644 --- a/src/math/prime_factors.rs +++ b/src/math/prime_factors.rs @@ -6,14 +6,15 @@ pub fn prime_factors(n: u64) -> Vec { let mut factors = Vec::new(); while i * i <= n { // changed % operation to is_multiple_of() - if !(n.is_multiple_of(i)) { + // changed function a bit to remove clippy warnings + if n.is_multiple_of(i) { + n /= i; + factors.push(i); + } else { if i != 2 { i += 1; } i += 1; - } else { - n /= i; - factors.push(i); } } if n > 1 { From 18078f9b3851cdb0ff8ec4bc6487f8bcaf389297 Mon Sep 17 00:00:00 2001 From: omi Date: Mon, 6 Oct 2025 22:29:35 +0530 Subject: [PATCH 7/7] Rounding out the changes --- src/ciphers/diffie_hellman.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/ciphers/diffie_hellman.rs b/src/ciphers/diffie_hellman.rs index 3cfe53802bb..31a2e0e5996 100644 --- a/src/ciphers/diffie_hellman.rs +++ b/src/ciphers/diffie_hellman.rs @@ -228,11 +228,8 @@ impl DiffieHellman { // Both parties now have the same shared secret key s which can be used for encryption or authentication. pub fn new(group: Option) -> Self { - let mut _group: u8 = 14; - if let Some(x) = group { - _group = x; - } - + // made this idiomatic according to clippy's suggestions + let _group = group.unwrap_or(14); if !PRIMES.contains_key(&_group) { panic!("group not in primes") }