Skip to content

Commit 39ed3e1

Browse files
Sync luhn (#552)
1 parent f6daf39 commit 39ed3e1

File tree

5 files changed

+149
-39
lines changed

5 files changed

+149
-39
lines changed
Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,68 @@
11
# Instructions
22

3-
Given a number determine whether or not it is valid per the Luhn formula.
3+
Determine whether a number is valid according to the [Luhn formula][luhn].
44

5-
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.
5+
The number will be provided as a string.
66

7-
The task is to check if a given string is valid.
8-
9-
## Validating a Number
7+
## Validating a number
108

119
Strings of length 1 or less are not valid.
1210
Spaces are allowed in the input, but they should be stripped before checking.
1311
All other non-digit characters are disallowed.
1412

15-
### Example 1: valid credit card number
13+
## Examples
1614

17-
```text
18-
4539 3195 0343 6467
19-
```
15+
### Valid credit card number
2016

21-
The first step of the Luhn algorithm is to double every second digit, starting from the right.
22-
We will be doubling
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.
2320

2421
```text
25-
4_3_ 3_9_ 0_4_ 6_6_
22+
4539 3195 0343 6467
23+
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
2624
```
2725

28-
If doubling the number results in a number greater than 9 then subtract 9 from the product.
29-
The results of our doubling:
26+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
27+
We end up with:
3028

3129
```text
3230
8569 6195 0383 3437
3331
```
3432

35-
Then sum all of the digits:
33+
Finally, we sum all digits.
34+
If the sum is evenly divisible by 10, the original number is valid.
3635

3736
```text
38-
8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
37+
8 + 5 + 6 + 9 + 6 + 1 + 9 + 5 + 0 + 3 + 8 + 3 + 3 + 4 + 3 + 7 = 80
3938
```
4039

41-
If the sum is evenly divisible by 10, then the number is valid.
42-
This number is valid!
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 468`.
4345

44-
### Example 2: invalid credit card number
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.
4547

4648
```text
47-
8273 1232 7352 0569
49+
066 123 478
50+
↑ ↑ ↑ ↑ (double these)
4851
```
4952

50-
Double the second digits, starting from the right
53+
If the result of doubling a digit is greater than 9, we subtract 9 from that result.
54+
We end up with:
5155

5256
```text
53-
7253 2262 5312 0539
57+
036 226 458
5458
```
5559

56-
Sum the digits
60+
We sum the digits:
5761

5862
```text
59-
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
63+
0 + 3 + 6 + 2 + 2 + 6 + 4 + 5 + 8 = 36
6064
```
6165

62-
57 is not evenly divisible by 10, so this number is not valid.
66+
36 is not evenly divisible by 10, so number `066 123 478` is not valid!
6367

6468
[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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
return {
2+
module_name = 'luhn',
3+
4+
generate_test = function(case)
5+
local template = [[
6+
assert.is_%s(luhn.valid('%s'))]]
7+
return template:format(case.expected, case.input.value)
8+
end
9+
}

exercises/practice/luhn/.meta/tests.toml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
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.
411

512
[792a7082-feb7-48c7-b88b-bbfec160865e]
613
description = "single digit strings can not be valid"
@@ -26,6 +33,9 @@ description = "invalid credit card"
2633
[20e67fad-2121-43ed-99a8-14b5b856adb9]
2734
description = "invalid long number with an even remainder"
2835

36+
[7e7c9fc1-d994-457c-811e-d390d52fba5e]
37+
description = "invalid long number with a remainder divisible by 5"
38+
2939
[ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa]
3040
description = "valid number with an even number of digits"
3141

@@ -50,8 +60,17 @@ description = "more than a single zero is valid"
5060
[ab56fa80-5de8-4735-8a4a-14dae588663e]
5161
description = "input digit 9 is correctly converted to output digit 9"
5262

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+
5369
[39a06a5a-5bad-4e0f-b215-b042d46209b1]
5470
description = "using ascii value for non-doubled non-digit isn't allowed"
5571

5672
[f94cf191-a62f-4868-bc72-7253114aa157]
5773
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"

exercises/practice/luhn/luhn_spec.lua

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,91 @@
11
local luhn = require('luhn')
22

33
describe('luhn', function()
4-
it('should indicate that single digits are invalid', function()
5-
assert.equal(false, luhn.valid('1'))
6-
assert.equal(false, luhn.valid('0'))
4+
it('single digit strings can not be valid', function()
5+
assert.is_false(luhn.valid('1'))
76
end)
87

9-
it('should check a valid Canadian SIN', function()
10-
assert.equal(true, luhn.valid('046 454 286'))
8+
it('a single zero is invalid', function()
9+
assert.is_false(luhn.valid('0'))
1110
end)
1211

13-
it('should check an invalid Canadian SIN', function()
14-
assert.equal(false, luhn.valid('046 454 287'))
12+
it('a simple valid sin that remains valid if reversed', function()
13+
assert.is_true(luhn.valid('059'))
1514
end)
1615

17-
it('should check an invalid credit card', function()
18-
assert.equal(false, luhn.valid('8273 1232 7352 0569'))
16+
it('a simple valid sin that becomes invalid if reversed', function()
17+
assert.is_true(luhn.valid('59'))
1918
end)
2019

21-
it('should not allow non-digits', function()
22-
assert.equal(false, luhn.valid('a46 454 286'))
20+
it('a valid canadian sin', function()
21+
assert.is_true(luhn.valid('055 444 285'))
22+
end)
23+
24+
it('invalid canadian sin', function()
25+
assert.is_false(luhn.valid('055 444 286'))
26+
end)
27+
28+
it('invalid credit card', function()
29+
assert.is_false(luhn.valid('8273 1232 7352 0569'))
30+
end)
31+
32+
it('invalid long number with an even remainder', function()
33+
assert.is_false(luhn.valid('1 2345 6789 1234 5678 9012'))
34+
end)
35+
36+
it('invalid long number with a remainder divisible by 5', function()
37+
assert.is_false(luhn.valid('1 2345 6789 1234 5678 9013'))
38+
end)
39+
40+
it('valid number with an even number of digits', function()
41+
assert.is_true(luhn.valid('095 245 88'))
42+
end)
43+
44+
it('valid number with an odd number of spaces', function()
45+
assert.is_true(luhn.valid('234 567 891 234'))
46+
end)
47+
48+
it('valid strings with a non-digit added at the end become invalid', function()
49+
assert.is_false(luhn.valid('059a'))
50+
end)
51+
52+
it('valid strings with punctuation included become invalid', function()
53+
assert.is_false(luhn.valid('055-444-285'))
54+
end)
55+
56+
it('valid strings with symbols included become invalid', function()
57+
assert.is_false(luhn.valid('055# 444$ 285'))
58+
end)
59+
60+
it('single zero with space is invalid', function()
61+
assert.is_false(luhn.valid(' 0'))
62+
end)
63+
64+
it('more than a single zero is valid', function()
65+
assert.is_true(luhn.valid('0000 0'))
66+
end)
67+
68+
it('input digit 9 is correctly converted to output digit 9', function()
69+
assert.is_true(luhn.valid('091'))
70+
end)
71+
72+
it('very long input is valid', function()
73+
assert.is_true(luhn.valid('9999999999 9999999999 9999999999 9999999999'))
74+
end)
75+
76+
it('valid luhn with an odd number of digits and non zero first digit', function()
77+
assert.is_true(luhn.valid('109'))
78+
end)
79+
80+
it('using ascii value for non-doubled non-digit isn\'t allowed', function()
81+
assert.is_false(luhn.valid('055b 444 285'))
82+
end)
83+
84+
it('using ascii value for doubled non-digit isn\'t allowed', function()
85+
assert.is_false(luhn.valid(':9'))
86+
end)
87+
88+
it('non-numeric, non-space char in the middle with a sum that\'s divisible by 10 isn\'t allowed', function()
89+
assert.is_false(luhn.valid('59%59'))
2390
end)
2491
end)

0 commit comments

Comments
 (0)