Skip to content

Commit

Permalink
Project fixes
Browse files Browse the repository at this point in the history
  - 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
  • Loading branch information
yourarj committed Dec 30, 2023
1 parent 4693bf2 commit 22ef2d0
Showing 26 changed files with 187 additions and 166 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -45,3 +45,4 @@ members = [
"leetcode-find-the-index-of-the-first-occurrence-in-a-string",
"oracle-linked-list-unique-elements",
]
resolver = "2"
3 changes: 3 additions & 0 deletions adaface-rust-tricky-quiz/src/sizeof.rs
Original file line number Diff line number Diff line change
@@ -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(());
8 changes: 4 additions & 4 deletions leetcode-add-two-numbers/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
};
2 changes: 1 addition & 1 deletion leetcode-basic-calculator-ii/src/main.rs
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ impl Solution {
let mut num = 0;
let mut last_operand = '+';
for current_char in (s + ".").chars() {
if ('0'..='9').contains(&current_char) {
if current_char.is_ascii_digit() {
num = num * 10 + (current_char as i32 - '0' as i32);
} else {
match last_operand {
6 changes: 3 additions & 3 deletions leetcode-car-pooling/src/main.rs
Original file line number Diff line number Diff line change
@@ -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))
}
}
Original file line number Diff line number Diff line change
@@ -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()));
}
}
5 changes: 1 addition & 4 deletions leetcode-design-a-stack-with-increment-operation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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) {
Original file line number Diff line number Diff line change
@@ -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;
}
}
5 changes: 1 addition & 4 deletions leetcode-jump-game-iv/src/lib.rs
Original file line number Diff line number Diff line change
@@ -5,10 +5,7 @@ impl Solution {
pub fn min_jumps(arr: Vec<i32>) -> i32 {
let mut neighborhood_map: HashMap<i32, Vec<usize>> = 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])
2 changes: 1 addition & 1 deletion leetcode-longest-common-prefix/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<char, i32> = 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
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 3 additions & 3 deletions leetcode-palindrome-number/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion leetcode-sqrtx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ impl Solution {
while r * r > x {
r = (r + x / r) / 2;
}
return r as i32;
r as i32
}
}

2 changes: 0 additions & 2 deletions leetcode-two-sum/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::ops::Deref;

fn main() {
println!("Hello, world!");
}
16 changes: 8 additions & 8 deletions leetcode-valid-parentheses/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.67.0"
channel = "1.75.0"
components = ["rustfmt", "clippy"]
19 changes: 9 additions & 10 deletions university-of-princeton-3sum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub struct ThreeSum;
impl ThreeSum {
pub fn three_sum(values: &Vec<i32>) -> Vec<Vec<i32>> {
let mut sorted = values.clone();
pub fn three_sum(sorted: &mut [i32]) -> Vec<Vec<i32>> {
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<i32>, 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);
}
Original file line number Diff line number Diff line change
@@ -11,11 +11,10 @@ pub struct Percolation<const SIZE: usize> {
grid_open_state: [[bool; SIZE]; SIZE],
}

impl<const SIZE: usize> Percolation<SIZE> {
// creates n-by-n grid, with all sites initially blocked
pub fn new() -> Self {
impl<const SIZE: usize> Default for Percolation<SIZE> {
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<const SIZE: usize> Percolation<SIZE> {
grid_open_state: [[false; SIZE]; SIZE],
}
}
}

impl<const SIZE: usize> Percolation<SIZE> {
// 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());
}
Loading

0 comments on commit 22ef2d0

Please sign in to comment.