Skip to content

Commit

Permalink
Add problem 2315: Count Asterisks
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Nov 8, 2024
1 parent 172cad0 commit 2436138
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,7 @@ pub mod problem_2303_calculate_amount_paid_in_taxes;
pub mod problem_2305_fair_distribution_of_cookies;
pub mod problem_2309_greatest_english_letter_in_upper_and_lower_case;
pub mod problem_2310_sum_of_numbers_with_units_digit_k;
pub mod problem_2315_count_asterisks;

#[cfg(test)]
mod test_utilities;
42 changes: 42 additions & 0 deletions src/problem_2315_count_asterisks/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn count_asterisks(s: String) -> i32 {
let mut result = 0;
let mut iter = s.bytes();

loop {
loop {
if let Some(c) = iter.next() {
if c == b'|' {
break;
}

result += i32::from(c == b'*');
} else {
return result;
}
}

iter.find(|&c| c == b'|');
}
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn count_asterisks(s: String) -> i32 {
Self::count_asterisks(s)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
22 changes: 22 additions & 0 deletions src/problem_2315_count_asterisks/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pub mod iterative;

pub trait Solution {
fn count_asterisks(s: String) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
("l|*e*et|c**o|*de|", 2),
("iamprogrammer", 0),
("yo|uar|e**|b|e***au|tifu|l", 5),
];

for (s, expected) in test_cases {
assert_eq!(S::count_asterisks(s.to_string()), expected);
}
}
}

0 comments on commit 2436138

Please sign in to comment.