Skip to content

Commit

Permalink
Add isbn-verifier exercise (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 19, 2024
1 parent 9648119 commit 05e662d
Show file tree
Hide file tree
Showing 12 changed files with 3,668 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "isbn-verifier",
"name": "ISBN Verifier",
"uuid": "b8d74744-3783-4c5e-9cc2-3efcb9aae236",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
]
},
Expand Down
42 changes: 42 additions & 0 deletions exercises/practice/isbn-verifier/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Instructions

The [ISBN-10 verification process][isbn-verification] is used to validate book identification numbers.
These normally contain dashes and look like: `3-598-21508-8`

## ISBN

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:

```text
(d₁ * 10 + d₂ * 9 + d₃ * 8 + d₄ * 7 + d₅ * 6 + d₆ * 5 + d₇ * 4 + d₈ * 3 + d₉ * 2 + d₁₀ * 1) mod 11 == 0
```

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

## Example

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

```text
(3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 1 * 5 + 5 * 4 + 0 * 3 + 8 * 2 + 8 * 1) mod 11 == 0
```

Since the result is 0, this proves that our ISBN is valid.

## Task

Given a string the program should check if the provided string is a valid ISBN-10.
Putting this into place requires some thinking about preprocessing/parsing of the string prior to calculating the check digit for the ISBN.

The program should be able to verify ISBN-10 both with and without separating dashes.

## Caveats

Converting from strings to numbers can be tricky in certain languages.
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.

[isbn-verification]: https://en.wikipedia.org/wiki/International_Standard_Book_Number
19 changes: 19 additions & 0 deletions exercises/practice/isbn-verifier/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"isbn_verifier.s"
],
"test": [
"isbn_verifier_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Check if a given string is a valid ISBN-10 number.",
"source": "Converting a string into a number and some basic processing utilizing a relatable real world example.",
"source_url": "https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_check_digit_calculation"
}
48 changes: 48 additions & 0 deletions exercises/practice/isbn-verifier/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.text
.globl is_valid

/* extern int is_valid(const char *isbn); */
is_valid:
mov x1, #10 /* number of digits remaining */
mov x4, #0 /* sum */
mov x5, #0 /* weighted sum */

.loop:
ldrb w2, [x0], #1 /* load byte, with post-increment */
cbz x2, .end

cmp x2, #45 /* hyphen */
beq .loop

sub x1, x1, #1 /* decrement number of digits remaining */
sub x3, x2, #48 /* '0' */
cmp x3, #10
bhs .non_digit /* unsigned >= */

.add:
add x4, x4, x3
add x5, x5, x4
b .loop

.non_digit:
tst x1, x1
bne .reject

mov x3, #10
cmp x2, #88 /* 'X' */
beq .add

.reject:
mov x0, #0
ret

.end:
tst x1, x1
bne .reject

mov x1, #11
udiv x4, x5, x1 /* weighted sum / 11 */
msub x3, x4, x1, x5 /* weighted sum % 11 */
tst x3, x3
cset x0, eq
ret
67 changes: 67 additions & 0 deletions exercises/practice/isbn-verifier/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[0caa3eac-d2e3-4c29-8df8-b188bc8c9292]
description = "valid isbn"

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

[4164bfee-fb0a-4a1c-9f70-64c6a1903dcd]
description = "valid isbn with a check digit of 10"

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

[9416f4a5-fe01-4b61-a07b-eb75892ef562]
description = "invalid check digit in isbn is not treated as zero"

[c19ba0c4-014f-4dc3-a63f-ff9aefc9b5ec]
description = "invalid character in isbn is not treated as zero"

[28025280-2c39-4092-9719-f3234b89c627]
description = "X is only valid as a check digit"

[f6294e61-7e79-46b3-977b-f48789a4945b]
description = "valid isbn without separating dashes"

[185ab99b-3a1b-45f3-aeec-b80d80b07f0b]
description = "isbn without separating dashes and X as check digit"

[7725a837-ec8e-4528-a92a-d981dd8cf3e2]
description = "isbn without check digit and dashes"

[47e4dfba-9c20-46ed-9958-4d3190630bdf]
description = "too long isbn and no dashes"

[737f4e91-cbba-4175-95bf-ae630b41fb60]
description = "too short isbn"

[5458a128-a9b6-4ff8-8afb-674e74567cef]
description = "isbn without check digit"

