-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync Luhn with problem specifications (#199)
- Loading branch information
Showing
6 changed files
with
105 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
"contributors": [ | ||
"adolfopa", | ||
"etrepum", | ||
"yurrriq" | ||
"yurrriq", | ||
"kahgoh" | ||
], | ||
"files": { | ||
"solution": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,30 @@ | ||
(defmodule luhn | ||
(export (valid? 1) | ||
(create 1) | ||
(checksum 1))) | ||
(export (valid? 1)) | ||
) | ||
|
||
(defun valid? (number) (== (rem (checksum number) 10) 0)) | ||
(defun valid? (number) | ||
(case (checksum (lists:reverse number) 0 0) | ||
((tuple 'ok count total) (when (> count 1)) (== (rem total 10) 0)) | ||
(_ 'false) | ||
) | ||
) | ||
|
||
(defun create (number) | ||
(lists:flatten `(,number ,(- #\: (rem (checksum (++ number "0")) 10))))) | ||
(defun checksum | ||
(('() count total) (tuple 'ok count total)) | ||
(((cons 32 reversed-number) count total) (checksum reversed-number count total)) | ||
(((cons h reversed-number) count total) | ||
(when (=< #\0 h) (=< h #\9)) | ||
(checksum reversed-number | ||
(+ count 1) | ||
(+ total (number-to-add count (- h #\0))) | ||
) | ||
) | ||
((_ count total) (tuple 'invalid count total)) | ||
) | ||
|
||
(defun checksum (number) | ||
(checksum | ||
(lists:reverse | ||
(lists:filter | ||
(lambda (c) | ||
(=< #\0 c #\9)) | ||
number)) | ||
'odd | ||
0)) | ||
(defun number-to-add | ||
((count digit) (when (== (rem count 2) 0)) digit) | ||
((_ digit) (when (< digit 5)) (* digit 2)) | ||
((_ digit) (- (* digit 2) 9)) | ||
) | ||
|
||
(defun checksum | ||
([() _ total] total) | ||
([(cons h reversed-number) 'odd total] | ||
(checksum reversed-number 'even (- (+ total h) #\0))) | ||
([(cons h reversed-number) 'even total] (when (< h #\5)) | ||
(checksum reversed-number 'odd (+ total (* (- h #\0) 2)))) | ||
([(cons h reversed-number) 'even total] (when (>= h #\5)) | ||
(checksum reversed-number 'odd (- (+ total (* (- h #\0) 2)) 9)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
(defmodule luhn | ||
(export (valid? 1)) | ||
) | ||
|
||
; Please implement the `valid?` function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters