Skip to content

Commit

Permalink
Add problem 2057: Smallest Index With Equal Value
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jan 9, 2024
1 parent e111817 commit f334e49
Show file tree
Hide file tree
Showing 3 changed files with 62 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 @@ -1412,6 +1412,7 @@ pub mod problem_2042_check_if_numbers_are_ascending_in_a_sentence;
pub mod problem_2043_simple_bank_system;
pub mod problem_2047_number_of_valid_words_in_a_sentence;
pub mod problem_2053_kth_distinct_string_in_an_array;
pub mod problem_2057_smallest_index_with_equal_value;

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

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

impl Solution {
pub fn smallest_equal(nums: Vec<i32>) -> i32 {
let mut i_mod_10 = 0;

for (i, num) in (0..).zip(nums) {
if i_mod_10 == num {
return i;
}

if i_mod_10 == 9 {
i_mod_10 = 0;
} else {
i_mod_10 += 1;
}
}

-1
}
}

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

impl super::Solution for Solution {
fn smallest_equal(nums: Vec<i32>) -> i32 {
Self::smallest_equal(nums)
}
}

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

pub trait Solution {
fn smallest_equal(nums: Vec<i32>) -> i32;
}

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

pub fn run<S: Solution>() {
let test_cases = [
(&[0, 1, 2] as &[_], 0),
(&[4, 3, 2, 1], 2),
(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 0], -1),
];

for (nums, expected) in test_cases {
assert_eq!(S::smallest_equal(nums.to_vec()), expected);
}
}
}

0 comments on commit f334e49

Please sign in to comment.