Created
March 1, 2021 14:48
-
-
Save FreeMasen/22d514ff397172f58321d074548a68b5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
pub fn is_valid2(code: &str) -> bool { | |
code.chars().filter(|&c| c != ' ').count() > 1 | |
&& code.chars().filter(|&c| c != ' ').all(|c| c.is_digit(10)) | |
&& code | |
.chars() | |
.filter(|&c| c != ' ') | |
.rev() | |
.enumerate() | |
.fold(0, |acc, (i, c)| match (i, c.to_digit(10).unwrap()) { | |
(i, d) if i % 2 == 0 => acc + d, | |
(_i, d) if d * 2 > 9 => acc + 2 * d - 9, | |
(_i, d) => acc + 2 * d, | |
}) | |
% 10 | |
== 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment