From 22ef2d0f73f3197f373784a789858d6f83fbda5e Mon Sep 17 00:00:00 2001 From: yourarj <11359226+yourarj@users.noreply.github.com> Date: Sat, 30 Dec 2023 22:14:57 +0530 Subject: [PATCH] Project fixes - rust version 1.75 - clippy warnings - workspace resolver v2 - test fixes Signed-off-by: yourarj <11359226+yourarj@users.noreply.github.com> Date: Sat Dec 30 22:14:57 2023 +0530 On branch main Changes to be committed: modified: Cargo.toml modified: adaface-rust-tricky-quiz/src/sizeof.rs modified: leetcode-add-two-numbers/src/main.rs modified: leetcode-basic-calculator-ii/src/main.rs modified: leetcode-car-pooling/src/main.rs modified: leetcode-check-if-number-has-equal-digit-count-and-digit-value/src/lib.rs modified: leetcode-design-a-stack-with-increment-operation/src/lib.rs modified: leetcode-find-the-index-of-the-first-occurrence-in-a-string/src/lib.rs modified: leetcode-jump-game-iv/src/lib.rs modified: leetcode-longest-common-prefix/src/lib.rs modified: leetcode-longest-substring-without-repeating-characters/src/lib.rs modified: leetcode-numbers-with-same-consecutive-differences/src/lib.rs modified: leetcode-palindrome-number/src/lib.rs modified: leetcode-sqrtx/src/lib.rs modified: leetcode-two-sum/src/main.rs modified: leetcode-valid-parentheses/src/lib.rs modified: rust-toolchain.toml modified: university-of-princeton-3sum/src/lib.rs modified: university-of-princeton-path-compressed-percolation-threshold-via-monte-carlo-simulation/src/lib.rs modified: university-of-princeton-path-compressed-weighted-quick-union-canonical-element/src/lib.rs modified: university-of-princeton-path-compressed-weighted-quick-union-social/src/ds.rs modified: university-of-princeton-path-compressed-weighted-quick-union-social/src/lib.rs modified: university-of-princeton-path-compressed-weighted-quick-union/src/lib.rs modified: university-of-princeton-quick-find/src/lib.rs modified: university-of-princeton-quick-union/src/lib.rs modified: university-of-princeton-weighted-quick-union/src/lib.rs --- Cargo.toml | 1 + adaface-rust-tricky-quiz/src/sizeof.rs | 3 + leetcode-add-two-numbers/src/main.rs | 8 +- leetcode-basic-calculator-ii/src/main.rs | 2 +- leetcode-car-pooling/src/main.rs | 6 +- .../src/lib.rs | 20 +-- .../src/lib.rs | 5 +- .../src/lib.rs | 2 +- leetcode-jump-game-iv/src/lib.rs | 5 +- leetcode-longest-common-prefix/src/lib.rs | 2 +- .../src/lib.rs | 10 +- .../src/lib.rs | 2 +- leetcode-palindrome-number/src/lib.rs | 6 +- leetcode-sqrtx/src/lib.rs | 2 +- leetcode-two-sum/src/main.rs | 2 - leetcode-valid-parentheses/src/lib.rs | 16 +-- rust-toolchain.toml | 2 +- university-of-princeton-3sum/src/lib.rs | 19 ++- .../src/lib.rs | 18 ++- .../src/lib.rs | 18 ++- .../src/ds.rs | 28 ++-- .../src/lib.rs | 6 +- .../src/lib.rs | 24 ++-- university-of-princeton-quick-find/src/lib.rs | 8 +- .../src/lib.rs | 8 +- .../src/lib.rs | 130 +++++++++--------- 26 files changed, 187 insertions(+), 166 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b877bd..e7f32de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,3 +45,4 @@ members = [ "leetcode-find-the-index-of-the-first-occurrence-in-a-string", "oracle-linked-list-unique-elements", ] +resolver = "2" diff --git a/adaface-rust-tricky-quiz/src/sizeof.rs b/adaface-rust-tricky-quiz/src/sizeof.rs index 0c442c7..49a8ace 100644 --- a/adaface-rust-tricky-quiz/src/sizeof.rs +++ b/adaface-rust-tricky-quiz/src/sizeof.rs @@ -17,10 +17,13 @@ mod tests { fn foo_test() { let sample = &Sample; foo(sample); + #[allow(noop_method_call)] foo(sample.clone()); let sample2 = &(); foo(sample2); + #[allow(clippy::unit_arg)] + #[allow(clippy::clone_on_copy)] foo(sample2.clone()); let sample3 = Rc::new(()); diff --git a/leetcode-add-two-numbers/src/main.rs b/leetcode-add-two-numbers/src/main.rs index 228bdb0..baabb49 100644 --- a/leetcode-add-two-numbers/src/main.rs +++ b/leetcode-add-two-numbers/src/main.rs @@ -22,13 +22,13 @@ impl Solution { current_node_ref.next = Some(Box::new(ListNode::new(sum % 10))); current_node_ref = current_node_ref.next.as_mut().unwrap(); - node_from_one = if node_from_one.is_some() { - node_from_one.unwrap().next + node_from_one = if let Some(inner) = node_from_one { + inner.next } else { node_from_one }; - node_from_two = if node_from_two.is_some() { - node_from_two.unwrap().next + node_from_two = if let Some(inner) = node_from_two { + inner.next } else { node_from_two }; diff --git a/leetcode-basic-calculator-ii/src/main.rs b/leetcode-basic-calculator-ii/src/main.rs index 2c88007..e19fb78 100644 --- a/leetcode-basic-calculator-ii/src/main.rs +++ b/leetcode-basic-calculator-ii/src/main.rs @@ -13,7 +13,7 @@ impl Solution { let mut num = 0; let mut last_operand = '+'; for current_char in (s + ".").chars() { - if ('0'..='9').contains(¤t_char) { + if current_char.is_ascii_digit() { num = num * 10 + (current_char as i32 - '0' as i32); } else { match last_operand { diff --git a/leetcode-car-pooling/src/main.rs b/leetcode-car-pooling/src/main.rs index 12c6377..36f9e21 100644 --- a/leetcode-car-pooling/src/main.rs +++ b/leetcode-car-pooling/src/main.rs @@ -42,18 +42,18 @@ mod tests { fn positive_test() { let input = vec![vec![2, 1, 5], vec![3, 3, 7]]; let capacity = 4; - assert_eq!(false, Solution::car_pooling(input, capacity)) + assert!(!Solution::car_pooling(input, capacity)) } #[test] fn negative_test() { let input = vec![vec![2, 1, 5], vec![3, 3, 7]]; let capacity = 5; - assert_eq!(true, Solution::car_pooling(input, capacity)) + assert!(Solution::car_pooling(input, capacity)) } #[test] fn negative_test_more_passenger_than_capacity() { let input = vec![vec![2, 1, 5], vec![3, 3, 7], vec![100, 6, 7]]; let capacity = 100; - assert_eq!(false, Solution::car_pooling(input, capacity)) + assert!(!Solution::car_pooling(input, capacity)) } } diff --git a/leetcode-check-if-number-has-equal-digit-count-and-digit-value/src/lib.rs b/leetcode-check-if-number-has-equal-digit-count-and-digit-value/src/lib.rs index 559f673..cd23424 100644 --- a/leetcode-check-if-number-has-equal-digit-count-and-digit-value/src/lib.rs +++ b/leetcode-check-if-number-has-equal-digit-count-and-digit-value/src/lib.rs @@ -4,18 +4,18 @@ pub struct Solution; impl Solution { pub fn digit_count(num: String) -> bool { - let map = num.chars().map(|char| char as u8 - '0' as u8).fold( - HashMap::new(), - |mut map, digit| { - *map.entry(digit).or_insert(0) += 1; - map - }, - ); + let map = + num.chars() + .map(|char| char as u8 - b'0') + .fold(HashMap::new(), |mut map, digit| { + *map.entry(digit).or_insert(0) += 1; + map + }); let mut is_match = true; for (index, c) in num.chars().enumerate() { - let expected_occurrence = c as u8 - '0' as u8; + let expected_occurrence = c as u8 - b'0'; if map.get(&(index as u8)).unwrap_or(&0) != &expected_occurrence { is_match = false; break; @@ -32,11 +32,11 @@ mod tests { #[test] fn test_01() { let input = "1210"; - assert_eq!(true, Solution::digit_count(input.into())); + assert!(Solution::digit_count(input.into())); } #[test] fn test_02() { let input = "030"; - assert_eq!(false, Solution::digit_count(input.into())); + assert!(!Solution::digit_count(input.into())); } } diff --git a/leetcode-design-a-stack-with-increment-operation/src/lib.rs b/leetcode-design-a-stack-with-increment-operation/src/lib.rs index eeb15ea..6699a8a 100644 --- a/leetcode-design-a-stack-with-increment-operation/src/lib.rs +++ b/leetcode-design-a-stack-with-increment-operation/src/lib.rs @@ -28,10 +28,7 @@ impl CustomStack { } pub fn pop(&mut self) -> i32 { - match self.inner.pop() { - Some(num) => num, - None => -1, - } + self.inner.pop().unwrap_or(-1) } pub fn increment(&mut self, k: i32, val: i32) { diff --git a/leetcode-find-the-index-of-the-first-occurrence-in-a-string/src/lib.rs b/leetcode-find-the-index-of-the-first-occurrence-in-a-string/src/lib.rs index a5fd8e2..b86b588 100644 --- a/leetcode-find-the-index-of-the-first-occurrence-in-a-string/src/lib.rs +++ b/leetcode-find-the-index-of-the-first-occurrence-in-a-string/src/lib.rs @@ -10,7 +10,7 @@ impl Solution { } for i in 0..=(hlen - nlen) { - if &haystack[i..i + nlen] == needle { + if haystack[i..i + nlen] == needle { return i as i32; } } diff --git a/leetcode-jump-game-iv/src/lib.rs b/leetcode-jump-game-iv/src/lib.rs index 1916daa..0add1b6 100644 --- a/leetcode-jump-game-iv/src/lib.rs +++ b/leetcode-jump-game-iv/src/lib.rs @@ -5,10 +5,7 @@ impl Solution { pub fn min_jumps(arr: Vec) -> i32 { let mut neighborhood_map: HashMap> = HashMap::new(); for (index, value) in arr.iter().enumerate() { - neighborhood_map - .entry(*value) - .or_insert(Vec::new()) - .push(index) + neighborhood_map.entry(*value).or_default().push(index) } // Self::find_shorted_path(&arr, &mut neighborhood_map, vec![0]) diff --git a/leetcode-longest-common-prefix/src/lib.rs b/leetcode-longest-common-prefix/src/lib.rs index 04ac4f0..1e30bd1 100644 --- a/leetcode-longest-common-prefix/src/lib.rs +++ b/leetcode-longest-common-prefix/src/lib.rs @@ -5,7 +5,7 @@ impl Solution { let mut ans = String::new(); 'outer: for (i, c) in input_strings[0].char_indices() { for str in &input_strings[1..] { - if str.len() < i + 1 || &str[i..i + 1] != &input_strings[0][i..i + 1] { + if str.len() < i + 1 || str[i..i + 1] != input_strings[0][i..i + 1] { break 'outer; } } diff --git a/leetcode-longest-substring-without-repeating-characters/src/lib.rs b/leetcode-longest-substring-without-repeating-characters/src/lib.rs index 6911ef9..d2f556e 100644 --- a/leetcode-longest-substring-without-repeating-characters/src/lib.rs +++ b/leetcode-longest-substring-without-repeating-characters/src/lib.rs @@ -5,16 +5,14 @@ pub struct Solution; impl Solution { pub fn length_of_longest_substring(s: String) -> i32 { let mut left = -1; - let mut right = 0; let mut longest_substring = 0; - let mut char_map = HashMap::with_capacity(s.len()); + let mut char_map: HashMap = HashMap::with_capacity(s.len()); - for c in s.chars() { - if let Some(e) = char_map.insert(c, right) { + for (right, c) in s.chars().enumerate() { + if let Some(e) = char_map.insert(c, right as i32) { left = max(e, left); } - longest_substring = max(longest_substring, right - left); - right += 1; + longest_substring = max(longest_substring, right as i32 - left); } longest_substring diff --git a/leetcode-numbers-with-same-consecutive-differences/src/lib.rs b/leetcode-numbers-with-same-consecutive-differences/src/lib.rs index 35222f5..4639405 100644 --- a/leetcode-numbers-with-same-consecutive-differences/src/lib.rs +++ b/leetcode-numbers-with-same-consecutive-differences/src/lib.rs @@ -26,7 +26,7 @@ impl Solution { // check with adding and subtracting diff for branch_num in possible_nums.iter() { let new_suffix_num = (partial_build_num % 10) + branch_num; - if new_suffix_num >= 0 && new_suffix_num < 10 { + if (0..10).contains(&new_suffix_num) { new_level_vec.push(partial_build_num * 10 + new_suffix_num); } } diff --git a/leetcode-palindrome-number/src/lib.rs b/leetcode-palindrome-number/src/lib.rs index adac0e5..32bb8d7 100644 --- a/leetcode-palindrome-number/src/lib.rs +++ b/leetcode-palindrome-number/src/lib.rs @@ -24,18 +24,18 @@ mod tests { #[test] fn it_works() { let result = Solution::is_palindrome(101); - assert_eq!(result, true); + assert!(result); } #[test] fn it_does_not_work_with_negative() { let result = Solution::is_palindrome(-101); - assert_eq!(result, false); + assert!(!result); } #[test] fn it_does_not_work() { let result = Solution::is_palindrome(10); - assert_eq!(result, false); + assert!(!result); } } diff --git a/leetcode-sqrtx/src/lib.rs b/leetcode-sqrtx/src/lib.rs index ee56f1a..d94f7b4 100644 --- a/leetcode-sqrtx/src/lib.rs +++ b/leetcode-sqrtx/src/lib.rs @@ -6,7 +6,7 @@ impl Solution { while r * r > x { r = (r + x / r) / 2; } - return r as i32; + r as i32 } } diff --git a/leetcode-two-sum/src/main.rs b/leetcode-two-sum/src/main.rs index bb772d8..1816114 100644 --- a/leetcode-two-sum/src/main.rs +++ b/leetcode-two-sum/src/main.rs @@ -1,5 +1,3 @@ -use std::ops::Deref; - fn main() { println!("Hello, world!"); } diff --git a/leetcode-valid-parentheses/src/lib.rs b/leetcode-valid-parentheses/src/lib.rs index d85b186..0895580 100644 --- a/leetcode-valid-parentheses/src/lib.rs +++ b/leetcode-valid-parentheses/src/lib.rs @@ -10,19 +10,19 @@ impl Solution { stack.push(par); } else { match stack.last() { - a if a == Some(&'{') => { + Some(&'{') => { if par != '}' { break; } stack.pop(); } - a if a == Some(&'[') => { + Some(&'[') => { if par != ']' { break; } stack.pop(); } - a if a == Some(&'(') => { + Some(&'(') => { if par != ')' { break; } @@ -35,7 +35,7 @@ impl Solution { } } } - stack.len() == 0 + stack.is_empty() } } @@ -46,24 +46,24 @@ mod tests { #[test] fn it_works() { let result = Solution::is_valid("()".into()); - assert_eq!(result, true); + assert!(result); } #[test] fn it_works_2() { let result = Solution::is_valid("()[]{}".into()); - assert_eq!(result, true); + assert!(result); } #[test] fn it_works_3() { let result = Solution::is_valid("(]".into()); - assert_eq!(result, false); + assert!(!result); } #[test] fn it_works_4() { let result = Solution::is_valid("]".into()); - assert_eq!(result, false); + assert!(!result); } } diff --git a/rust-toolchain.toml b/rust-toolchain.toml index b42ddcf..c1f5c7b 100644 --- a/rust-toolchain.toml +++ b/rust-toolchain.toml @@ -1,3 +1,3 @@ [toolchain] -channel = "1.67.0" +channel = "1.75.0" components = ["rustfmt", "clippy"] diff --git a/university-of-princeton-3sum/src/lib.rs b/university-of-princeton-3sum/src/lib.rs index dfc70be..55627a8 100644 --- a/university-of-princeton-3sum/src/lib.rs +++ b/university-of-princeton-3sum/src/lib.rs @@ -1,7 +1,6 @@ pub struct ThreeSum; impl ThreeSum { - pub fn three_sum(values: &Vec) -> Vec> { - let mut sorted = values.clone(); + pub fn three_sum(sorted: &mut [i32]) -> Vec> { sorted.sort(); let mut found_num_sets = Vec::new(); for (start, elem) in sorted.iter().enumerate() { @@ -19,23 +18,23 @@ impl ThreeSum { match sorted[left_index] + sorted[right_index] { sum if sum == -elem => { found_num_sets.push(vec![*elem, sorted[left_index], sorted[right_index]]); - left_index = Self::next_valid_left_index(&sorted, left_index); - right_index = Self::next_valid_right_index(&sorted, right_index, start); + left_index = Self::next_valid_left_index(sorted, left_index); + right_index = Self::next_valid_right_index(sorted, right_index, start); } // when resultant sum exceeding expectations sum if sum > -elem => { - right_index = Self::next_valid_right_index(&sorted, right_index, start); + right_index = Self::next_valid_right_index(sorted, right_index, start); } // when resultant sum lagging expectations - _ => left_index = Self::next_valid_left_index(&sorted, left_index), + _ => left_index = Self::next_valid_left_index(sorted, left_index), } } } found_num_sets } - fn next_valid_left_index(values: &Vec, current_left_index: usize) -> usize { + fn next_valid_left_index(values: &[i32], current_left_index: usize) -> usize { let mut next = current_left_index + 1; // to avoid outputting duplicates we keep incrementing our index // if we encounter the same element @@ -61,11 +60,11 @@ mod tests { #[test] fn it_works() { - let arr = vec![-1, 1, 2, -1, 5, -3]; - let result = ThreeSum::three_sum(&arr); + let mut arr = [-1, 1, 2, -1, 5, -3]; + let result = ThreeSum::three_sum(&mut arr); println!("result is {:?}", result); - assert!(result.get(0).eq(&Some(&vec![-3, 1, 2]))); + assert!(result.first().eq(&Some(&vec![-3, 1, 2]))); assert!(result.get(1).eq(&Some(&vec![-1, -1, 2]))); assert_eq!(result.len(), 2); } diff --git a/university-of-princeton-path-compressed-percolation-threshold-via-monte-carlo-simulation/src/lib.rs b/university-of-princeton-path-compressed-percolation-threshold-via-monte-carlo-simulation/src/lib.rs index 06446e8..2631a08 100644 --- a/university-of-princeton-path-compressed-percolation-threshold-via-monte-carlo-simulation/src/lib.rs +++ b/university-of-princeton-path-compressed-percolation-threshold-via-monte-carlo-simulation/src/lib.rs @@ -11,11 +11,10 @@ pub struct Percolation { grid_open_state: [[bool; SIZE]; SIZE], } -impl Percolation { - // creates n-by-n grid, with all sites initially blocked - pub fn new() -> Self { +impl Default for Percolation { + fn default() -> Self { let total_grid_elements = SIZE * SIZE; - Percolation { + Self { grid_size: SIZE, grid: WeightedQuickUnionFind::new(), virtual_top: 0, @@ -25,6 +24,13 @@ impl Percolation { grid_open_state: [[false; SIZE]; SIZE], } } +} + +impl Percolation { + // creates n-by-n grid, with all sites initially blocked + pub fn new() -> Self { + Self::default() + } // opens the site (row, col) if it is not open already pub fn open(&mut self, row: usize, col: usize) { @@ -123,7 +129,7 @@ mod tests { per.open(0, 0); per.open(1, 0); per.open(2, 0); - assert_eq!(false, per.percolates()); + assert!(!per.percolates()); per.open(3, 0); assert!(per.percolates()); } @@ -143,7 +149,7 @@ mod tests { per.open(2, 1); per.open(2, 0); - assert_eq!(false, per.percolates()); + assert!(!per.percolates()); per.open(3, 0); assert!(per.percolates()); } diff --git a/university-of-princeton-path-compressed-weighted-quick-union-canonical-element/src/lib.rs b/university-of-princeton-path-compressed-weighted-quick-union-canonical-element/src/lib.rs index ec638ed..1e92832 100644 --- a/university-of-princeton-path-compressed-weighted-quick-union-canonical-element/src/lib.rs +++ b/university-of-princeton-path-compressed-weighted-quick-union-canonical-element/src/lib.rs @@ -10,19 +10,25 @@ pub struct WeightedQuickUnion { weights: [usize; SIZE], } -impl WeightedQuickUnion { - /// create new instance of QuickUnion - pub fn new() -> Self { +impl Default for WeightedQuickUnion { + fn default() -> Self { let mut arr = [0; SIZE]; - for index in 0..SIZE { + (0..SIZE).for_each(|index| { arr[index] = index; - } - WeightedQuickUnion { + }); + Self { arr, weights: [1; SIZE], } } +} + +impl WeightedQuickUnion { + /// create new instance of QuickUnion + pub fn new() -> Self { + Self::default() + } /// find the root of given num fn root(&mut self, mut num: usize) -> usize { diff --git a/university-of-princeton-path-compressed-weighted-quick-union-social/src/ds.rs b/university-of-princeton-path-compressed-weighted-quick-union-social/src/ds.rs index 6eb08a6..edcf556 100644 --- a/university-of-princeton-path-compressed-weighted-quick-union-social/src/ds.rs +++ b/university-of-princeton-path-compressed-weighted-quick-union-social/src/ds.rs @@ -10,19 +10,27 @@ pub struct WeightedQuickUnion { weights: [usize; SIZE], } -impl WeightedQuickUnion { - /// create new instance of QuickUnion - pub fn new() -> Self { +impl Default for WeightedQuickUnion { + fn default() -> Self { let mut arr = [0; SIZE]; - for index in 0..SIZE { + (0..SIZE).for_each(|index| { arr[index] = index; - } - WeightedQuickUnion { + }); + + Self { arr, weights: [1; SIZE], } } +} + +impl WeightedQuickUnion { + /// create new instance of QuickUnion + #[allow(dead_code)] + pub fn new() -> Self { + Self::default() + } /// find the root of given num fn root(&mut self, mut num: usize) -> usize { @@ -39,14 +47,14 @@ impl WeightedQuickUnion { } pub fn union(&mut self, a: usize, b: usize) { + if a == b { + return; + } + // TODO update code to for weight comparison let root_a = self.root(a); let root_b = self.root(b); - if root_a == root_b { - return; - } - if self.weights[root_a] < self.weights[root_b] { self.arr[root_a] = root_b; self.weights[root_b] += self.weights[root_a]; diff --git a/university-of-princeton-path-compressed-weighted-quick-union-social/src/lib.rs b/university-of-princeton-path-compressed-weighted-quick-union-social/src/lib.rs index 470eb48..1d2b391 100644 --- a/university-of-princeton-path-compressed-weighted-quick-union-social/src/lib.rs +++ b/university-of-princeton-path-compressed-weighted-quick-union-social/src/lib.rs @@ -8,7 +8,9 @@ pub struct Social { impl Default for Social { fn default() -> Self { - Self::new() + Self { + uf: WeightedQuickUnion::default(), + } } } @@ -34,7 +36,7 @@ mod tests { use super::*; #[test] - fn it_works_from_lecture() { + fn befriend_test() { let mut soc = Social::<10>::new(); soc.befriend(4, 3); soc.befriend(3, 8); diff --git a/university-of-princeton-path-compressed-weighted-quick-union/src/lib.rs b/university-of-princeton-path-compressed-weighted-quick-union/src/lib.rs index 9ef558f..e6a3dd4 100644 --- a/university-of-princeton-path-compressed-weighted-quick-union/src/lib.rs +++ b/university-of-princeton-path-compressed-weighted-quick-union/src/lib.rs @@ -54,9 +54,9 @@ impl Default for WeightedQuickUnionFind { fn default() -> Self { let mut arr = [0; SIZE]; - for index in 0..SIZE { + (0..SIZE).for_each(|index| { arr[index] = index; - } + }); WeightedQuickUnionFind { arr, weights: [1; SIZE], @@ -90,15 +90,15 @@ mod tests { // weights = {1, 1, 3, 1, 4, 1, 10, 1, 1, 1} // set two all mus be connected - assert_eq!(true, qf.connected(0, 0)); - assert_eq!(true, qf.connected(0, 1)); - assert_eq!(true, qf.connected(0, 2)); - assert_eq!(true, qf.connected(0, 3)); - assert_eq!(true, qf.connected(0, 4)); - assert_eq!(true, qf.connected(0, 5)); - assert_eq!(true, qf.connected(0, 6)); - assert_eq!(true, qf.connected(0, 7)); - assert_eq!(true, qf.connected(0, 8)); - assert_eq!(true, qf.connected(0, 9)); + assert!(qf.connected(0, 0)); + assert!(qf.connected(0, 1)); + assert!(qf.connected(0, 2)); + assert!(qf.connected(0, 3)); + assert!(qf.connected(0, 4)); + assert!(qf.connected(0, 5)); + assert!(qf.connected(0, 6)); + assert!(qf.connected(0, 7)); + assert!(qf.connected(0, 8)); + assert!(qf.connected(0, 9)); } } diff --git a/university-of-princeton-quick-find/src/lib.rs b/university-of-princeton-quick-find/src/lib.rs index b64efc5..57bbbd9 100644 --- a/university-of-princeton-quick-find/src/lib.rs +++ b/university-of-princeton-quick-find/src/lib.rs @@ -43,9 +43,9 @@ mod tests { qf.union(1, 0); qf.union(6, 7); - assert_eq!(false, qf.connected(4, 6)); - assert_eq!(true, qf.connected(3, 4)); - assert_eq!(true, qf.connected(6, 2)); - assert_eq!(true, qf.connected(6, 6)); + assert!(!qf.connected(4, 6)); + assert!(qf.connected(3, 4)); + assert!(qf.connected(6, 2)); + assert!(qf.connected(6, 6)); } } diff --git a/university-of-princeton-quick-union/src/lib.rs b/university-of-princeton-quick-union/src/lib.rs index d46e8ef..5c098cf 100644 --- a/university-of-princeton-quick-union/src/lib.rs +++ b/university-of-princeton-quick-union/src/lib.rs @@ -53,9 +53,9 @@ mod tests { qf.union(1, 0); qf.union(6, 7); - assert_eq!(false, qf.connected(4, 6)); - assert_eq!(true, qf.connected(3, 4)); - assert_eq!(true, qf.connected(6, 2)); - assert_eq!(true, qf.connected(6, 6)); + assert!(!qf.connected(4, 6)); + assert!(qf.connected(3, 4)); + assert!(qf.connected(6, 2)); + assert!(qf.connected(6, 6)); } } diff --git a/university-of-princeton-weighted-quick-union/src/lib.rs b/university-of-princeton-weighted-quick-union/src/lib.rs index 60812c4..440b06b 100644 --- a/university-of-princeton-weighted-quick-union/src/lib.rs +++ b/university-of-princeton-weighted-quick-union/src/lib.rs @@ -15,10 +15,10 @@ impl WeightedQuickUnion { pub fn new() -> Self { let mut arr = [0; SIZE]; - for index in 0..SIZE { + (0..SIZE).for_each(|index| { arr[index] = index; - } - WeightedQuickUnion { + }); + Self { arr, weights: [1; SIZE], } @@ -56,6 +56,12 @@ impl WeightedQuickUnion { } } +impl Default for WeightedQuickUnion { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use super::*; @@ -82,61 +88,61 @@ mod tests { // and every element in individual is connected to every other one. // set one all must be connected - assert_eq!(true, qf.connected(3, 4)); - assert_eq!(true, qf.connected(3, 8)); - assert_eq!(true, qf.connected(3, 9)); - assert_eq!(true, qf.connected(4, 8)); - assert_eq!(true, qf.connected(4, 9)); - assert_eq!(true, qf.connected(8, 9)); + assert!(qf.connected(3, 4)); + assert!(qf.connected(3, 8)); + assert!(qf.connected(3, 9)); + assert!(qf.connected(4, 8)); + assert!(qf.connected(4, 9)); + assert!(qf.connected(8, 9)); // must be connected to self - assert_eq!(true, qf.connected(6, 6)); + assert!(qf.connected(6, 6)); // set two all mus be connected - assert_eq!(true, qf.connected(0, 1)); - assert_eq!(true, qf.connected(0, 2)); - assert_eq!(true, qf.connected(0, 5)); - assert_eq!(true, qf.connected(0, 6)); - assert_eq!(true, qf.connected(0, 7)); - assert_eq!(true, qf.connected(1, 2)); - assert_eq!(true, qf.connected(1, 5)); - assert_eq!(true, qf.connected(1, 6)); - assert_eq!(true, qf.connected(1, 7)); - assert_eq!(true, qf.connected(2, 5)); - assert_eq!(true, qf.connected(2, 6)); - assert_eq!(true, qf.connected(2, 7)); - assert_eq!(true, qf.connected(5, 6)); - assert_eq!(true, qf.connected(5, 7)); - assert_eq!(true, qf.connected(6, 7)); + assert!(qf.connected(0, 1)); + assert!(qf.connected(0, 2)); + assert!(qf.connected(0, 5)); + assert!(qf.connected(0, 6)); + assert!(qf.connected(0, 7)); + assert!(qf.connected(1, 2)); + assert!(qf.connected(1, 5)); + assert!(qf.connected(1, 6)); + assert!(qf.connected(1, 7)); + assert!(qf.connected(2, 5)); + assert!(qf.connected(2, 6)); + assert!(qf.connected(2, 7)); + assert!(qf.connected(5, 6)); + assert!(qf.connected(5, 7)); + assert!(qf.connected(6, 7)); // the two sets must not be connected in any way - assert_eq!(false, qf.connected(3, 0)); - assert_eq!(false, qf.connected(3, 1)); - assert_eq!(false, qf.connected(3, 2)); - assert_eq!(false, qf.connected(3, 5)); - assert_eq!(false, qf.connected(3, 6)); - assert_eq!(false, qf.connected(3, 7)); - - assert_eq!(false, qf.connected(4, 0)); - assert_eq!(false, qf.connected(4, 1)); - assert_eq!(false, qf.connected(4, 2)); - assert_eq!(false, qf.connected(4, 5)); - assert_eq!(false, qf.connected(4, 6)); - assert_eq!(false, qf.connected(4, 7)); - - assert_eq!(false, qf.connected(8, 0)); - assert_eq!(false, qf.connected(8, 1)); - assert_eq!(false, qf.connected(8, 2)); - assert_eq!(false, qf.connected(8, 5)); - assert_eq!(false, qf.connected(8, 6)); - assert_eq!(false, qf.connected(8, 7)); - - assert_eq!(false, qf.connected(9, 0)); - assert_eq!(false, qf.connected(9, 1)); - assert_eq!(false, qf.connected(9, 2)); - assert_eq!(false, qf.connected(9, 5)); - assert_eq!(false, qf.connected(9, 6)); - assert_eq!(false, qf.connected(9, 7)); + assert!(!qf.connected(3, 0)); + assert!(!qf.connected(3, 1)); + assert!(!qf.connected(3, 2)); + assert!(!qf.connected(3, 5)); + assert!(!qf.connected(3, 6)); + assert!(!qf.connected(3, 7)); + + assert!(!qf.connected(4, 0)); + assert!(!qf.connected(4, 1)); + assert!(!qf.connected(4, 2)); + assert!(!qf.connected(4, 5)); + assert!(!qf.connected(4, 6)); + assert!(!qf.connected(4, 7)); + + assert!(!qf.connected(8, 0)); + assert!(!qf.connected(8, 1)); + assert!(!qf.connected(8, 2)); + assert!(!qf.connected(8, 5)); + assert!(!qf.connected(8, 6)); + assert!(!qf.connected(8, 7)); + + assert!(!qf.connected(9, 0)); + assert!(!qf.connected(9, 1)); + assert!(!qf.connected(9, 2)); + assert!(!qf.connected(9, 5)); + assert!(!qf.connected(9, 6)); + assert!(!qf.connected(9, 7)); } #[test] @@ -161,15 +167,15 @@ mod tests { // weights = {1, 1, 3, 1, 4, 1, 10, 1, 1, 1} // set two all mus be connected - assert_eq!(true, qf.connected(0, 0)); - assert_eq!(true, qf.connected(0, 1)); - assert_eq!(true, qf.connected(0, 2)); - assert_eq!(true, qf.connected(0, 3)); - assert_eq!(true, qf.connected(0, 4)); - assert_eq!(true, qf.connected(0, 5)); - assert_eq!(true, qf.connected(0, 6)); - assert_eq!(true, qf.connected(0, 7)); - assert_eq!(true, qf.connected(0, 8)); - assert_eq!(true, qf.connected(0, 9)); + assert!(qf.connected(0, 0)); + assert!(qf.connected(0, 1)); + assert!(qf.connected(0, 2)); + assert!(qf.connected(0, 3)); + assert!(qf.connected(0, 4)); + assert!(qf.connected(0, 5)); + assert!(qf.connected(0, 6)); + assert!(qf.connected(0, 7)); + assert!(qf.connected(0, 8)); + assert!(qf.connected(0, 9)); } }