[70b6ad83-d0a2-4ca7-a4d5-a9ab731800f7]
description = "check digit of X should not be used for 0"

[94610459-55ab-4c35-9b93-ff6ea1a8e562]
description = "empty isbn"

[7bff28d4-d770-48cc-80d6-b20b3a0fb46c]
description = "input is 9 characters"

[ed6e8d1b-382c-4081-8326-8b772c581fec]
description = "invalid characters are not ignored after checking length"

[daad3e58-ce00-4395-8a8e-e3eded1cdc86]
description = "invalid characters are not ignored before checking length"

[fb5e48d8-7c03-4bfb-a088-b101df16fdc3]
description = "input is too long but contains a valid isbn"
36 changes: 36 additions & 0 deletions exercises/practice/isbn-verifier/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/isbn-verifier/isbn_verifier.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl is_valid

is_valid:
ret
127 changes: 127 additions & 0 deletions exercises/practice/isbn-verifier/isbn_verifier_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#include "vendor/unity.h"

extern int is_valid(const char *isbn);

void setUp(void) {
}

void tearDown(void) {
}

void test_valid_isbn(void) {
TEST_ASSERT_TRUE(is_valid("3-598-21508-8"));
}

void test_invalid_isbn_check_digit(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3-598-21508-9"));
}

void test_valid_isbn_with_a_check_digit_of_10(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(is_valid("3-598-21507-X"));
}

void test_check_digit_is_a_character_other_than_x(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3-598-21507-A"));
}

void test_invalid_check_digit_in_isbn_is_not_treated_as_zero(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("4-598-21507-B"));
}

void test_invalid_character_in_isbn_is_not_treated_as_zero(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3-598-P1581-X"));
}

void test_x_is_only_valid_as_a_check_digit(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3-598-2X507-9"));
}

void test_valid_isbn_without_separating_dashes(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(is_valid("3598215088"));
}

void test_isbn_without_separating_dashes_and_x_as_check_digit(void) {
TEST_IGNORE();
TEST_ASSERT_TRUE(is_valid("359821507X"));
}

void test_isbn_without_check_digit_and_dashes(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("359821507"));
}

void test_too_long_isbn_and_no_dashes(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3598215078X"));
}

void test_too_short_isbn(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("00"));
}

void test_isbn_without_check_digit(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3-598-21507"));
}

void test_check_digit_of_x_should_not_be_used_for_0(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3-598-21515-X"));
}

void test_empty_isbn(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid(""));
}

void test_input_is_9_characters(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("134456729"));
}

void test_invalid_characters_are_not_ignored_after_checking_length(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3132P34035"));
}

void test_invalid_characters_are_not_ignored_before_checking_length(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("3598P215088"));
}

void test_input_is_too_long_but_contains_a_valid_isbn(void) {
TEST_IGNORE();
TEST_ASSERT_FALSE(is_valid("98245726788"));
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_valid_isbn);
RUN_TEST(test_invalid_isbn_check_digit);
RUN_TEST(test_valid_isbn_with_a_check_digit_of_10);
RUN_TEST(test_check_digit_is_a_character_other_than_x);
RUN_TEST(test_invalid_check_digit_in_isbn_is_not_treated_as_zero);
RUN_TEST(test_invalid_character_in_isbn_is_not_treated_as_zero);
RUN_TEST(test_x_is_only_valid_as_a_check_digit);
RUN_TEST(test_valid_isbn_without_separating_dashes);
RUN_TEST(test_isbn_without_separating_dashes_and_x_as_check_digit);
RUN_TEST(test_isbn_without_check_digit_and_dashes);
RUN_TEST(test_too_long_isbn_and_no_dashes);
RUN_TEST(test_too_short_isbn);
RUN_TEST(test_isbn_without_check_digit);
RUN_TEST(test_check_digit_of_x_should_not_be_used_for_0);
RUN_TEST(test_empty_isbn);
RUN_TEST(test_input_is_9_characters);
RUN_TEST(test_invalid_characters_are_not_ignored_after_checking_length);
RUN_TEST(test_invalid_characters_are_not_ignored_before_checking_length);
RUN_TEST(test_input_is_too_long_but_contains_a_valid_isbn);
return UNITY_END();
}
Loading

0 comments on commit 05e662d

Please sign in to comment.