diff --git a/Cargo.toml b/Cargo.toml index f6afbf3d3ab..5d13498dc4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,7 +53,6 @@ missing_panics_doc = { level = "allow", priority = 1 } module_name_repetitions = { level = "allow", priority = 1 } must_use_candidate = { level = "allow", priority = 1 } needless_pass_by_value = { level = "allow", priority = 1 } -range_plus_one = { level = "allow", priority = 1 } redundant_closure_for_method_calls = { level = "allow", priority = 1 } return_self_not_must_use = { level = "allow", priority = 1 } semicolon_if_nothing_returned = { level = "allow", priority = 1 } diff --git a/src/ciphers/transposition.rs b/src/ciphers/transposition.rs index 1c32b3cfb82..521d3b2843e 100644 --- a/src/ciphers/transposition.rs +++ b/src/ciphers/transposition.rs @@ -5,7 +5,7 @@ //! original message. The most commonly referred to Transposition Cipher is the //! COLUMNAR TRANSPOSITION cipher, which is demonstrated below. -use std::ops::Range; +use std::ops::RangeInclusive; /// Encrypts or decrypts a message, using multiple keys. The /// encryption is based on the columnar transposition method. @@ -142,8 +142,8 @@ fn decrypt(mut msg: String, key_order: Vec) -> String { split_large.iter_mut().rev().for_each(|key_index| { counter -= 1; - let range: Range = - ((*key_index * split_size) + counter)..(((*key_index + 1) * split_size) + counter + 1); + let range: RangeInclusive = + ((*key_index * split_size) + counter)..=(((*key_index + 1) * split_size) + counter); let slice: String = msg[range.clone()].to_string(); indexed_vec.push((*key_index, slice)); diff --git a/src/data_structures/hash_table.rs b/src/data_structures/hash_table.rs index d382c803c58..8eb39bdefb3 100644 --- a/src/data_structures/hash_table.rs +++ b/src/data_structures/hash_table.rs @@ -93,7 +93,7 @@ mod tests { let mut hash_table = HashTable::new(); let initial_capacity = hash_table.elements.capacity(); - for i in 0..initial_capacity * 3 / 4 + 1 { + for i in 0..=initial_capacity * 3 / 4 { hash_table.insert(TestKey(i), TestKey(i + 10)); } diff --git a/src/data_structures/lazy_segment_tree.rs b/src/data_structures/lazy_segment_tree.rs index 1a45d9877fb..d34b0d35432 100644 --- a/src/data_structures/lazy_segment_tree.rs +++ b/src/data_structures/lazy_segment_tree.rs @@ -235,7 +235,10 @@ mod tests { fn check_single_interval_min(array: Vec) -> TestResult { let mut seg_tree = LazySegmentTree::from_vec(&array, min); for (i, value) in array.into_iter().enumerate() { - let res = seg_tree.query(i..(i + 1)); + let res = seg_tree.query(Range { + start: i, + end: i + 1, + }); if res != Some(value) { return TestResult::error(format!("Expected {:?}, got {:?}", Some(value), res)); } @@ -247,7 +250,10 @@ mod tests { fn check_single_interval_max(array: Vec) -> TestResult { let mut seg_tree = LazySegmentTree::from_vec(&array, max); for (i, value) in array.into_iter().enumerate() { - let res = seg_tree.query(i..(i + 1)); + let res = seg_tree.query(Range { + start: i, + end: i + 1, + }); if res != Some(value) { return TestResult::error(format!("Expected {:?}, got {:?}", Some(value), res)); } @@ -259,7 +265,10 @@ mod tests { fn check_single_interval_sum(array: Vec) -> TestResult { let mut seg_tree = LazySegmentTree::from_vec(&array, max); for (i, value) in array.into_iter().enumerate() { - let res = seg_tree.query(i..(i + 1)); + let res = seg_tree.query(Range { + start: i, + end: i + 1, + }); if res != Some(value) { return TestResult::error(format!("Expected {:?}, got {:?}", Some(value), res)); } diff --git a/src/dynamic_programming/fibonacci.rs b/src/dynamic_programming/fibonacci.rs index 77954a32190..f1a55ce77f1 100644 --- a/src/dynamic_programming/fibonacci.rs +++ b/src/dynamic_programming/fibonacci.rs @@ -226,7 +226,7 @@ fn get_pisano_sequence_and_period(m: i64) -> (i128, Vec) { let mut pisano_sequence: Vec = vec![a, b]; // Iterating through all the fib numbers to get the sequence - for _i in 0..(m * m) + 1 { + for _i in 0..=(m * m) { let c = (a + b) % m as i128; // adding number into the sequence diff --git a/src/general/mex.rs b/src/general/mex.rs index 7867f61a93e..a0514a35c54 100644 --- a/src/general/mex.rs +++ b/src/general/mex.rs @@ -6,7 +6,7 @@ use std::collections::BTreeSet; /// O(nlog(n)) implementation pub fn mex_using_set(arr: &[i64]) -> i64 { let mut s: BTreeSet = BTreeSet::new(); - for i in 0..arr.len() + 1 { + for i in 0..=arr.len() { s.insert(i as i64); } for x in arr { diff --git a/src/graph/bipartite_matching.rs b/src/graph/bipartite_matching.rs index e2f73f51182..48c25e8064f 100644 --- a/src/graph/bipartite_matching.rs +++ b/src/graph/bipartite_matching.rs @@ -45,13 +45,13 @@ impl BipartiteMatching { // Note: It does not modify self.mt1, it only works on self.mt2 pub fn kuhn(&mut self) { self.mt2 = vec![-1; self.num_vertices_grp2 + 1]; - for v in 1..self.num_vertices_grp1 + 1 { + for v in 1..=self.num_vertices_grp1 { self.used = vec![false; self.num_vertices_grp1 + 1]; self.try_kuhn(v); } } pub fn print_matching(&self) { - for i in 1..self.num_vertices_grp2 + 1 { + for i in 1..=self.num_vertices_grp2 { if self.mt2[i] == -1 { continue; } @@ -115,7 +115,7 @@ impl BipartiteMatching { let mut dist = vec![i32::MAX; self.num_vertices_grp1 + 1]; let mut res = 0; while self.bfs(&mut dist) { - for u in 1..self.num_vertices_grp1 + 1 { + for u in 1..=self.num_vertices_grp1 { if self.mt1[u] == 0 && self.dfs(u as i32, &mut dist) { res += 1; } diff --git a/src/machine_learning/cholesky.rs b/src/machine_learning/cholesky.rs index 23be6b6ad7a..3afcc040245 100644 --- a/src/machine_learning/cholesky.rs +++ b/src/machine_learning/cholesky.rs @@ -4,7 +4,7 @@ pub fn cholesky(mat: Vec, n: usize) -> Vec { } let mut res = vec![0.0; mat.len()]; for i in 0..n { - for j in 0..(i + 1) { + for j in 0..=i { let mut s = 0.0; for k in 0..j { s += res[i * n + k] * res[j * n + k]; diff --git a/src/math/area_under_curve.rs b/src/math/area_under_curve.rs index 3b93f3364fd..d4d7133ec38 100644 --- a/src/math/area_under_curve.rs +++ b/src/math/area_under_curve.rs @@ -12,7 +12,7 @@ pub fn area_under_curve(start: f64, end: f64, func: fn(f64) -> f64, step_count: let mut fx1 = func(start); let mut fx2: f64; - for eval_point in (1..step_count + 1).map(|x| (x as f64 * step_length) + start) { + for eval_point in (1..=step_count).map(|x| (x as f64 * step_length) + start) { fx2 = func(eval_point); area += (fx2 + fx1).abs() * step_length * 0.5; fx1 = fx2; diff --git a/src/math/factors.rs b/src/math/factors.rs index e0e5c5bb477..5131642dffa 100644 --- a/src/math/factors.rs +++ b/src/math/factors.rs @@ -6,7 +6,7 @@ pub fn factors(number: u64) -> Vec { let mut factors: Vec = Vec::new(); - for i in 1..((number as f64).sqrt() as u64 + 1) { + for i in 1..=((number as f64).sqrt() as u64) { if number % i == 0 { factors.push(i); if i != number / i { diff --git a/src/math/gaussian_elimination.rs b/src/math/gaussian_elimination.rs index 5282a6e0659..1370b15ddd7 100644 --- a/src/math/gaussian_elimination.rs +++ b/src/math/gaussian_elimination.rs @@ -38,7 +38,7 @@ fn echelon(matrix: &mut [Vec], i: usize, j: usize) { if matrix[i][i] == 0f32 { } else { let factor = matrix[j + 1][i] / matrix[i][i]; - (i..size + 1).for_each(|k| { + (i..=size).for_each(|k| { matrix[j + 1][k] -= factor * matrix[i][k]; }); } @@ -48,9 +48,9 @@ fn eliminate(matrix: &mut [Vec], i: usize) { let size = matrix.len(); if matrix[i][i] == 0f32 { } else { - for j in (1..i + 1).rev() { + for j in (1..=i).rev() { let factor = matrix[j - 1][i] / matrix[i][i]; - for k in (0..size + 1).rev() { + for k in (0..=size).rev() { matrix[j - 1][k] -= factor * matrix[i][k]; } } diff --git a/src/math/pascal_triangle.rs b/src/math/pascal_triangle.rs index 3929e63d1bb..34643029b6b 100644 --- a/src/math/pascal_triangle.rs +++ b/src/math/pascal_triangle.rs @@ -12,7 +12,7 @@ pub fn pascal_triangle(num_rows: i32) -> Vec> { let mut ans: Vec> = vec![]; - for i in 1..num_rows + 1 { + for i in 1..=num_rows { let mut vec: Vec = vec![1]; let mut res: i32 = 1; diff --git a/src/math/perfect_numbers.rs b/src/math/perfect_numbers.rs index 2d8a7eff89b..0d819d2b2f1 100644 --- a/src/math/perfect_numbers.rs +++ b/src/math/perfect_numbers.rs @@ -14,7 +14,7 @@ pub fn perfect_numbers(max: usize) -> Vec { let mut result: Vec = Vec::new(); // It is not known if there are any odd perfect numbers, so we go around all the numbers. - for i in 1..max + 1 { + for i in 1..=max { if is_perfect_number(i) { result.push(i); } diff --git a/src/math/prime_numbers.rs b/src/math/prime_numbers.rs index e8af55ed220..f045133a168 100644 --- a/src/math/prime_numbers.rs +++ b/src/math/prime_numbers.rs @@ -4,7 +4,7 @@ pub fn prime_numbers(max: usize) -> Vec { if max >= 2 { result.push(2) } - for i in (3..max + 1).step_by(2) { + for i in (3..=max).step_by(2) { let stop: usize = (i as f64).sqrt() as usize + 1; let mut status = true; diff --git a/src/number_theory/compute_totient.rs b/src/number_theory/compute_totient.rs index 8ccb97749ff..88af0649fcd 100644 --- a/src/number_theory/compute_totient.rs +++ b/src/number_theory/compute_totient.rs @@ -17,7 +17,7 @@ pub fn compute_totient(n: i32) -> vec::Vec { } // Compute other Phi values - for p in 2..n + 1 { + for p in 2..=n { // If phi[p] is not computed already, // then number p is prime if phi[(p) as usize] == p { @@ -27,7 +27,7 @@ pub fn compute_totient(n: i32) -> vec::Vec { // Update phi values of all // multiples of p - for i in ((2 * p)..n + 1).step_by(p as usize) { + for i in ((2 * p)..=n).step_by(p as usize) { phi[(i) as usize] = (phi[i as usize] / p) * (p - 1); } } diff --git a/src/sorting/binary_insertion_sort.rs b/src/sorting/binary_insertion_sort.rs index fd41c94ed07..3ecb47456e8 100644 --- a/src/sorting/binary_insertion_sort.rs +++ b/src/sorting/binary_insertion_sort.rs @@ -22,7 +22,7 @@ pub fn binary_insertion_sort(arr: &mut [T]) { let key = arr[i].clone(); let index = _binary_search(&arr[..i], &key); - arr[index..i + 1].rotate_right(1); + arr[index..=i].rotate_right(1); arr[index] = key; } } diff --git a/src/sorting/pancake_sort.rs b/src/sorting/pancake_sort.rs index 6f003b100fd..c37b646ca1a 100644 --- a/src/sorting/pancake_sort.rs +++ b/src/sorting/pancake_sort.rs @@ -17,8 +17,8 @@ where .map(|(idx, _)| idx) .unwrap(); if max_index != i { - arr[0..max_index + 1].reverse(); - arr[0..i + 1].reverse(); + arr[0..=max_index].reverse(); + arr[0..=i].reverse(); } } arr.to_vec() diff --git a/src/sorting/quick_sort_3_ways.rs b/src/sorting/quick_sort_3_ways.rs index af9bef342d8..cf333114170 100644 --- a/src/sorting/quick_sort_3_ways.rs +++ b/src/sorting/quick_sort_3_ways.rs @@ -8,7 +8,7 @@ fn _quick_sort_3_ways(arr: &mut [T], lo: usize, hi: usize) { } let mut rng = rand::rng(); - arr.swap(lo, rng.random_range(lo..hi + 1)); + arr.swap(lo, rng.random_range(lo..=hi)); let mut lt = lo; // arr[lo+1, lt] < v let mut gt = hi + 1; // arr[gt, r] > v diff --git a/src/sorting/sort_utils.rs b/src/sorting/sort_utils.rs index 519744344f5..140d10a7f33 100644 --- a/src/sorting/sort_utils.rs +++ b/src/sorting/sort_utils.rs @@ -8,7 +8,7 @@ pub fn generate_random_vec(n: u32, range_l: i32, range_r: i32) -> Vec { let mut count = n; while count > 0 { - arr.push(rng.random_range(range_l..range_r + 1)); + arr.push(rng.random_range(range_l..=range_r)); count -= 1; } diff --git a/src/string/manacher.rs b/src/string/manacher.rs index 98ea95aa90c..e45a3f15612 100644 --- a/src/string/manacher.rs +++ b/src/string/manacher.rs @@ -69,7 +69,7 @@ pub fn manacher(s: String) -> String { .map(|(idx, _)| idx) .unwrap(); let radius_of_max = (length_of_palindrome[center_of_max] - 1) / 2; - let answer = &chars[(center_of_max - radius_of_max)..(center_of_max + radius_of_max + 1)] + let answer = &chars[(center_of_max - radius_of_max)..=(center_of_max + radius_of_max)] .iter() .collect::(); answer.replace('#', "")