add luhn's algorithm (issue #312)

main
William Perron 1 year ago
parent e4660ab365
commit 93f04e0d98
No known key found for this signature in database
GPG Key ID: D1815C43C9BA3DE1

4
Cargo.lock generated

@ -37,6 +37,10 @@ version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
[[package]]
name = "luhn"
version = "0.1.0"
[[package]]
name = "once_cell"
version = "1.17.1"

@ -3,6 +3,7 @@ members = [
"balance",
"empty-zeros",
"fraction-math",
"luhn",
"parens",
"repeated-groups",
"scrabble",

@ -0,0 +1,8 @@
[package]
name = "luhn"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

@ -0,0 +1,24 @@
fn main() {
println!("{}", validate(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]));
}
fn validate(card: Vec<u8>) -> bool {
let mut card = card;
let given_check = card.pop().unwrap();
let mut check = 0;
for (i, n) in card.iter().rev().enumerate() {
check += match i & 1 == 0 {
true => {
if *n > 4 {
(*n * 2) - 9
} else {
*n * 2
}
}
false => *n,
};
}
10 - (check % 10) == given_check
}
Loading…
Cancel
Save