mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-21 22:33:11 -05:00
Zig: completed Luhn
This commit is contained in:
parent
852758eb9c
commit
e16700fbea
6 changed files with 273 additions and 0 deletions
19
zig/luhn/.exercism/config.json
Normal file
19
zig/luhn/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"ee7"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"luhn.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_luhn.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"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"
|
||||||
|
}
|
1
zig/luhn/.exercism/metadata.json
Normal file
1
zig/luhn/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"luhn","id":"5c3683c495e143e98cf457b99607c9fb","url":"https://exercism.org/tracks/zig/exercises/luhn","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/luhn/HELP.md
Normal file
53
zig/luhn/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit luhn.zig` 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 [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [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.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-guide]: https://zig.guide/
|
||||||
|
[ziglings]: https://codeberg.org/ziglings/exercises
|
79
zig/luhn/README.md
Normal file
79
zig/luhn/README.md
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
# Luhn
|
||||||
|
|
||||||
|
Welcome to Luhn on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Given a number determine whether or not it is valid per the Luhn formula.
|
||||||
|
|
||||||
|
The [Luhn algorithm][luhn] is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.
|
||||||
|
|
||||||
|
The task is to check if a given string is valid.
|
||||||
|
|
||||||
|
## 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.
|
||||||
|
|
||||||
|
### Example 1: valid credit card number
|
||||||
|
|
||||||
|
```text
|
||||||
|
4539 3195 0343 6467
|
||||||
|
```
|
||||||
|
|
||||||
|
The first step of the Luhn algorithm is to double every second digit, starting from the right.
|
||||||
|
We will be doubling
|
||||||
|
|
||||||
|
```text
|
||||||
|
4_3_ 3_9_ 0_4_ 6_6_
|
||||||
|
```
|
||||||
|
|
||||||
|
If doubling the number results in a number greater than 9 then subtract 9 from the product.
|
||||||
|
The results of our doubling:
|
||||||
|
|
||||||
|
```text
|
||||||
|
8569 6195 0383 3437
|
||||||
|
```
|
||||||
|
|
||||||
|
Then sum all of the digits:
|
||||||
|
|
||||||
|
```text
|
||||||
|
8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
|
||||||
|
```
|
||||||
|
|
||||||
|
If the sum is evenly divisible by 10, then the number is valid.
|
||||||
|
This number is valid!
|
||||||
|
|
||||||
|
### Example 2: invalid credit card number
|
||||||
|
|
||||||
|
```text
|
||||||
|
8273 1232 7352 0569
|
||||||
|
```
|
||||||
|
|
||||||
|
Double the second digits, starting from the right
|
||||||
|
|
||||||
|
```text
|
||||||
|
7253 2262 5312 0539
|
||||||
|
```
|
||||||
|
|
||||||
|
Sum the digits
|
||||||
|
|
||||||
|
```text
|
||||||
|
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
|
||||||
|
```
|
||||||
|
|
||||||
|
57 is not evenly divisible by 10, so this number is not valid.
|
||||||
|
|
||||||
|
[luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @ee7
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
The Luhn Algorithm on Wikipedia - https://en.wikipedia.org/wiki/Luhn_algorithm
|
29
zig/luhn/luhn.zig
Normal file
29
zig/luhn/luhn.zig
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const ascii = std.ascii;
|
||||||
|
|
||||||
|
pub fn isValid(s: []const u8) bool {
|
||||||
|
var idx: usize = s.len;
|
||||||
|
var luhn_idx: usize = 1;
|
||||||
|
var total: u64 = 0;
|
||||||
|
|
||||||
|
while (idx > 0) {
|
||||||
|
idx -= 1;
|
||||||
|
if (s[idx] == 32) continue;
|
||||||
|
if (!ascii.isDigit(s[idx])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var curr: u8 = s[idx] - 48;
|
||||||
|
if (luhn_idx % 2 == 0) {
|
||||||
|
curr *= 2;
|
||||||
|
if (curr > 9) {
|
||||||
|
curr -= 9;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total += curr;
|
||||||
|
luhn_idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (luhn_idx <= 2) return false;
|
||||||
|
return total % 10 == 0;
|
||||||
|
}
|
92
zig/luhn/test_luhn.zig
Normal file
92
zig/luhn/test_luhn.zig
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const isValid = @import("luhn.zig").isValid;
|
||||||
|
|
||||||
|
test "single digit strings cannot be valid" {
|
||||||
|
try testing.expect(!isValid("1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a single zero is invalid" {
|
||||||
|
try testing.expect(!isValid("0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a simple valid SIN that remains valid if reversed" {
|
||||||
|
try testing.expect(isValid("059"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a simple valid SIN that becomes invalid if reversed" {
|
||||||
|
try testing.expect(isValid("59"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a valid Canadian SIN" {
|
||||||
|
try testing.expect(isValid("055 444 285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid Canadian SIN" {
|
||||||
|
try testing.expect(!isValid("055 444 286"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid credit card" {
|
||||||
|
try testing.expect(!isValid("8273 1232 7352 0569"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid long number with an even remainder" {
|
||||||
|
try testing.expect(!isValid("1 2345 6789 1234 5678 9012"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid long number with a remainder divisible by 5" {
|
||||||
|
try testing.expect(!isValid("1 2345 6789 1234 5678 9013"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid number with an even number of digits" {
|
||||||
|
try testing.expect(isValid("095 245 88"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid number with an odd number of spaces" {
|
||||||
|
try testing.expect(isValid("234 567 891 234"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid strings with a non-digit added at the end become invalid" {
|
||||||
|
try testing.expect(!isValid("059a"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid strings with punctuation included become invalid" {
|
||||||
|
try testing.expect(!isValid("055-444-285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid strings with symbols included become invalid" {
|
||||||
|
try testing.expect(!isValid("055# 444$ 285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "single zero with space is invalid" {
|
||||||
|
try testing.expect(!isValid(" 0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "more than a single zero is valid" {
|
||||||
|
try testing.expect(isValid("0000 0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "input digit 9 is correctly converted to output digit 9" {
|
||||||
|
try testing.expect(isValid("091"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "very long input is valid" {
|
||||||
|
try testing.expect(isValid("9999999999 9999999999 9999999999 9999999999"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid luhn with an odd number of digits and non zero first digit" {
|
||||||
|
try testing.expect(isValid("109"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "using ascii value for non-doubled non-digit isn't allowed" {
|
||||||
|
try testing.expect(!isValid("055b 444 285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "using ascii value for doubled non-digit isn't allowed" {
|
||||||
|
try testing.expect(!isValid(":9"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed" {
|
||||||
|
try testing.expect(!isValid("59%59"));
|
||||||
|
}
|
Loading…
Reference in a new issue