Skip to content

Commit eeda0fa

Browse files
authored
Merge branch 'main' into book_store_main
2 parents df34c2f + 435286d commit eeda0fa

9 files changed

Lines changed: 397 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,14 @@
435435
"prerequisites": [],
436436
"difficulty": 1
437437
},
438+
{
439+
"slug": "isbn-verifier",
440+
"name": "ISBN Verifier",
441+
"uuid": "941750f4-d641-44b9-a7eb-7be50cd93cee",
442+
"practices": [],
443+
"prerequisites": [],
444+
"difficulty": 2
445+
},
438446
{
439447
"slug": "state-of-tic-tac-toe",
440448
"name": "State of Tic-Tac-Toe",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Instructions
2+
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`
5+
6+
## ISBN
7+
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:
11+
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
14+
```
15+
16+
If the result is 0, then it is a valid ISBN-10, otherwise it is invalid.
17+
18+
## Example
19+
20+
Let's take the ISBN-10 `3-598-21508-8`.
21+
We plug it in to the formula, and get:
22+
23+
```text
24+
(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0
25+
```
26+
27+
Since the result is 0, this proves that our ISBN is valid.
28+
29+
## Task
30+
31+
Given a string the program should check if the provided string is a valid ISBN-10.
32+
Putting this into place requires some thinking about preprocessing/parsing of the string prior to calculating the check digit for the ISBN.
33+
34+
The program should be able to verify ISBN-10 both with and without separating dashes.
35+
36+
## Caveats
37+
38+
Converting from strings to numbers can be tricky in certain languages.
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.
41+
42+
[isbn-verification]: https://en.wikipedia.org/wiki/International_Standard_Book_Number
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"zcl_isbn_verifier.clas.abap"
8+
],
9+
"test": [
10+
"zcl_isbn_verifier.clas.testclasses.abap"
11+
],
12+
"example": [
13+
".meta/zcl_isbn_verifier.clas.abap"
14+
]
15+
},
16+
"blurb": "Check if a given string is a valid ISBN-10 number.",
17+
"source": "Converting a string into a number and some basic processing utilizing a relatable real world example.",
18+
"source_url": "https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation"
19+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.
11+
12+
[0caa3eac-d2e3-4c29-8df8-b188bc8c9292]
13+
description = "valid isbn"
14+
15+
[19f76b53-7c24-45f8-87b8-4604d0ccd248]
16+
description = "invalid isbn check digit"
17+
18+
[4164bfee-fb0a-4a1c-9f70-64c6a1903dcd]
19+
description = "valid isbn with a check digit of 10"
20+
21+
[3ed50db1-8982-4423-a993-93174a20825c]
22+
description = "check digit is a character other than X"
23+
24+
[9416f4a5-fe01-4b61-a07b-eb75892ef562]
25+
description = "invalid check digit in isbn is not treated as zero"
26+
27+
[c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec]
28+
description = "invalid character in isbn is not treated as zero"
29+
30+
[28025280-2c39-4092-9719-f3234b89c627]
31+
description = "X is only valid as a check digit"
32+
33+
[8005b57f-f194-44ee-88d2-a77ac4142591]
34+
description = "only one check digit is allowed"
35+
36+
[fdb14c99-4cf8-43c5-b06d-eb1638eff343]
37+
description = "X is not substituted by the value 10"
38+
39+
[f6294e61-7e79-46b3-977b-f48789a4945b]
40+
description = "valid isbn without separating dashes"
41+
42+
[185ab99b-3a1b-45f3-aeec-b80d80b07f0b]
43+
description = "isbn without separating dashes and X as check digit"
44+
45+
[7725a837-ec8e-4528-a92a-d981dd8cf3e2]
46+
description = "isbn without check digit and dashes"
47+
48+
[47e4dfba-9c20-46ed-9958-4d3190630bdf]
49+
description = "too long isbn and no dashes"
50+
51+
[737f4e91-cbba-4175-95bf-ae630b41fb60]
52+
description = "too short isbn"
53+
54+
[5458a128-a9b6-4ff8-8afb-674e74567cef]
55+
description = "isbn without check digit"
56+
57+
[70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7]
58+
description = "check digit of X should not be used for 0"
59+
60+
[94610459-55ab-4c35-9b93-ff6ea1a8e562]
61+
description = "empty isbn"
62+
63+
[7bff28d4-d770-48cc-80d6-b20b3a0fb46c]
64+
description = "input is 9 characters"
65+
66+
[ed6e8d1b-382c-4081-8326-8b772c581fec]
67+
description = "invalid characters are not ignored after checking length"
68+
69+
[daad3e58-ce00-4395-8a8e-e3eded1cdc86]
70+
description = "invalid characters are not ignored before checking length"
71+
72+
[fb5e48d8-7c03-4bfb-a088-b101df16fdc3]
73+
description = "input is too long but contains a valid isbn"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
CLASS zcl_isbn_verifier DEFINITION
2+
PUBLIC
3+
FINAL
4+
CREATE PUBLIC .
5+
6+
PUBLIC SECTION.
7+
METHODS is_valid
8+
IMPORTING
9+
VALUE(isbn) TYPE string
10+
RETURNING
11+
VALUE(result) TYPE abap_bool.
12+
PROTECTED SECTION.
13+
PRIVATE SECTION.
14+
15+
ENDCLASS.
16+
17+
18+
CLASS zcl_isbn_verifier IMPLEMENTATION.
19+
METHOD is_valid.
20+
DATA(digits) = replace( val = isbn sub = '-' with = '' occ = 0 ).
21+
22+
IF strlen( digits ) <> 10 OR NOT contains( val = digits regex = '^[0-9]{9}[0-9X]$' ).
23+
RETURN.
24+
ENDIF.
25+
26+
DATA(total) = 0.
27+
28+
DO 10 TIMES.
29+
DATA(character) = substring( val = digits off = sy-index - 1 len = 1 ).
30+
DATA(digit) = COND i( WHEN character = 'X' THEN 10 ELSE character ).
31+
total += digit * ( 11 - sy-index ).
32+
ENDDO.
33+
34+
result = xsdbool( total MOD 11 = 0 ).
35+
ENDMETHOD.
36+
37+
38+
ENDCLASS.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<abapGit version="v1.0.0" serializer="LCL_OBJECT_DEVC" serializer_version="v1.0.0">
3+
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0">
4+
<asx:values>
5+
<DEVC>
6+
<CTEXT>Exercism: ISBN Verifier</CTEXT>
7+
</DEVC>
8+
</asx:values>
9+
</asx:abap>
10+
</abapGit>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CLASS zcl_isbn_verifier DEFINITION
2+
PUBLIC
3+
FINAL
4+
CREATE PUBLIC .
5+
6+
PUBLIC SECTION.
7+
METHODS is_valid
8+
IMPORTING
9+
VALUE(isbn) TYPE string
10+
RETURNING
11+
VALUE(result) TYPE abap_bool.
12+
PROTECTED SECTION.
13+
PRIVATE SECTION.
14+
15+
ENDCLASS.
16+
17+
18+
CLASS zcl_isbn_verifier IMPLEMENTATION.
19+
METHOD is_valid.
20+
" add solution here
21+
ENDMETHOD.
22+
23+
24+
ENDCLASS.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
CLASS ltcl_isbn_verifier DEFINITION FOR TESTING
2+
DURATION SHORT
3+
RISK LEVEL HARMLESS.
4+
5+
PRIVATE SECTION.
6+
DATA cut TYPE REF TO zcl_isbn_verifier.
7+
METHODS setup.
8+
METHODS test_valid_isbn FOR TESTING RAISING cx_static_check.
9+
METHODS test_invalid_check_digit FOR TESTING RAISING cx_static_check.
10+
METHODS test_valid_x_check_digit FOR TESTING RAISING cx_static_check.
11+
METHODS test_check_digit_not_x FOR TESTING RAISING cx_static_check.
12+
METHODS test_invalid_check_not_zero FOR TESTING RAISING cx_static_check.
13+
METHODS test_invalid_char_not_zero FOR TESTING RAISING cx_static_check.
14+
METHODS test_x_only_check_digit FOR TESTING RAISING cx_static_check.
15+
METHODS test_one_check_digit_allowed FOR TESTING RAISING cx_static_check.
16+
METHODS test_x_not_substituted FOR TESTING RAISING cx_static_check.
17+
METHODS test_valid_without_dashes FOR TESTING RAISING cx_static_check.
18+
METHODS test_without_dashes_x FOR TESTING RAISING cx_static_check.
19+
METHODS test_missing_check_no_dashes FOR TESTING RAISING cx_static_check.
20+
METHODS test_too_long_no_dashes FOR TESTING RAISING cx_static_check.
21+
METHODS test_too_short FOR TESTING RAISING cx_static_check.
22+
METHODS test_missing_check_digit FOR TESTING RAISING cx_static_check.
23+
METHODS test_x_not_for_zero FOR TESTING RAISING cx_static_check.
24+
METHODS test_empty_isbn FOR TESTING RAISING cx_static_check.
25+
METHODS test_nine_characters FOR TESTING RAISING cx_static_check.
26+
METHODS test_invalid_after_length FOR TESTING RAISING cx_static_check.
27+
METHODS test_invalid_before_length FOR TESTING RAISING cx_static_check.
28+
METHODS test_too_long_contains_valid FOR TESTING RAISING cx_static_check.
29+
30+
ENDCLASS.
31+
32+
33+
CLASS ltcl_isbn_verifier IMPLEMENTATION.
34+
35+
METHOD setup.
36+
cut = NEW zcl_isbn_verifier( ).
37+
ENDMETHOD.
38+
39+
METHOD test_valid_isbn.
40+
cl_abap_unit_assert=>assert_equals(
41+
act = cut->is_valid( '3-598-21508-8' )
42+
exp = abap_true ).
43+
ENDMETHOD.
44+
45+
METHOD test_invalid_check_digit.
46+
cl_abap_unit_assert=>assert_equals(
47+
act = cut->is_valid( '3-598-21508-9' )
48+
exp = abap_false ).
49+
ENDMETHOD.
50+
51+
METHOD test_valid_x_check_digit.
52+
cl_abap_unit_assert=>assert_equals(
53+
act = cut->is_valid( '3-598-21507-X' )
54+
exp = abap_true ).
55+
ENDMETHOD.
56+
57+
METHOD test_check_digit_not_x.
58+
cl_abap_unit_assert=>assert_equals(
59+
act = cut->is_valid( '3-598-21507-A' )
60+
exp = abap_false ).
61+
ENDMETHOD.
62+
63+
METHOD test_invalid_check_not_zero.
64+
cl_abap_unit_assert=>assert_equals(
65+
act = cut->is_valid( '4-598-21507-B' )
66+
exp = abap_false ).
67+
ENDMETHOD.
68+
69+
METHOD test_invalid_char_not_zero.
70+
cl_abap_unit_assert=>assert_equals(
71+
act = cut->is_valid( '3-598-P1581-X' )
72+
exp = abap_false ).
73+
ENDMETHOD.
74+
75+
METHOD test_x_only_check_digit.
76+
cl_abap_unit_assert=>assert_equals(
77+
act = cut->is_valid( '3-598-2X507-9' )
78+
exp = abap_false ).
79+
ENDMETHOD.
80+
81+
METHOD test_one_check_digit_allowed.
82+
cl_abap_unit_assert=>assert_equals(
83+
act = cut->is_valid( '3-598-21508-96' )
84+
exp = abap_false ).
85+
ENDMETHOD.
86+
87+
METHOD test_x_not_substituted.
88+
cl_abap_unit_assert=>assert_equals(
89+
act = cut->is_valid( '3-598-2X507-5' )
90+
exp = abap_false ).
91+
ENDMETHOD.
92+
93+
METHOD test_valid_without_dashes.
94+
cl_abap_unit_assert=>assert_equals(
95+
act = cut->is_valid( '3598215088' )
96+
exp = abap_true ).
97+
ENDMETHOD.
98+
99+
METHOD test_without_dashes_x.
100+
cl_abap_unit_assert=>assert_equals(
101+
act = cut->is_valid( '359821507X' )
102+
exp = abap_true ).
103+
ENDMETHOD.
104+
105+
METHOD test_missing_check_no_dashes.
106+
cl_abap_unit_assert=>assert_equals(
107+
act = cut->is_valid( '359821507' )
108+
exp = abap_false ).
109+
ENDMETHOD.
110+
111+
METHOD test_too_long_no_dashes.
112+
cl_abap_unit_assert=>assert_equals(
113+
act = cut->is_valid( '3598215078X' )
114+
exp = abap_false ).
115+
ENDMETHOD.
116+
117+
METHOD test_too_short.
118+
cl_abap_unit_assert=>assert_equals(
119+
act = cut->is_valid( '00' )
120+
exp = abap_false ).
121+
ENDMETHOD.
122+
123+
METHOD test_missing_check_digit.
124+
cl_abap_unit_assert=>assert_equals(
125+
act = cut->is_valid( '3-598-21507' )
126+
exp = abap_false ).
127+
ENDMETHOD.
128+
129+
METHOD test_x_not_for_zero.
130+
cl_abap_unit_assert=>assert_equals(
131+
act = cut->is_valid( '3-598-21515-X' )
132+
exp = abap_false ).
133+
ENDMETHOD.
134+
135+
METHOD test_empty_isbn.
136+
cl_abap_unit_assert=>assert_equals(
137+
act = cut->is_valid( '' )
138+
exp = abap_false ).
139+
ENDMETHOD.
140+
141+
METHOD test_nine_characters.
142+
cl_abap_unit_assert=>assert_equals(
143+
act = cut->is_valid( '134456729' )
144+
exp = abap_false ).
145+
ENDMETHOD.
146+
147+
METHOD test_invalid_after_length.
148+
cl_abap_unit_assert=>assert_equals(
149+
act = cut->is_valid( '3132P34035' )
150+
exp = abap_false ).
151+
ENDMETHOD.
152+
153+
METHOD test_invalid_before_length.
154+
cl_abap_unit_assert=>assert_equals(
155+
act = cut->is_valid( '3598P215088' )
156+
exp = abap_false ).
157+
ENDMETHOD.
158+
159+
METHOD test_too_long_contains_valid.
160+
cl_abap_unit_assert=>assert_equals(
161+
act = cut->is_valid( '98245726788' )
162+
exp = abap_false ).
163+
ENDMETHOD.
164+
165+
166+
ENDCLASS.

0 commit comments

Comments
 (0)