From f269694505dd7eb77bd310e591f840b67c721a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 9 Apr 2024 05:58:01 -0700 Subject: [PATCH] Update binary search (#369) * array to vector in test suite * Newline between test cases * Simplify tests for #f * Add instructions append --- .../.docs/instructions.append.md | 6 +++ .../binary-search/binary-search-test.rkt | 47 ++++++++++--------- 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 exercises/practice/binary-search/.docs/instructions.append.md diff --git a/exercises/practice/binary-search/.docs/instructions.append.md b/exercises/practice/binary-search/.docs/instructions.append.md new file mode 100644 index 00000000..3ecc5a16 --- /dev/null +++ b/exercises/practice/binary-search/.docs/instructions.append.md @@ -0,0 +1,6 @@ +# Instructions append + +In Racket, this exercise uses sorted [vectors] rather than lists because vectors support constant-time access of its elements. Like several other Lisp tracks, when an item isn't present in the vector, the expectation is that the [Boolean literal `#f`][booleans] will be returned rather than raising an exception. + +[vectors]: https://docs.racket-lang.org/guide/vectors.html +[booleans]: https://docs.racket-lang.org/reference/booleans.html diff --git a/exercises/practice/binary-search/binary-search-test.rkt b/exercises/practice/binary-search/binary-search-test.rkt index 0fef752b..d4f2120f 100644 --- a/exercises/practice/binary-search/binary-search-test.rkt +++ b/exercises/practice/binary-search/binary-search-test.rkt @@ -9,38 +9,43 @@ (test-suite "binary-search tests" - (test-eqv? "finds a value in an array with one element" + (test-eqv? "finds a value in a vector with one element" (binary-search #(6) 6) 0) - (test-eqv? "finds a value in the middle of an array" + + (test-eqv? "finds a value in the middle of a vector" (binary-search #(1 3 4 6 8 9 11) 6) 3) - (test-eqv? "finds a value at the beginning of an array" + + (test-eqv? "finds a value at the beginning of a vector" (binary-search #(1 3 4 6 8 9 11) 1) 0) - (test-eqv? "finds a value at the end of an array" + + (test-eqv? "finds a value at the end of a vector" (binary-search #(1 3 4 6 8 9 11) 11) 6) - (test-eqv? "finds a value in an array of odd length" + + (test-eqv? "finds a value in a vector of odd length" (binary-search #(1 3 5 8 13 21 34 55 89 144 233 377 634) 144) 9) - (test-eqv? "finds a value in an array of even length" + + (test-eqv? "finds a value in a vector of even length" (binary-search #(1 3 5 8 13 21 34 55 89 144 233 377) 21) 5) - (test-eqv? "identifies that a value is not included in the array" - (binary-search #(1 3 4 6 8 9 11) 7) - #f) - (test-eqv? "a value smaller than the array's smallest value is not found" - (binary-search #(1 3 4 6 8 9 11) 0) - #f) - (test-eqv? "a value larger than the array's smallest value is not found" - (binary-search #(1 3 4 6 8 9 11) 13) - #f) - (test-eqv? "nothing is found in an empty array" - (binary-search #() 1) - #f) - (test-eqv? "nothing is found when the left and right bounds cross" - (binary-search #(1 2) 0) - #f))) + + (test-false "identifies that a value is not included in the vector" + (binary-search #(1 3 4 6 8 9 11) 7)) + + (test-false "a value smaller than the vector's smallest value is not found" + (binary-search #(1 3 4 6 8 9 11) 0)) + + (test-false "a value larger than the vector's largest value is not found" + (binary-search #(1 3 4 6 8 9 11) 13)) + + (test-false "nothing is found in an empty vector" + (binary-search #() 1)) + + (test-false "nothing is found when the left and right bounds cross" + (binary-search #(1 2) 0)))) (run-tests suite))