Skip to content

Commit eadaadf

Browse files
Sync isbn-verifier (#1316)
1 parent 4561bb9 commit eadaadf

File tree

3 files changed

+64
-27
lines changed

3 files changed

+64
-27
lines changed
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# Instructions
22

3-
The [ISBN-10 verification process](https://en.wikipedia.org/wiki/International_Standard_Book_Number) is used to validate book identification
4-
numbers. These normally contain dashes and look like: `3-598-21508-8`
3+
The [ISBN-10 verification process][isbn-verification] is used to validate book identification numbers.
4+
These normally contain dashes and look like: `3-598-21508-8`
55

66
## ISBN
77

8-
The ISBN-10 format is 9 digits (0 to 9) plus one check character (either a digit or an X only). In the case the check character is an X, this represents the value '10'. These may be communicated with or without hyphens, and can be checked for their validity by the following formula:
8+
The ISBN-10 format is 9 digits (0 to 9) plus one check character (either a digit or an X only).
9+
In the case the check character is an X, this represents the value '10'.
10+
These may be communicated with or without hyphens, and can be checked for their validity by the following formula:
911

10-
```
11-
(x1 * 10 + x2 * 9 + x3 * 8 + x4 * 7 + x5 * 6 + x6 * 5 + x7 * 4 + x8 * 3 + x9 * 2 + x10 * 1) mod 11 == 0
12+
```text
13+
(d₁ * 10 + d₂ * 9 + d₃ * 8 + d₄ * 7 + d₅ * 6 + d₆ * 5 + d₇ * 4 + d₈ * 3 + d₉ * 2 + d₁₀ * 1) mod 11 == 0
1214
```
1315

1416
If the result is 0, then it is a valid ISBN-10, otherwise it is invalid.
1517

1618
## Example
1719

18-
Let's take the ISBN-10 `3-598-21508-8`. We plug it in to the formula, and get:
20+
Let's take the ISBN-10 `3-598-21508-8`.
21+
We plug it in to the formula, and get:
1922

20-
```
23+
```text
2124
(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0
2225
```
2326

@@ -33,10 +36,7 @@ The program should be able to verify ISBN-10 both with and without separating da
3336
## Caveats
3437

3538
Converting from strings to numbers can be tricky in certain languages.
36-
Now, it's even trickier since the check digit of an ISBN-10 may be 'X' (representing '10'). For instance `3-598-21507-X` is a valid ISBN-10.
37-
38-
## Bonus tasks
39-
40-
- Generate a valid ISBN-13 from the input ISBN-10 (and maybe verify it again with a derived verifier).
39+
Now, it's even trickier since the check digit of an ISBN-10 may be 'X' (representing '10').
40+
For instance `3-598-21507-X` is a valid ISBN-10.
4141

42-
- Generate valid ISBN, maybe even from a given starting ISBN.
42+
[isbn-verification]: https://en.wikipedia.org/wiki/International_Standard_Book_Number

exercises/practice/isbn-verifier/.meta/tests.toml

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[0caa3eac-d2e3-4c29-8df8-b188bc8c9292]
6-
description = "valid isbn number"
13+
description = "valid isbn"
714

815
[19f76b53-7c24-45f8-87b8-4604d0ccd248]
916
description = "invalid isbn check digit"
1017

1118
[4164bfee-fb0a-4a1c-9f70-64c6a1903dcd]
12-
description = "valid isbn number with a check digit of 10"
19+
description = "valid isbn with a check digit of 10"
1320

1421
[3ed50db1-8982-4423-a993-93174a20825c]
1522
description = "check digit is a character other than X"
1623

24+
[9416f4a5-fe01-4b61-a07b-eb75892ef562]
25+
description = "invalid check digit in isbn is not treated as zero"
26+
1727
[c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec]
18-
description = "invalid character in isbn"
28+
description = "invalid character in isbn is not treated as zero"
1929

2030
[28025280-2c39-4092-9719-f3234b89c627]
2131
description = "X is only valid as a check digit"
@@ -48,7 +58,10 @@ description = "empty isbn"
4858
description = "input is 9 characters"
4959

5060
[ed6e8d1b-382c-4081-8326-8b772c581fec]
51-
description = "invalid characters are not ignored"
61+
description = "invalid characters are not ignored after checking length"
62+
63+
[daad3e58-ce00-4395-8a8e-e3eded1cdc86]
64+
description = "invalid characters are not ignored before checking length"
5265

5366
[fb5e48d8-7c03-4bfb-a088-b101df16fdc3]
5467
description = "input is too long but contains a valid isbn"

exercises/practice/isbn-verifier/isbn-verifier.test.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
import { isValid } from './isbn-verifier'
22

33
describe('ISBN Verifier', () => {
4-
it('valid isbn number', () => {
4+
it('valid isbn', () => {
55
expect(isValid('3-598-21508-8')).toBeTruthy()
66
})
77

88
xit('invalid isbn check digit', () => {
99
expect(isValid('3-598-21508-9')).toBeFalsy()
1010
})
1111

12-
xit('valid isbn number with a check digit of 10', () => {
12+
xit('valid isbn with a check digit of 10', () => {
1313
expect(isValid('3-598-21507-X')).toBeTruthy()
1414
})
1515

1616
xit('check digit is a character other than X', () => {
1717
expect(isValid('3-598-21507-A')).toBeFalsy()
1818
})
1919

20-
xit('invalid character in isbn', () => {
20+
xit('invalid check digit in isbn is not treated as zero', () => {
21+
expect(isValid('4-598-21507-B')).toBeFalsy()
22+
})
23+
24+
xit('invalid character in isbn is not treated as zero', () => {
2125
expect(isValid('3-598-2K507-0')).toBeFalsy()
2226
})
2327

@@ -37,16 +41,20 @@ describe('ISBN Verifier', () => {
3741
expect(isValid('359821507')).toBeFalsy()
3842
})
3943

44+
xit('too long isbn', () => {
45+
expect(isValid('3-598-21507-XX')).toBeFalsy()
46+
})
47+
4048
xit('too long isbn and no dashes', () => {
4149
expect(isValid('3598215078X')).toBeFalsy()
4250
})
4351

44-
xit('isbn without check digit', () => {
45-
expect(isValid('3-598-21507')).toBeFalsy()
52+
xit('too short isbn', () => {
53+
expect(isValid('00')).toBeFalsy()
4654
})
4755

48-
xit('too long isbn', () => {
49-
expect(isValid('3-598-21507-XX')).toBeFalsy()
56+
xit('isbn without check digit', () => {
57+
expect(isValid('3-598-21507')).toBeFalsy()
5058
})
5159

5260
xit('check digit of X should not be used for 0', () => {
@@ -56,4 +64,20 @@ describe('ISBN Verifier', () => {
5664
xit('empty isbn', () => {
5765
expect(isValid('')).toBeFalsy()
5866
})
67+
68+
xit('input is 9 characters', () => {
69+
expect(isValid('134456729')).toBeFalsy()
70+
})
71+
72+
xit('invalid characters are not ignored after checking length', () => {
73+
expect(isValid('3132P34035')).toBeFalsy()
74+
})
75+
76+
xit('invalid characters are not ignored before checking length', () => {
77+
expect(isValid('3598P215088')).toBeFalsy()
78+
})
79+
80+
xit('input is too long but contains a valid isbn', () => {
81+
expect(isValid('98245726788')).toBeFalsy()
82+
})
5983
})

0 commit comments

Comments
 (0)