ISBN13 check digit: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
(Same tests for all - if that's OK?) |
||
Line 2: | Line 2: | ||
;Task: |
;Task: |
||
Validate the check digit of an ISBN-13 code. |
Validate the check digit of an ISBN-13 code. |
||
Multiply every other digit by 3. Add the digits together. Take the remainder of division by 10. If it is 0, the ISBN-13 check digit is correct. |
Multiply every other digit by 3. Add the digits together. Take the remainder of division by 10. If it is 0, the ISBN-13 check digit is correct. |
||
Use the following codes for testing: |
|||
<pre>978-1734314502 |
|||
978-1734314509 |
|||
978-1788399081 |
|||
978-1788399083</pre> |
|||
Show output here, on this page |
|||
;See also: |
;See also: |
||
<p>See https://isbn-information.com/the-13-digit-isbn.html for details on the method of validation. |
<p>See https://isbn-information.com/the-13-digit-isbn.html for details on the method of validation. |
Revision as of 17:17, 4 December 2019
ISBN13 check digit
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Validate the check digit of an ISBN-13 code. Multiply every other digit by 3. Add the digits together. Take the remainder of division by 10. If it is 0, the ISBN-13 check digit is correct.
Use the following codes for testing:
978-1734314502 978-1734314509 978-1788399081 978-1788399083
Show output here, on this page
- See also
See https://isbn-information.com/the-13-digit-isbn.html for details on the method of validation.
langur
<lang langur>val .isbn13checkdigit = f(.n) {
val .s = replace(.n, "-", "") if len(.s) != 13 { return false } # multiply every other number by 3 val .nums = map( f(.i, .d) if(.i rem 2 == 0: .d x 3; .d), series(13), map f .c-'0', s2cp .s, ) fold(f .x + .y, .nums) rem 10 == 0
}
val .tests = h{
"978-1734314502": true, "978-1734314509": false, "978-1788399081": true, "978-1788399083": false,
}
for .key in sort(keys .tests) {
val .pass = .isbn13checkdigit(.key) write .key, ": ", if(.pass: "good"; "bad") writeln if(.pass == .tests[.key]: ""; " (ISBN-13 CHECK DIGIT TEST FAILED)")
}</lang>
- Output:
978-1734314502: good 978-1734314509: bad 978-1788399081: good 978-1788399083: bad