Skip to content

Commit

Permalink
style: include range_plus_one
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Feb 4, 2025
1 parent 8049c34 commit 1ed4f72
Show file tree
Hide file tree
Showing 20 changed files with 38 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 3 additions & 3 deletions src/ciphers/transposition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -142,8 +142,8 @@ fn decrypt(mut msg: String, key_order: Vec<usize>) -> String {

split_large.iter_mut().rev().for_each(|key_index| {
counter -= 1;
let range: Range<usize> =
((*key_index * split_size) + counter)..(((*key_index + 1) * split_size) + counter + 1);
let range: RangeInclusive<usize> =
((*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));
Expand Down
2 changes: 1 addition & 1 deletion src/data_structures/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
15 changes: 12 additions & 3 deletions src/data_structures/lazy_segment_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ mod tests {
fn check_single_interval_min(array: Vec<i32>) -> 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));
}
Expand All @@ -247,7 +250,10 @@ mod tests {
fn check_single_interval_max(array: Vec<i32>) -> 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));
}
Expand All @@ -259,7 +265,10 @@ mod tests {
fn check_single_interval_sum(array: Vec<i32>) -> 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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/dynamic_programming/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ fn get_pisano_sequence_and_period(m: i64) -> (i128, Vec<i128>) {
let mut pisano_sequence: Vec<i128> = 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
Expand Down
2 changes: 1 addition & 1 deletion src/general/mex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::BTreeSet;
/// O(nlog(n)) implementation
pub fn mex_using_set(arr: &[i64]) -> i64 {
let mut s: BTreeSet<i64> = BTreeSet::new();
for i in 0..arr.len() + 1 {
for i in 0..=arr.len() {
s.insert(i as i64);
}
for x in arr {
Expand Down
6 changes: 3 additions & 3 deletions src/graph/bipartite_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/machine_learning/cholesky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn cholesky(mat: Vec<f64>, n: usize) -> Vec<f64> {
}
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];
Expand Down
2 changes: 1 addition & 1 deletion src/math/area_under_curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/math/factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
pub fn factors(number: u64) -> Vec<u64> {
let mut factors: Vec<u64> = 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 {
Expand Down
6 changes: 3 additions & 3 deletions src/math/gaussian_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn echelon(matrix: &mut [Vec<f32>], 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];
});
}
Expand All @@ -48,9 +48,9 @@ fn eliminate(matrix: &mut [Vec<f32>], 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];
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/pascal_triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
pub fn pascal_triangle(num_rows: i32) -> Vec<Vec<i32>> {
let mut ans: Vec<Vec<i32>> = vec![];

for i in 1..num_rows + 1 {
for i in 1..=num_rows {
let mut vec: Vec<i32> = vec![1];

let mut res: i32 = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/math/perfect_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn perfect_numbers(max: usize) -> Vec<usize> {
let mut result: Vec<usize> = 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/prime_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub fn prime_numbers(max: usize) -> Vec<usize> {
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;

Expand Down
4 changes: 2 additions & 2 deletions src/number_theory/compute_totient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn compute_totient(n: i32) -> vec::Vec<i32> {
}

// 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 {
Expand All @@ -27,7 +27,7 @@ pub fn compute_totient(n: i32) -> vec::Vec<i32> {

// 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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/sorting/binary_insertion_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn binary_insertion_sort<T: Ord + Clone>(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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/sorting/pancake_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/sorting/quick_sort_3_ways.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn _quick_sort_3_ways<T: Ord>(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
Expand Down
2 changes: 1 addition & 1 deletion src/sorting/sort_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn generate_random_vec(n: u32, range_l: i32, range_r: i32) -> Vec<i32> {
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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/string/manacher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>();
answer.replace('#', "")
Expand Down

0 comments on commit 1ed4f72

Please sign in to comment.