Skip to content

Commit

Permalink
fix luhn (exercism#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 authored Jul 11, 2023
1 parent 223aa13 commit d4b2a3f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 49 deletions.
3 changes: 2 additions & 1 deletion exercises/practice/luhn/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"alxndr",
"marocchino",
"meientau",
"rootulp"
"rootulp",
"meatball133"
],
"files": {
"solution": [
Expand Down
63 changes: 17 additions & 46 deletions exercises/practice/luhn/.meta/example.coffee
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions exercises/practice/luhn/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"

Expand Down
20 changes: 18 additions & 2 deletions exercises/practice/luhn/luhn.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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', ->
Expand Down Expand Up @@ -62,10 +66,22 @@ 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

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

0 comments on commit d4b2a3f

Please sign in to comment.