Skip to content
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

Update binary search #369

Merged
merged 4 commits into from
Apr 9, 2024
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
6 changes: 6 additions & 0 deletions exercises/practice/binary-search/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -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
47 changes: 26 additions & 21 deletions exercises/practice/binary-search/binary-search-test.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -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))