-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2269: Find the K-Beauty of a Number
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/problem_2269_find_the_k_beauty_of_a_number/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
pub struct Solution; | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::num::NonZeroU32; | ||
|
||
impl Solution { | ||
pub fn divisor_substrings(num: i32, k: i32) -> i32 { | ||
let num = num as u32; | ||
let mut k = k as u32; | ||
let mut x = num; | ||
let mut base = 1; | ||
let mut window = 0; | ||
|
||
loop { | ||
window += base * (x % 10); | ||
x /= 10; | ||
k -= 1; | ||
|
||
if k == 0 { | ||
break; | ||
} | ||
|
||
base *= 10; | ||
} | ||
|
||
let mut result = 0; | ||
|
||
loop { | ||
if NonZeroU32::new(window).map_or(false, |window| num % window == 0) { | ||
result += 1; | ||
} | ||
|
||
if x == 0 { | ||
break; | ||
} | ||
|
||
window = window / 10 + base * (x % 10); | ||
x /= 10; | ||
} | ||
|
||
result | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn divisor_substrings(num: i32, k: i32) -> i32 { | ||
Self::divisor_substrings(num, k) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
pub mod iterative; | ||
|
||
pub trait Solution { | ||
fn divisor_substrings(num: i32, k: i32) -> i32; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [((240, 2), 2), ((430_043, 2), 2)]; | ||
|
||
for ((num, k), expected) in test_cases { | ||
assert_eq!(S::divisor_substrings(num, k), expected); | ||
} | ||
} | ||
} |