ci(rs): add pre-commit hook for ‹rustfmt›

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2024-02-21 23:28:35 +01:00
parent 56b6eee734
commit 94c49118db
Signed by: mfocko
GPG key ID: 7C47D46246790496
6 changed files with 42 additions and 21 deletions

View file

@ -8,3 +8,8 @@ repos:
- id: end-of-file-fixer - id: end-of-file-fixer
- id: check-yaml - id: check-yaml
- id: check-added-large-files - id: check-added-large-files
- repo: https://github.com/mfocko/pre-commit-hooks
rev: main
hooks:
- id: rust-fmt
files: \.rs$

View file

@ -16,12 +16,12 @@
// } // }
// } // }
// } // }
use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
impl Solution { impl Solution {
fn inorder(values: &mut Vec<i32>, node: Option<&Rc<RefCell<TreeNode>>>) { fn inorder(values: &mut Vec<i32>, node: Option<&Rc<RefCell<TreeNode>>>) {
match node { match node {
None => {}, None => {}
Some(n) => { Some(n) => {
Solution::inorder(values, (*n).borrow().left.as_ref()); Solution::inorder(values, (*n).borrow().left.as_ref());
values.push((*n).borrow().val); values.push((*n).borrow().val);

View file

@ -17,9 +17,9 @@
// } // }
// } // }
use std::rc::Rc;
use std::cmp;
use std::cell::RefCell; use std::cell::RefCell;
use std::cmp;
use std::rc::Rc;
fn _max_depth(root: Option<Rc<RefCell<TreeNode>>>, depth: i32) -> i32 { fn _max_depth(root: Option<Rc<RefCell<TreeNode>>>, depth: i32) -> i32 {
match root { match root {

View file

@ -7,7 +7,10 @@ struct Accumulator {
impl Accumulator { impl Accumulator {
fn new() -> Accumulator { fn new() -> Accumulator {
Accumulator { removed: 0, removed_distinct: 0 } Accumulator {
removed: 0,
removed_distinct: 0,
}
} }
fn update(self, length: usize, count: i32) -> Accumulator { fn update(self, length: usize, count: i32) -> Accumulator {
@ -26,24 +29,27 @@ impl Solution {
pub fn min_set_size(arr: Vec<i32>) -> i32 { pub fn min_set_size(arr: Vec<i32>) -> i32 {
let mut counts = arr let mut counts = arr
.iter() .iter()
.fold(BTreeMap::<i32, i32>::new(), |mut acc: BTreeMap<i32, i32>, x: &i32| { .fold(
let current = *acc.get(x).unwrap_or(&0); BTreeMap::<i32, i32>::new(),
acc.insert(*x, current + 1); |mut acc: BTreeMap<i32, i32>, x: &i32| {
let current = *acc.get(x).unwrap_or(&0);
acc.insert(*x, current + 1);
acc acc
}) },
)
.iter() .iter()
.map(|(&x, &y)| { (x, y) }) .map(|(&x, &y)| (x, y))
.collect::<Vec<(i32, i32)>>(); .collect::<Vec<(i32, i32)>>();
counts counts.sort_by(|&(_, y0), &(_, y1)| y1.cmp(&y0));
.sort_by(|&(_, y0), &(_, y1)| y1.cmp(&y0));
counts counts
.iter() .iter()
.fold(Accumulator::new(), |acc, &(_, count)| { .fold(Accumulator::new(), |acc, &(_, count)| {
acc.update(arr.len(), count) acc.update(arr.len(), count)
}).removed_distinct })
.removed_distinct
} }
} }
@ -59,12 +65,18 @@ mod tests {
#[test] #[test]
fn test_example() { fn test_example() {
assert_eq!(Solution::min_set_size(vec![3, 3, 3, 3, 5, 5, 5, 2, 2, 7]), 2); assert_eq!(
Solution::min_set_size(vec![3, 3, 3, 3, 5, 5, 5, 2, 2, 7]),
2
);
} }
#[test] #[test]
fn test_all_same() { fn test_all_same() {
assert_eq!(Solution::min_set_size(vec![7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]), 1); assert_eq!(
Solution::min_set_size(vec![7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]),
1
);
} }
#[test] #[test]

View file

@ -1,8 +1,10 @@
impl Solution { impl Solution {
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> { pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
nums.iter().scan(0, |s, &x| { nums.iter()
*s = *s + x; .scan(0, |s, &x| {
Some(*s) *s = *s + x;
}).collect() Some(*s)
})
.collect()
} }
} }

View file

@ -13,6 +13,8 @@ impl Solution {
let mut frequencies: Vec<_> = freqs.iter().collect(); let mut frequencies: Vec<_> = freqs.iter().collect();
frequencies.sort_by_key(|&(_, count)| Reverse(count)); frequencies.sort_by_key(|&(_, count)| Reverse(count));
frequencies.iter().fold(String::new(), |s, (c, count)| s + &c.to_string().repeat(**count)) frequencies.iter().fold(String::new(), |s, (c, count)| {
s + &c.to_string().repeat(**count)
})
} }
} }