Skip to content

Commit e47e472

Browse files
authored
feat: add luhn exercise (#29)
1 parent 8fca784 commit e47e472

10 files changed

Lines changed: 542 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@
137137
"prerequisites": [],
138138
"difficulty": 3
139139
},
140+
{
141+
"slug": "luhn",
142+
"name": "Luhn",
143+
"uuid": "f8a9f28c-2ffe-4d3c-86c6-05e023e5e6ca",
144+
"practices": [],
145+
"prerequisites": [],
146+
"difficulty": 3
147+
},
140148
{
141149
"slug": "pangram",
142150
"name": "Pangram",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Instructions
2+
3+
Determine whether a number is valid according to the [Luhn formula][luhn].
4+
5+
The number will be provided as a string.
6+
7+
## Validating a number
8+
9+
Strings of length 1 or less are not valid.
10+
Spaces are allowed in the input, but they should be stripped before checking.
11+
All other non-digit characters are disallowed.
12+
13+
## Examples
14+
15+
### Valid credit card number
16+
17+
The number to be checked is `4539 3195 0343 6467`.
18+
19+
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.
20+
21+
```text
22+
4539 3195 0343 6467
23+
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
24+
```
25+
26+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
27+
We end up with:
28+
29+
```text
30+
8569 6195 0383 3437
31+
```
32+
33+
Finally, we sum all digits.
34+
If the sum is evenly divisible by 10, the original number is valid.
35+
36+
```text
37+
8 + 5 + 6 + 9 + 6 + 1 + 9 + 5 + 0 + 3 + 8 + 3 + 3 + 4 + 3 + 7 = 80
38+
```
39+
40+
80 is evenly divisible by 10, so number `4539 3195 0343 6467` is valid!
41+
42+
### Invalid Canadian SIN
43+
44+
The number to be checked is `066 123 478`.
45+
46+
We start at the end of the number and double every second digit, beginning with the second digit from the right and moving left.
47+
48+
```text
49+
066 123 478
50+
↑ ↑ ↑ ↑ (double these)
51+
```
52+
53+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
54+
We end up with:
55+
56+
```text
57+
036 226 458
58+
```
59+
60+
We sum the digits:
61+
62+
```text
63+
0 + 3 + 6 + 2 + 2 + 6 + 4 + 5 + 8 = 36
64+
```
65+
66+
36 is not evenly divisible by 10, so number `066 123 478` is not valid!
67+
68+
[luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Introduction
2+
3+
At the Global Verification Authority, you've just been entrusted with a critical assignment.
4+
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.
5+
The Luhn algorithm is a simple checksum formula used to help identify mistyped numbers.
6+
7+
A batch of identifiers has just arrived on your desk.
8+
All of them must pass the Luhn test to ensure they're legitimate.
9+
If any fail, they'll be flagged as invalid, preventing mistakes such as incorrect transactions or failed account verifications.
10+
11+
Can you ensure this is done right? The integrity of many services depends on you.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"vaeng"
4+
],
5+
"files": {
6+
"solution": [
7+
"solution.jai"
8+
],
9+
"test": [
10+
"tests.jai"
11+
],
12+
"example": [
13+
".meta/example.jai"
14+
]
15+
},
16+
"blurb": "Given a number determine whether or not it is valid per the Luhn formula.",
17+
"source": "The Luhn Algorithm on Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Luhn_algorithm"
19+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
is_valid :: (value_str: string) -> bool {
2+
result: int;
3+
counter: int;
4+
5+
for < value_str {
6+
c := to_lower(it);
7+
if c == #char " " then continue;
8+
if c < #char "0" || c > #char "9" then return false;
9+
digit := c - #char "0";
10+
11+
if counter % 2 == 1
12+
then if digit * 2 > 9 then result += digit * 2 - 9; else result += digit * 2;
13+
else
14+
result += digit;
15+
16+
counter += 1;
17+
}
18+
if counter <= 1 return false;
19+
20+
return result % 10 == 0;
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
to_test_case :: (canonical_case: CanonicalCase) -> Test {
2+
expected := canonical_case.expected.boolean;
3+
_, value := get_string_property(canonical_case.input, "value");
4+
5+
return Test.{
6+
name = to_test_name(canonical_case.description_path),
7+
body = tprint("assert_equal(%, is_valid(%));", to_code(expected), to_code(value)),
8+
skip = canonical_case.index > 0
9+
};
10+
}
11+
12+
#run run_generator("luhn", to_test_case);
13+
14+
#import "Basic";
15+
#import,dir "../../../shared/generator";
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[792a7082-feb7-48c7-b88b-bbfec160865e]
13+
description = "single digit strings can not be valid"
14+
15+
[698a7924-64d4-4d89-8daa-32e1aadc271e]
16+
description = "a single zero is invalid"
17+
18+
[73c2f62b-9b10-4c9f-9a04-83cee7367965]
19+
description = "a simple valid SIN that remains valid if reversed"
20+
21+
[9369092e-b095-439f-948d-498bd076be11]
22+
description = "a simple valid SIN that becomes invalid if reversed"
23+
24+
[8f9f2350-1faf-4008-ba84-85cbb93ffeca]
25+
description = "a valid Canadian SIN"
26+
27+
[1cdcf269-6560-44fc-91f6-5819a7548737]
28+
description = "invalid Canadian SIN"
29+
30+
[656c48c1-34e8-4e60-9a5a-aad8a367810a]
31+
description = "invalid credit card"
32+
33+
[20e67fad-2121-43ed-99a8-14b5b856adb9]
34+
description = "invalid long number with an even remainder"
35+
36+
[7e7c9fc1-d994-457c-811e-d390d52fba5e]
37+
description = "invalid long number with a remainder divisible by 5"
38+
39+
[ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa]
40+
description = "valid number with an even number of digits"
41+
42+
[ef081c06-a41f-4761-8492-385e13c8202d]
43+
description = "valid number with an odd number of spaces"
44+
45+
[bef66f64-6100-4cbb-8f94-4c9713c5e5b2]
46+
description = "valid strings with a non-digit added at the end become invalid"
47+
48+
[2177e225-9ce7-40f6-b55d-fa420e62938e]
49+
description = "valid strings with punctuation included become invalid"
50+
51+
[ebf04f27-9698-45e1-9afe-7e0851d0fe8d]
52+
description = "valid strings with symbols included become invalid"
53+
54+
[08195c5e-ce7f-422c-a5eb-3e45fece68ba]
55+
description = "single zero with space is invalid"
56+
57+
[12e63a3c-f866-4a79-8c14-b359fc386091]
58+
description = "more than a single zero is valid"
59+
60+
[ab56fa80-5de8-4735-8a4a-14dae588663e]
61+
description = "input digit 9 is correctly converted to output digit 9"
62+
63+
[b9887ee8-8337-46c5-bc45-3bcab51bc36f]
64+
description = "very long input is valid"
65+
66+
[8a7c0e24-85ea-4154-9cf1-c2db90eabc08]
67+
description = "valid luhn with an odd number of digits and non zero first digit"
68+
69+
[39a06a5a-5bad-4e0f-b215-b042d46209b1]
70+
description = "using ascii value for non-doubled non-digit isn't allowed"
71+
72+
[f94cf191-a62f-4868-bc72-7253114aa157]
73+
description = "using ascii value for doubled non-digit isn't allowed"
74+
75+
[8b72ad26-c8be-49a2-b99c-bcc3bf631b33]
76+
description = "non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed"

0 commit comments

Comments
 (0)