Skip to content

isbn-verifier: Sync tests #706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion exercises/practice/isbn-verifier/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"AndreaCrotti",
"haus",
"jgomo3",
"sjwarner-bp"
"sjwarner-bp",
"tasxatzial"
],
"files": {
"solution": [
Expand Down
33 changes: 20 additions & 13 deletions exercises/practice/isbn-verifier/.meta/example.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
(ns isbn-verifier)

(defn is-in? [xs x] (some #(= x %) xs))
(defn- get-value
[ch]
(if (= ch \X)
10
(Character/digit ^char ch 10)))

(defn isbn-chars [isbn]
(filter #(is-in? [\0 \1 \2 \3 \4 \5 \6 \7 \8 \9 \X] %) isbn))
(defn- valid-isbn-value?
[s]
(->> (remove #{\-} s)
(map get-value)
(map * (range 10 0 -1))
(reduce +)
(#(mod % 11))
zero?))

(defn isbn? [isbn]
(let [chars (isbn-chars isbn)
nums (map #(if (= \X %) 10 (Character/digit % 10)) chars)]
(and (-> chars butlast (is-in? \X) not)
(= 10 (count chars))
(as-> nums x
(map #(* %1 %2) (range 10 0 -1) x)
(reduce + x)
(mod x 11)
(zero? x)))))
(defn valid-isbn-pattern?
[s]
(boolean (re-matches #"\d{9}(X|\d)|\d-\d{3}-\d{5}-(X|\d)" s)))

(defn isbn?
[s]
(and (valid-isbn-pattern? s) (valid-isbn-value? s)))
27 changes: 20 additions & 7 deletions exercises/practice/isbn-verifier/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# 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 number"
description = "valid isbn"

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

[4164bfee-fb0a-4a1c-9f70-64c6a1903dcd]
description = "valid isbn number with a check digit of 10"
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"
description = "invalid character in isbn is not treated as zero"

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

[ed6e8d1b-382c-4081-8326-8b772c581fec]
description = "invalid characters are not ignored"
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"
8 changes: 5 additions & 3 deletions exercises/practice/isbn-verifier/src/isbn_verifier.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
(ns isbn-verifier)

(defn isbn? [isbn] ;; <- arglist goes here
;; your code goes here
)
(defn isbn?
"Returns true if the given isbn is valid; otherwise, returns false"
[isbn]
;; function body
)
94 changes: 64 additions & 30 deletions exercises/practice/isbn-verifier/test/isbn_verifier_test.clj
Original file line number Diff line number Diff line change
@@ -1,45 +1,79 @@
(ns isbn-verifier-test
(:require [clojure.test :refer [deftest is]]
[isbn-verifier :refer [isbn?]]))
(:require [clojure.test :refer [deftest testing is]]
isbn-verifier))

(deftest valid-isbn-number
(is (= true (isbn? "3-598-21508-8"))))
(deftest isbn?_test_1
(testing "valid isbn"
(is (true? (isbn-verifier/isbn? "3-598-21508-8")))))

(deftest invalid-isbn-check-digit
(is (= false (isbn? "3-598-21508-9"))))
(deftest isbn?_test_2
(testing "invalid isbn check digit"
(is (false? (isbn-verifier/isbn? "3-598-21508-9")))))

(deftest valid-isbn-number-with-a-check-digit-of-10
(is (= true (isbn? "3-598-21507-X"))))
(deftest isbn?_test_3
(testing "valid isbn with a check digit of 10"
(is (true? (isbn-verifier/isbn? "3-598-21507-X")))))

(deftest check-digit-is-a-character-other-than-X
(is (= false (isbn? "3-598-21507-A"))))
(deftest isbn?_test_4
(testing "check digit is a character other than X"
(is (false? (isbn-verifier/isbn? "3-598-21507-A")))))

(deftest invalid-character-in-isbn
(is (= false (isbn? "3-598-2K507-0"))))
(deftest isbn?_test_5
(testing "invalid check digit in isbn is not treated as zero"
(is (false? (isbn-verifier/isbn? "4-598-21507-B")))))

(deftest X-is-only-valid-as-a-check-digit
(is (= false (isbn? "3-598-2X507-9"))))
(deftest isbn?_test_6
(testing "invalid character in isbn is not treated as zero"
(is (false? (isbn-verifier/isbn? "3-598-P1581-X")))))

(deftest valid-isbn-without-separating-dashes
(is (= true (isbn? "3598215088"))))
(deftest isbn?_test_7
(testing "X is only valid as a check digit"
(is (false? (isbn-verifier/isbn? "3-598-2X507-9")))))

(deftest isbn-without-separating-dashes-and-X-as-check-digit
(is (= true (isbn? "359821507X"))))
(deftest isbn?_test_8
(testing "valid isbn without separating dashes"
(is (true? (isbn-verifier/isbn? "3598215088")))))

(deftest isbn-without-check-digit-and-dashes
(is (= false (isbn? "359821507"))))
(deftest isbn?_test_9
(testing "isbn without separating dashes and X as check digit"
(is (true? (isbn-verifier/isbn? "359821507X")))))

(deftest too-long-isbn-and-no-dashes
(is (= false (isbn? "3598215078X"))))
(deftest isbn?_test_10
(testing "isbn without check digit and dashes"
(is (false? (isbn-verifier/isbn? "359821507")))))

(deftest too-short-isbn
(is (= false (isbn? "00"))))
(deftest isbn?_test_11
(testing "too long isbn and no dashes"
(is (false? (isbn-verifier/isbn? "3598215078X")))))

(deftest isbn-without-check-digit
(is (= false (isbn? "3-598-21507"))))
(deftest isbn?_test_12
(testing "too short isbn"
(is (false? (isbn-verifier/isbn? "00")))))

(deftest too-long-isbn
(is (= false (isbn? "3-598-21507-XX"))))
(deftest isbn?_test_13
(testing "isbn without check digit"
(is (false? (isbn-verifier/isbn? "3-598-21507")))))

(deftest check-digit-of-X-should-not-be-used-for-0
(is (= false (isbn? "3-598-21515-X"))))
(deftest isbn?_test_14
(testing "check digit of X should not be used for 0"
(is (false? (isbn-verifier/isbn? "3-598-21515-X")))))

(deftest isbn?_test_15
(testing "empty isbn"
(is (false? (isbn-verifier/isbn? "")))))

(deftest isbn?_test_16
(testing "input is 9 characters"
(is (false? (isbn-verifier/isbn? "134456729")))))

(deftest isbn?_test_17
(testing "invalid characters are not ignored after checking length"
(is (false? (isbn-verifier/isbn? "3132P34035")))))

(deftest isbn?_test_18
(testing "invalid characters are not ignored before checking length"
(is (false? (isbn-verifier/isbn? "3598P215088")))))

(deftest isbn?_test_19
(testing "input is too long but contains a valid isbn"
(is (false? (isbn-verifier/isbn? "98245726788")))))