From d4b2a3f53ba7b123384a3690fc062fcee043bc0a Mon Sep 17 00:00:00 2001 From: meatball <69751659+meatball133@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:20:52 +0200 Subject: [PATCH] fix luhn (#206) --- exercises/practice/luhn/.meta/config.json | 3 +- exercises/practice/luhn/.meta/example.coffee | 63 ++++++-------------- exercises/practice/luhn/.meta/tests.toml | 9 +++ exercises/practice/luhn/luhn.spec.coffee | 20 ++++++- 4 files changed, 46 insertions(+), 49 deletions(-) diff --git a/exercises/practice/luhn/.meta/config.json b/exercises/practice/luhn/.meta/config.json index 827f78e..f97a57d 100644 --- a/exercises/practice/luhn/.meta/config.json +++ b/exercises/practice/luhn/.meta/config.json @@ -7,7 +7,8 @@ "alxndr", "marocchino", "meientau", - "rootulp" + "rootulp", + "meatball133" ], "files": { "solution": [ diff --git a/exercises/practice/luhn/.meta/example.coffee b/exercises/practice/luhn/.meta/example.coffee index 66cc67b..7dfaa54 100644 --- a/exercises/practice/luhn/.meta/example.coffee +++ b/exercises/practice/luhn/.meta/example.coffee @@ -1,46 +1,17 @@ -module.exports = class Luhn - constructor: (number) -> - @checkDigit = number % 10 - @addends = @calculateAddends(number) - @checksum = @calculateChecksum(@addends) - @valid = @determineIfValid(@checksum) - - calculateAddends: (number) -> - - numberAsString = "" + number + "" - numbers = numberAsString.split('') - - addends = (@calculateAddend(i,numbers) for i in [0..numbers.length-1]) - addends.reverse() - - calculateAddend: (i,numbers) -> - index = numbers.length - 1 - i - - currentAddend = parseInt(numbers[index]) - - if (i + 1) % 2 == 0 - currentAddend = currentAddend * 2 - if currentAddend > 10 - currentAddend = currentAddend - 9 - - currentAddend - - calculateChecksum: (numbers) -> - numbers.reduce (x,y) -> x + y - - determineIfValid: (sum) -> - (sum % 10 == 0) - - - @create = (number) -> - finalNumber = number * 10 - luhnNumber = new Luhn(finalNumber) - index = 0 - - while(!luhnNumber.valid) - finalNumber = number * 10 + index - luhnNumber = new Luhn(finalNumber) - break if luhnNumber.valid - index += 1 - - finalNumber +class Luhn + constructor: (@num) -> + + valid: -> + @num = @num.replace /\s+/g, '' + return false if @num.length < 2 + return false if @num.match /\D/ + sum = 0 + for digit, i in @num.split('').reverse() + digit = parseInt digit + if i % 2 == 1 + digit *= 2 + digit -= 9 if digit > 9 + sum += digit + sum % 10 == 0 + +module.exports = Luhn diff --git a/exercises/practice/luhn/.meta/tests.toml b/exercises/practice/luhn/.meta/tests.toml index 06c2880..c0be0c4 100644 --- a/exercises/practice/luhn/.meta/tests.toml +++ b/exercises/practice/luhn/.meta/tests.toml @@ -33,6 +33,9 @@ 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" @@ -57,6 +60,12 @@ 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" diff --git a/exercises/practice/luhn/luhn.spec.coffee b/exercises/practice/luhn/luhn.spec.coffee index bd10b44..fd1a244 100644 --- a/exercises/practice/luhn/luhn.spec.coffee +++ b/exercises/practice/luhn/luhn.spec.coffee @@ -26,8 +26,12 @@ describe 'Luhn', -> luhn = new Luhn('8273 1232 7352 0569') expect(luhn.valid()).toBe false - xit 'invalid credit card', -> - luhn = new Luhn('8273 1232 7352 0569') + xit 'invalid long number with an even remainder', -> + luhn = new Luhn('1 2345 6789 1234 5678 9012') + expect(luhn.valid()).toBe false + + xit 'invalid long number with a remainder divisible by 5', -> + luhn = new Luhn('1 2345 6789 1234 5678 9013') expect(luhn.valid()).toBe false xit 'valid number with an even number of digits', -> @@ -62,6 +66,14 @@ describe 'Luhn', -> luhn = new Luhn('091') expect(luhn.valid()).toBe true + xit 'very long input is valid', -> + luhn = new Luhn('9999999999 9999999999 9999999999 9999999999') + expect(luhn.valid()).toBe true + + xit 'valid luhn with an odd number of digits and non zero first digit', -> + luhn = new Luhn('109') + expect(luhn.valid()).toBe true + xit 'using ascii value for non-doubled non-digit isn\'t allowed', -> luhn = new Luhn('055b 444 285') expect(luhn.valid()).toBe false @@ -69,3 +81,7 @@ describe 'Luhn', -> xit 'using ascii value for doubled non-digit isn\'t allowed', -> luhn = new Luhn(':9') expect(luhn.valid()).toBe false + + xit 'non-numeric, non-space char in the middle with a sum that\'s divisible by 10 isn\'t allowed', -> + luhn = new Luhn('59%59') + expect(luhn.valid()).toBe false