luhn
This commit is contained in:
parent
d18bc9dae6
commit
edb9158b39
7 changed files with 391 additions and 0 deletions
41
rust/luhn/.exercism/config.json
Normal file
41
rust/luhn/.exercism/config.json
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"authors": [
|
||||
"IanWhitney"
|
||||
],
|
||||
"contributors": [
|
||||
"AvasDream",
|
||||
"bitfield",
|
||||
"coriolinus",
|
||||
"cwhakes",
|
||||
"efx",
|
||||
"ErikSchierboom",
|
||||
"gibfahn",
|
||||
"idealhack",
|
||||
"lutostag",
|
||||
"mkantor",
|
||||
"navossoc",
|
||||
"nfiles",
|
||||
"petertseng",
|
||||
"rofrol",
|
||||
"stkent",
|
||||
"stringparser",
|
||||
"workingjubilee",
|
||||
"xakon",
|
||||
"ZapAnton"
|
||||
],
|
||||
"files": {
|
||||
"solution": [
|
||||
"src/lib.rs",
|
||||
"Cargo.toml"
|
||||
],
|
||||
"test": [
|
||||
"tests/luhn.rs"
|
||||
],
|
||||
"example": [
|
||||
".meta/example.rs"
|
||||
]
|
||||
},
|
||||
"blurb": "Given a number determine whether or not it is valid per the Luhn formula.",
|
||||
"source": "The Luhn Algorithm on Wikipedia",
|
||||
"source_url": "https://en.wikipedia.org/wiki/Luhn_algorithm"
|
||||
}
|
||||
2
rust/luhn/.gitignore
vendored
Normal file
2
rust/luhn/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
||||
9
rust/luhn/Cargo.toml
Normal file
9
rust/luhn/Cargo.toml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "luhn"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
# Not all libraries from crates.io are available in Exercism's test runner.
|
||||
# The full list of available libraries is here:
|
||||
# https://github.com/exercism/rust-test-runner/blob/main/local-registry/Cargo.toml
|
||||
[dependencies]
|
||||
89
rust/luhn/HELP.md
Normal file
89
rust/luhn/HELP.md
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
# Help
|
||||
|
||||
## Running the tests
|
||||
|
||||
Execute the tests with:
|
||||
|
||||
```bash
|
||||
$ cargo test
|
||||
```
|
||||
|
||||
All but the first test have been ignored. After you get the first test to
|
||||
pass, open the tests source file which is located in the `tests` directory
|
||||
and remove the `#[ignore]` flag from the next test and get the tests to pass
|
||||
again. Each separate test is a function with `#[test]` flag above it.
|
||||
Continue, until you pass every test.
|
||||
|
||||
If you wish to run _only ignored_ tests without editing the tests source file, use:
|
||||
|
||||
```bash
|
||||
$ cargo test -- --ignored
|
||||
```
|
||||
|
||||
If you are using Rust 1.51 or later, you can run _all_ tests with
|
||||
|
||||
```bash
|
||||
$ cargo test -- --include-ignored
|
||||
```
|
||||
|
||||
To run a specific test, for example `some_test`, you can use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test
|
||||
```
|
||||
|
||||
If the specific test is ignored, use:
|
||||
|
||||
```bash
|
||||
$ cargo test some_test -- --ignored
|
||||
```
|
||||
|
||||
To learn more about Rust tests refer to the online [test documentation][rust-tests].
|
||||
|
||||
[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
|
||||
|
||||
## Submitting your solution
|
||||
|
||||
You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
|
||||
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||
|
||||
It's possible to submit an incomplete solution which allows you to:
|
||||
|
||||
- See how others have completed the exercise
|
||||
- Request help from a mentor
|
||||
|
||||
## Need to get help?
|
||||
|
||||
If you'd like help solving the exercise, check the following pages:
|
||||
|
||||
- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
|
||||
- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
|
||||
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||
|
||||
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||
|
||||
## Rust Installation
|
||||
|
||||
Refer to the [exercism help page][help-page] for Rust installation and learning
|
||||
resources.
|
||||
|
||||
## Submitting the solution
|
||||
|
||||
Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
|
||||
|
||||
## Feedback, Issues, Pull Requests
|
||||
|
||||
Head to [the forum](https://forum.exercism.org/c/programming/rust/) and create a post to provide feedback about an exercise or if you want to help implement new exercises.
|
||||
Members of the rust track team are happy to help!
|
||||
|
||||
The GitHub [track repository][github] is the home for all of the Rust exercises.
|
||||
|
||||
If you want to know more about Exercism, take a look at the [contribution guide].
|
||||
|
||||
## Submitting Incomplete Solutions
|
||||
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
|
||||
|
||||
[help-page]: https://exercism.org/tracks/rust/learning
|
||||
[github]: https://github.com/exercism/rust
|
||||
[contribution guide]: https://exercism.org/docs/community/contributors
|
||||
117
rust/luhn/README.md
Normal file
117
rust/luhn/README.md
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
# Luhn
|
||||
|
||||
Welcome to Luhn on Exercism's Rust Track.
|
||||
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||
|
||||
## Introduction
|
||||
|
||||
At the Global Verification Authority, you've just been entrusted with a critical assignment.
|
||||
Across the city, from online purchases to secure logins, countless operations rely on the accuracy of numerical identifiers like credit card numbers, bank account numbers, transaction codes, and tracking IDs.
|
||||
The Luhn algorithm is a simple checksum formula used to help identify mistyped numbers.
|
||||
|
||||
A batch of identifiers has just arrived on your desk.
|
||||
All of them must pass the Luhn test to ensure they're legitimate.
|
||||
If any fail, they'll be flagged as invalid, preventing mistakes such as incorrect transactions or failed account verifications.
|
||||
|
||||
Can you ensure this is done right? The integrity of many services depends on you.
|
||||
|
||||
## Instructions
|
||||
|
||||
Determine whether a number is valid according to the [Luhn formula][luhn].
|
||||
|
||||
The number will be provided as a string.
|
||||
|
||||
## Validating a number
|
||||
|
||||
Strings of length 1 or less are not valid.
|
||||
Spaces are allowed in the input, but they should be stripped before checking.
|
||||
All other non-digit characters are disallowed.
|
||||
|
||||
## Examples
|
||||
|
||||
### Valid credit card number
|
||||
|
||||
The number to be checked is `4539 3195 0343 6467`.
|
||||
|
||||
The first step of the Luhn algorithm is to start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
|
||||
|
||||
```text
|
||||
4539 3195 0343 6467
|
||||
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
|
||||
```
|
||||
|
||||
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
|
||||
We end up with:
|
||||
|
||||
```text
|
||||
8569 6195 0383 3437
|
||||
```
|
||||
|
||||
Finally, we sum all digits.
|
||||
If the sum is evenly divisible by 10, the original number is valid.
|
||||
|
||||
```text
|
||||
8 + 5 + 6 + 9 + 6 + 1 + 9 + 5 + 0 + 3 + 8 + 3 + 3 + 4 + 3 + 7 = 80
|
||||
```
|
||||
|
||||
80 is evenly divisible by 10, so number `4539 3195 0343 6467` is valid!
|
||||
|
||||
### Invalid Canadian SIN
|
||||
|
||||
The number to be checked is `066 123 478`.
|
||||
|
||||
We start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
|
||||
|
||||
```text
|
||||
066 123 478
|
||||
↑ ↑ ↑ ↑ (double these)
|
||||
```
|
||||
|
||||
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
|
||||
We end up with:
|
||||
|
||||
```text
|
||||
036 226 458
|
||||
```
|
||||
|
||||
We sum the digits:
|
||||
|
||||
```text
|
||||
0 + 3 + 6 + 2 + 2 + 6 + 4 + 5 + 8 = 36
|
||||
```
|
||||
|
||||
36 is not evenly divisible by 10, so number `066 123 478` is not valid!
|
||||
|
||||
[luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm
|
||||
|
||||
## Source
|
||||
|
||||
### Created by
|
||||
|
||||
- @IanWhitney
|
||||
|
||||
### Contributed to by
|
||||
|
||||
- @AvasDream
|
||||
- @bitfield
|
||||
- @coriolinus
|
||||
- @cwhakes
|
||||
- @efx
|
||||
- @ErikSchierboom
|
||||
- @gibfahn
|
||||
- @idealhack
|
||||
- @lutostag
|
||||
- @mkantor
|
||||
- @navossoc
|
||||
- @nfiles
|
||||
- @petertseng
|
||||
- @rofrol
|
||||
- @stkent
|
||||
- @stringparser
|
||||
- @workingjubilee
|
||||
- @xakon
|
||||
- @ZapAnton
|
||||
|
||||
### Based on
|
||||
|
||||
The Luhn Algorithm on Wikipedia - https://en.wikipedia.org/wiki/Luhn_algorithm
|
||||
22
rust/luhn/src/lib.rs
Normal file
22
rust/luhn/src/lib.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/// Check a Luhn checksum.
|
||||
pub fn is_valid(code: &str) -> bool {
|
||||
code.chars()
|
||||
.rev()
|
||||
.filter(|c| !c.is_whitespace())
|
||||
.try_fold((0, 0), |(i, sum), c| {
|
||||
c.to_digit(10).map(|d| {
|
||||
let added = if 1 == i % 2 {
|
||||
let multiplyed = d * 2;
|
||||
if multiplyed > 9 {
|
||||
multiplyed - 9
|
||||
} else {
|
||||
multiplyed
|
||||
}
|
||||
} else {
|
||||
d
|
||||
};
|
||||
(i + 1, sum + added)
|
||||
})
|
||||
})
|
||||
.map_or(false, |(i, sum)| i > 1 && 0 == sum % 10)
|
||||
}
|
||||
111
rust/luhn/tests/luhn.rs
Normal file
111
rust/luhn/tests/luhn.rs
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
use luhn::*;
|
||||
|
||||
#[test]
|
||||
fn single_digit_strings_can_not_be_valid() {
|
||||
assert!(!is_valid("1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_single_zero_is_invalid() {
|
||||
assert!(!is_valid("0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_simple_valid_sin_that_remains_valid_if_reversed() {
|
||||
assert!(is_valid("059"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_simple_valid_sin_that_becomes_invalid_if_reversed() {
|
||||
assert!(is_valid("59"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_valid_canadian_sin() {
|
||||
assert!(is_valid("055 444 285"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_canadian_sin() {
|
||||
assert!(!is_valid("055 444 286"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_credit_card() {
|
||||
assert!(!is_valid("8273 1232 7352 0569"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_long_number_with_an_even_remainder() {
|
||||
assert!(!is_valid("1 2345 6789 1234 5678 9012"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_long_number_with_a_remainder_divisible_by_5() {
|
||||
assert!(!is_valid("1 2345 6789 1234 5678 9013"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_number_with_an_even_number_of_digits() {
|
||||
assert!(is_valid("095 245 88"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_number_with_an_odd_number_of_spaces() {
|
||||
assert!(is_valid("234 567 891 234"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_strings_with_a_non_digit_added_at_the_end_become_invalid() {
|
||||
assert!(!is_valid("059a"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_strings_with_punctuation_included_become_invalid() {
|
||||
assert!(!is_valid("055-444-285"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_strings_with_symbols_included_become_invalid() {
|
||||
assert!(!is_valid("055# 444$ 285"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_zero_with_space_is_invalid() {
|
||||
assert!(!is_valid(" 0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn more_than_a_single_zero_is_valid() {
|
||||
assert!(is_valid("0000 0"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn input_digit_9_is_correctly_converted_to_output_digit_9() {
|
||||
assert!(is_valid("091"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn very_long_input_is_valid() {
|
||||
assert!(is_valid("9999999999 9999999999 9999999999 9999999999"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_luhn_with_an_odd_number_of_digits_and_non_zero_first_digit() {
|
||||
assert!(is_valid("109"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn using_ascii_value_for_non_doubled_non_digit_isn_t_allowed() {
|
||||
assert!(!is_valid("055b 444 285"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn using_ascii_value_for_doubled_non_digit_isn_t_allowed() {
|
||||
assert!(!is_valid(":9"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_numeric_non_space_char_in_the_middle_with_a_sum_that_s_divisible_by_10_isn_t_allowed() {
|
||||
assert!(!is_valid("59%59"));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue