Skip to content

Commit

Permalink
Add exercise: isbn-verifier (#218)
Browse files Browse the repository at this point in the history
* Add exercise: isbn-verifier

* Correct isbn-verifier example

* Update  `name` to header for exercise page

* Reduce indent to 2 spaces for consistency
  • Loading branch information
simisc committed Jul 20, 2023
1 parent 1bd4d42 commit eed4aa8
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@
"prerequisites": [],
"difficulty": 1
},
{
"slug": "isbn-verifier",
"name": "ISBN Verifier",
"uuid": "a9880507-d14c-4fb1-8320-94c63a15b7d2",
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "two-fer",
"name": "Two-Fer",
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": [
"simisc"
],
"files": {
"solution": [
"isbn_verifier.f90"
],
"test": [
"isbn_verifier_test.f90"
],
"example": [
".meta/example.f90"
]
},
"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"
}
34 changes: 34 additions & 0 deletions exercises/practice/isbn-verifier/.meta/example.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module isbn_verifier
implicit none

contains

function isValid(isbn) result(valid)
character(*), intent(in) :: isbn
logical :: valid
integer :: counter, checksum, i, digit

counter = 10
checksum = 0

do i = 1, len_trim(isbn)

if ('0' <= isbn(i:i) .and. isbn(i:i) <= '9') then
digit = ichar(isbn(i:i)) - ichar('0')
checksum = checksum + counter * digit
counter = counter - 1
else if (isbn(i:i) == 'X' .and. counter == 1) then
checksum = checksum + 10
counter = counter - 1
else if (isbn(i:i) /= '-') then
valid = .false.
return
end if

if (counter < 0) exit
end do

valid = counter == 0 .and. mod(checksum, 11) == 0
end function isValid

end module isbn_verifier
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"
71 changes: 71 additions & 0 deletions exercises/practice/isbn-verifier/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.0.0)

# Name the project after the exercise
project(${exercise} Fortran)

# set debug build default
set(CMAKE_BUILD_TYPE Debug)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Activate Fortran compiler warnings
if(CMAKE_Fortran_COMPILER_ID MATCHES "Intel") # Intel fortran
if(WIN32)
set (CCMAKE_Fortran_FLAG ${CCMAKE_Fortran_FLAGS} "/warn:all")
else()
set (CMAKE_Fortran_FLAGS ${CCMAKE_Fortran_FLAGS} "-warn all")
endif()
endif()
if(CMAKE_Fortran_COMPILER_ID MATCHES "GNU") # GFrotran
set (CMAKE_Fortran_FLAGS ${CCMAKE_Fortran_FLAGS} "-std=f2008 -W -Wall -Wextra -pedantic -fbacktrace")
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
add_definitions(-DEXERCISM_RUN_ALL_TESTS)
set(exercise_f90 ${file}_build_all.f90)
else()
# if building in exercise folder add testlib
set(testlib_path ${CMAKE_CURRENT_SOURCE_DIR}/testlib)
if (NOT EXISTS ${testlib_path})
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../../../testlib) # in git repo locally?
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/../../../testlib DESTINATION ${CMAKE_CURRENT_SOURCE_DIR} PATTERN *)
else() # get from git with http
message("Downloading testlib from https://raw.githubusercontent.com/exercism/fortran/master")
file(MAKE_DIRECTORY ${testlib_path} )
file(DOWNLOAD https://raw.githubusercontent.com/exercism/fortran/master/testlib/TesterMain.f90 ${testlib_path}/TesterMain.f90 SHOW_PROGRESS)
file(DOWNLOAD https://raw.githubusercontent.com/exercism/fortran/master/testlib/CMakeLists.txt ${testlib_path}/CMakeLists.txt SHOW_PROGRESS)
endif()
endif()
# add lib to build
add_subdirectory(testlib)
include_directories(testlib ${CMAKE_CURRENT_BINARY_DIR}/testlib)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.f90)
set(exercise_f90 ${file}.f90)
endif()
endif()

# Get test_files if exercise needs it
file(GLOB test_files test_files/*.*)
foreach(test_file ${test_files})
message("Copying ${test_file} to binary directory for example ${file}")
configure_file(${test_file} . COPYONLY)
endforeach()

# Build executable from sources and headers
add_executable(${exercise} ${exercise_f90} ${file}_test.f90 )

target_link_libraries(${exercise} TesterMain)

include(CTest)

add_test (${exercise}_test ${exercise} )

# Run the tests on every build
#add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})

13 changes: 13 additions & 0 deletions exercises/practice/isbn-verifier/isbn_verifier.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module isbn_verifier
implicit none

contains

function isValid(isbn) result(valid)
character(*), intent(in) :: isbn
logical :: valid

valid = isbn == "123" ! Replace this line with your implementation
end function isValid

end module isbn_verifier
86 changes: 86 additions & 0 deletions exercises/practice/isbn-verifier/isbn_verifier_test.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
! The tests were created from https://github.com/exercism/problem-specifications/blob/main/exercises/isbn-verifier/canonical-data.json

program isbn_verifier_test_main
use TesterMain
use isbn_verifier
implicit none

! Test 1: valid isbn
call assert_equal(.true., isValid("3-598-21508-8"), &
"valid isbn")

! Test 2: invalid isbn check digit
call assert_equal(.false., isValid("3-598-21508-9"), &
"invalid isbn check digit")

! Test 3: valid isbn with a check digit of 10
call assert_equal(.true., isValid("3-598-21507-X"), &
"valid isbn with a check digit of 10")

! Test 4: check digit is a character other than X
call assert_equal(.false., isValid("3-598-21507-A"), &
"check digit is a character other than X")

! Test 5: invalid check digit in isbn is not treated as zero
call assert_equal(.false., isValid("4-598-21507-B"), &
"invalid check digit in isbn is not treated as zero")

! Test 6: invalid character in isbn is not treated as zero
call assert_equal(.false., isValid("3-598-P1581-X"), &
"invalid character in isbn is not treated as zero")

! Test 7: X is only valid as a check digit
call assert_equal(.false., isValid("3-598-2X507-9"), &
"X is only valid as a check digit")

! Test 8: valid isbn without separating dashes
call assert_equal(.true., isValid("3598215088"), &
"valid isbn without separating dashes")

! Test 9: isbn without separating dashes and X as check digit
call assert_equal(.true., isValid("359821507X"), &
"isbn without separating dashes and X as check digit")

! Test 10: isbn without check digit and dashes
call assert_equal(.false., isValid("359821507"), &
"isbn without check digit and dashes")

! Test 11: too long isbn and no dashes
call assert_equal(.false., isValid("3598215078X"), &
"too long isbn and no dashes")

! Test 12: too short isbn
call assert_equal(.false., isValid("00"), &
"too short isbn")

! Test 13: isbn without check digit
call assert_equal(.false., isValid("3-598-21507"), &
"isbn without check digit")

! Test 14: check digit of X should not be used for 0
call assert_equal(.false., isValid("3-598-21515-X"), &
"check digit of X should not be used for 0")

! Test 15: empty isbn
call assert_equal(.false., isValid(""), &
"empty isbn")

! Test 16: input is 9 characters
call assert_equal(.false., isValid("134456729"), &
"input is 9 characters")

! Test 17: invalid characters are not ignored after checking length
call assert_equal(.false., isValid("3132P34035"), &
"invalid characters are not ignored after checking length")

! Test 18: invalid characters are not ignored before checking length
call assert_equal(.false., isValid("3598P215088"), &
"invalid characters are not ignored before checking length")

! Test 19: input is too long but contains a valid isbn
call assert_equal(.false., isValid("98245726788"), &
"input is too long but contains a valid isbn")

call test_report()

end program

0 comments on commit eed4aa8

Please sign in to comment.