From 477e9a5d0b2fe82a2718a8ecfd286831689cc683 Mon Sep 17 00:00:00 2001 From: Glenn Jackman Date: Wed, 24 Dec 2025 18:56:18 -0500 Subject: [PATCH] luhn --- config.json | 8 ++ exercises/practice/luhn/.busted | 5 ++ exercises/practice/luhn/.docs/instructions.md | 68 +++++++++++++++++ exercises/practice/luhn/.docs/introduction.md | 11 +++ exercises/practice/luhn/.meta/config.json | 19 +++++ exercises/practice/luhn/.meta/example.moon | 22 ++++++ .../practice/luhn/.meta/spec_generator.moon | 6 ++ exercises/practice/luhn/.meta/tests.toml | 76 +++++++++++++++++++ exercises/practice/luhn/luhn.moon | 4 + exercises/practice/luhn/luhn_spec.moon | 68 +++++++++++++++++ 10 files changed, 287 insertions(+) create mode 100644 exercises/practice/luhn/.busted create mode 100644 exercises/practice/luhn/.docs/instructions.md create mode 100644 exercises/practice/luhn/.docs/introduction.md create mode 100644 exercises/practice/luhn/.meta/config.json create mode 100644 exercises/practice/luhn/.meta/example.moon create mode 100644 exercises/practice/luhn/.meta/spec_generator.moon create mode 100644 exercises/practice/luhn/.meta/tests.toml create mode 100644 exercises/practice/luhn/luhn.moon create mode 100644 exercises/practice/luhn/luhn_spec.moon diff --git a/config.json b/config.json index e8f1b20..3641036 100644 --- a/config.json +++ b/config.json @@ -358,6 +358,14 @@ "practices": [], "prerequisites": [], "difficulty": 4 + }, + { + "slug": "luhn", + "name": "Luhn", + "uuid": "8b2916c6-5ffc-489a-bedb-d5402b3d7152", + "practices": [], + "prerequisites": [], + "difficulty": 4 } ] }, diff --git a/exercises/practice/luhn/.busted b/exercises/practice/luhn/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/luhn/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/luhn/.docs/instructions.md b/exercises/practice/luhn/.docs/instructions.md new file mode 100644 index 0000000..7702c6b --- /dev/null +++ b/exercises/practice/luhn/.docs/instructions.md @@ -0,0 +1,68 @@ +# 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 diff --git a/exercises/practice/luhn/.docs/introduction.md b/exercises/practice/luhn/.docs/introduction.md new file mode 100644 index 0000000..dee4800 --- /dev/null +++ b/exercises/practice/luhn/.docs/introduction.md @@ -0,0 +1,11 @@ +# 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. diff --git a/exercises/practice/luhn/.meta/config.json b/exercises/practice/luhn/.meta/config.json new file mode 100644 index 0000000..d3f84d7 --- /dev/null +++ b/exercises/practice/luhn/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "glennj" + ], + "files": { + "solution": [ + "luhn.moon" + ], + "test": [ + "luhn_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "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" +} diff --git a/exercises/practice/luhn/.meta/example.moon b/exercises/practice/luhn/.meta/example.moon new file mode 100644 index 0000000..14abe8d --- /dev/null +++ b/exercises/practice/luhn/.meta/example.moon @@ -0,0 +1,22 @@ +doubled = { + [false]: {[0]: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, + [true]: {[0]: 0, 2, 4, 6, 8, 1, 3, 5, 7, 9}, +} + +{ + is_valid: (input) -> + stripped = input\gsub '%s', '' + return false if #stripped < 2 + + double = false + sum = 0 + + for i = #stripped, 1, -1 + digit = tonumber stripped\sub(i, i) + return false if not digit + sum += doubled[double][digit] + double = not double + + sum % 10 == 0 +} + diff --git a/exercises/practice/luhn/.meta/spec_generator.moon b/exercises/practice/luhn/.meta/spec_generator.moon new file mode 100644 index 0000000..56bc522 --- /dev/null +++ b/exercises/practice/luhn/.meta/spec_generator.moon @@ -0,0 +1,6 @@ +{ + module_name: 'Luhn', + + generate_test: (case, level) -> + indent "assert.is_#{case.expected} Luhn.is_valid #{quote case.input.value}", level +} diff --git a/exercises/practice/luhn/.meta/tests.toml b/exercises/practice/luhn/.meta/tests.toml new file mode 100644 index 0000000..c0be0c4 --- /dev/null +++ b/exercises/practice/luhn/.meta/tests.toml @@ -0,0 +1,76 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[792a7082-feb7-48c7-b88b-bbfec160865e] +description = "single digit strings can not be valid" + +[698a7924-64d4-4d89-8daa-32e1aadc271e] +description = "a single zero is invalid" + +[73c2f62b-9b10-4c9f-9a04-83cee7367965] +description = "a simple valid SIN that remains valid if reversed" + +[9369092e-b095-439f-948d-498bd076be11] +description = "a simple valid SIN that becomes invalid if reversed" + +[8f9f2350-1faf-4008-ba84-85cbb93ffeca] +description = "a valid Canadian SIN" + +[1cdcf269-6560-44fc-91f6-5819a7548737] +description = "invalid Canadian SIN" + +[656c48c1-34e8-4e60-9a5a-aad8a367810a] +description = "invalid credit card" + +[20e67fad-2121-43ed-99a8-14b5b856adb9] +description = "invalid long number with an even remainder" + +[7e7c9fc1-d994-457c-811e-d390d52fba5e] +description = "invalid long number with a remainder divisible by 5" + +[ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa] +description = "valid number with an even number of digits" + +[ef081c06-a41f-4761-8492-385e13c8202d] +description = "valid number with an odd number of spaces" + +[bef66f64-6100-4cbb-8f94-4c9713c5e5b2] +description = "valid strings with a non-digit added at the end become invalid" + +[2177e225-9ce7-40f6-b55d-fa420e62938e] +description = "valid strings with punctuation included become invalid" + +[ebf04f27-9698-45e1-9afe-7e0851d0fe8d] +description = "valid strings with symbols included become invalid" + +[08195c5e-ce7f-422c-a5eb-3e45fece68ba] +description = "single zero with space is invalid" + +[12e63a3c-f866-4a79-8c14-b359fc386091] +description = "more than a single zero is valid" + +[ab56fa80-5de8-4735-8a4a-14dae588663e] +description = "input digit 9 is correctly converted to output digit 9" + +[b9887ee8-8337-46c5-bc45-3bcab51bc36f] +description = "very long input is valid" + +[8a7c0e24-85ea-4154-9cf1-c2db90eabc08] +description = "valid luhn with an odd number of digits and non zero first digit" + +[39a06a5a-5bad-4e0f-b215-b042d46209b1] +description = "using ascii value for non-doubled non-digit isn't allowed" + +[f94cf191-a62f-4868-bc72-7253114aa157] +description = "using ascii value for doubled non-digit isn't allowed" + +[8b72ad26-c8be-49a2-b99c-bcc3bf631b33] +description = "non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed" diff --git a/exercises/practice/luhn/luhn.moon b/exercises/practice/luhn/luhn.moon new file mode 100644 index 0000000..c976b36 --- /dev/null +++ b/exercises/practice/luhn/luhn.moon @@ -0,0 +1,4 @@ +{ + is_valid: (input) -> + error 'Implement me' +} diff --git a/exercises/practice/luhn/luhn_spec.moon b/exercises/practice/luhn/luhn_spec.moon new file mode 100644 index 0000000..8a826fa --- /dev/null +++ b/exercises/practice/luhn/luhn_spec.moon @@ -0,0 +1,68 @@ +Luhn = require 'luhn' + +describe 'luhn', -> + it 'single digit strings can not be valid', -> + assert.is_false Luhn.is_valid '1' + + pending 'a single zero is invalid', -> + assert.is_false Luhn.is_valid '0' + + pending 'a simple valid SIN that remains valid if reversed', -> + assert.is_true Luhn.is_valid '059' + + pending 'a simple valid SIN that becomes invalid if reversed', -> + assert.is_true Luhn.is_valid '59' + + pending 'a valid Canadian SIN', -> + assert.is_true Luhn.is_valid '055 444 285' + + pending 'invalid Canadian SIN', -> + assert.is_false Luhn.is_valid '055 444 286' + + pending 'invalid credit card', -> + assert.is_false Luhn.is_valid '8273 1232 7352 0569' + + pending 'invalid long number with an even remainder', -> + assert.is_false Luhn.is_valid '1 2345 6789 1234 5678 9012' + + pending 'invalid long number with a remainder divisible by 5', -> + assert.is_false Luhn.is_valid '1 2345 6789 1234 5678 9013' + + pending 'valid number with an even number of digits', -> + assert.is_true Luhn.is_valid '095 245 88' + + pending 'valid number with an odd number of spaces', -> + assert.is_true Luhn.is_valid '234 567 891 234' + + pending 'valid strings with a non-digit added at the end become invalid', -> + assert.is_false Luhn.is_valid '059a' + + pending 'valid strings with punctuation included become invalid', -> + assert.is_false Luhn.is_valid '055-444-285' + + pending 'valid strings with symbols included become invalid', -> + assert.is_false Luhn.is_valid '055# 444$ 285' + + pending 'single zero with space is invalid', -> + assert.is_false Luhn.is_valid ' 0' + + pending 'more than a single zero is valid', -> + assert.is_true Luhn.is_valid '0000 0' + + pending 'input digit 9 is correctly converted to output digit 9', -> + assert.is_true Luhn.is_valid '091' + + pending 'very long input is valid', -> + assert.is_true Luhn.is_valid '9999999999 9999999999 9999999999 9999999999' + + pending 'valid luhn with an odd number of digits and non zero first digit', -> + assert.is_true Luhn.is_valid '109' + + pending "using ascii value for non-doubled non-digit isn't allowed", -> + assert.is_false Luhn.is_valid '055b 444 285' + + pending "using ascii value for doubled non-digit isn't allowed", -> + assert.is_false Luhn.is_valid ':9' + + pending "non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed", -> + assert.is_false Luhn.is_valid '59%59'