-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31875ae
commit 88d9e87
Showing
9 changed files
with
363 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Instructions | ||
|
||
Your task is to implement a binary search algorithm. | ||
|
||
A binary search algorithm finds an item in a list by repeatedly splitting it in half, only keeping the half which contains the item we're looking for. | ||
It allows us to quickly narrow down the possible locations of our item until we find it, or until we've eliminated all possible locations. | ||
|
||
~~~~exercism/caution | ||
Binary search only works when a list has been sorted. | ||
~~~~ | ||
|
||
The algorithm looks like this: | ||
|
||
- Find the middle element of a _sorted_ list and compare it with the item we're looking for. | ||
- If the middle element is our item, then we're done! | ||
- If the middle element is greater than our item, we can eliminate that element and all the elements **after** it. | ||
- If the middle element is less than our item, we can eliminate that element and all the elements **before** it. | ||
- If every element of the list has been eliminated then the item is not in the list. | ||
- Otherwise, repeat the process on the part of the list that has not been eliminated. | ||
|
||
Here's an example: | ||
|
||
Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`. | ||
|
||
- We start by comparing 23 with the middle element, 16. | ||
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`. | ||
- We then compare 23 with the new middle element, 28. | ||
- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`. | ||
- We've found our item. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Introduction | ||
|
||
You have stumbled upon a group of mathematicians who are also singer-songwriters. | ||
They have written a song for each of their favorite numbers, and, as you can imagine, they have a lot of favorite numbers (like [0][zero] or [73][seventy-three] or [6174][kaprekars-constant]). | ||
|
||
You are curious to hear the song for your favorite number, but with so many songs to wade through, finding the right song could take a while. | ||
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about. | ||
|
||
You realize that you can use a binary search algorithm to quickly find a song given the title. | ||
|
||
[zero]: https://en.wikipedia.org/wiki/0 | ||
[seventy-three]: https://en.wikipedia.org/wiki/73_(number) | ||
[kaprekars-constant]: https://en.wikipedia.org/wiki/6174_(number) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"erikschierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"binary-search.8th" | ||
], | ||
"test": [ | ||
"test.8th" | ||
], | ||
"example": [ | ||
".meta/example.8th" | ||
] | ||
}, | ||
"blurb": "Implement a binary search algorithm.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Binary_search_algorithm" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
: middle \ n n - n | ||
n:+ 2 n:/mod nip | ||
; | ||
|
||
: find-recur \ a n n n -- n | ||
>r | ||
2dup n:> if 3drop null ;then | ||
3dup middle a:_@ | ||
dup r@ n:= if drop middle nip ;then | ||
dup r@ n:> if drop 2dup middle n:1- nip r> recurse ;then | ||
drop 2dup middle n:1+ -rot nip r> recurse | ||
; | ||
|
||
: find \ a n -- n | ||
>r a:len n:1- 0 swap r> find-recur | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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. | ||
|
||
[b55c24a9-a98d-4379-a08c-2adcf8ebeee8] | ||
description = "finds a value in an array with one element" | ||
|
||
[73469346-b0a0-4011-89bf-989e443d503d] | ||
description = "finds a value in the middle of an array" | ||
|
||
[327bc482-ab85-424e-a724-fb4658e66ddb] | ||
description = "finds a value at the beginning of an array" | ||
|
||
[f9f94b16-fe5e-472c-85ea-c513804c7d59] | ||
description = "finds a value at the end of an array" | ||
|
||
[f0068905-26e3-4342-856d-ad153cadb338] | ||
description = "finds a value in an array of odd length" | ||
|
||
[fc316b12-c8b3-4f5e-9e89-532b3389de8c] | ||
description = "finds a value in an array of even length" | ||
|
||
[da7db20a-354f-49f7-a6a1-650a54998aa6] | ||
description = "identifies that a value is not included in the array" | ||
|
||
[95d869ff-3daf-4c79-b622-6e805c675f97] | ||
description = "a value smaller than the array's smallest value is not found" | ||
|
||
[8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba] | ||
description = "a value larger than the array's largest value is not found" | ||
|
||
[f439a0fa-cf42-4262-8ad1-64bf41ce566a] | ||
description = "nothing is found in an empty array" | ||
|
||
[2c353967-b56d-40b8-acff-ce43115eed64] | ||
description = "nothing is found when the left and right bounds cross" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
: find \ a n -- n | ||
|
||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
needs console/loaded | ||
|
||
\ ----------------------------------------------------------------- | ||
|
||
ns: test | ||
|
||
-1 var, test-count | ||
var tests-passed | ||
var tests-failed | ||
var tests-skipped | ||
true var, run-test | ||
|
||
\ Some utility words | ||
|
||
|
||
: test-passed \ s x x -- \\ test name, expected value, actual value | ||
2drop | ||
1 tests-passed n:+! | ||
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr | ||
; | ||
|
||
: test-skipped \ s -- | ||
1 tests-skipped n:+! | ||
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr | ||
; | ||
|
||
: test-failed \ s x x -- \\ test name, expected value, actual value | ||
1 tests-failed n:+! | ||
rot | ||
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr | ||
" Actual: «" . . "»" . cr | ||
" Expected: «" . . "»" . cr cr | ||
; | ||
|
||
: isword? \ x -- x f | ||
dup >kind ns:w n:= | ||
; | ||
|
||
: run-test? \ -- T | ||
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then | ||
; | ||
|
||
\ Num passed + num skipped + num failed should == num tests | ||
: all-tests-run? \ -- T | ||
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+ | ||
test-count @ n:= | ||
; | ||
|
||
\ returns true if x is a date, false otherwise | ||
: date? \ x -- x T | ||
dup >kind ns:d n:= | ||
; | ||
|
||
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html | ||
: eq? \ x x -- T | ||
\ are the items the same kind? | ||
2dup >kind swap >kind n:= | ||
!if 2drop false ;then | ||
|
||
\ same kind: try different comparators | ||
number? if n:= ;then | ||
string? if s:= ;then | ||
array? if ' eq? a:= 2nip ;then | ||
map? if ' eq? m:= 2nip ;then | ||
date? if d:= ;then | ||
|
||
\ otherwise fall back to 'lazy evaluation' | ||
l: = | ||
; | ||
|
||
: eps_eq? \ n x x -- T | ||
\ are the items the same kind? | ||
2dup >kind swap >kind n:= | ||
!if 2drop false ;then | ||
number? !if 2drop false ;then | ||
rot n:~= | ||
; | ||
|
||
: check-depth \ ... n -- ... | ||
dup>r | ||
n:1+ depth n:= | ||
!if | ||
con:red con:onBlack | ||
"PANIC: expected stack depth to be " . r> . cr | ||
"Stack is:" . cr | ||
.s cr | ||
255 die | ||
then | ||
rdrop | ||
; | ||
|
||
\ ----------------------------------------------------------------- | ||
|
||
\ status report at end of run | ||
( all-tests-run? | ||
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then | ||
) onexit | ||
|
||
\ Print a summary of the tests run | ||
( con:white con:onBlack | ||
test-count @ . space "tests planned - " . | ||
tests-passed @ . space "passed - " . | ||
tests-skipped @ . space "skipped - " . | ||
tests-failed @ . space "failed" . cr | ||
) onexit | ||
|
||
\ ----------------------------------------------------------------- | ||
\ The public-facing words | ||
\ ----------------------------------------------------------------- | ||
|
||
: equal? \ s x w -- | s w x -- | ||
run-test? !if 2drop test-skipped ;; then | ||
isword? !if swap then | ||
w:exec | ||
3 check-depth | ||
2dup \ so test-failed can show actual and expected | ||
eq? if test-passed else test-failed then | ||
; | ||
|
||
: approx_equal? \ s x w n -- | s w x n -- | ||
run-test? !if 3drop test-skipped ;; then | ||
-rot isword? !if swap then | ||
w:exec | ||
4 check-depth | ||
3dup \ so test-failed can show actual and expected | ||
eps_eq? | ||
if rot drop test-passed else rot drop test-failed then | ||
; | ||
|
||
: true? \ s w -- | ||
run-test? !if drop test-skipped ;; then | ||
w:exec | ||
2 check-depth | ||
true swap dup \ so test-failed can show actual and expected | ||
if test-passed else test-failed then | ||
; | ||
|
||
: false? \ s w -- | ||
run-test? !if drop test-skipped ;; then | ||
w:exec | ||
2 check-depth | ||
false swap dup \ so test-failed can show actual and expected | ||
!if test-passed else test-failed then | ||
; | ||
|
||
: null? \ s w -- | ||
run-test? !if drop test-skipped ;; then | ||
w:exec | ||
2 check-depth | ||
null swap dup \ so test-failed can show actual and expected | ||
G:null? nip if test-passed else test-failed then | ||
; | ||
|
||
: SKIP-REST-OF-TESTS false run-test ! ; | ||
|
||
: tests \ n -- | ||
test-count ! | ||
; | ||
|
||
\ Set the exit status: | ||
\ 0 = all OK | ||
\ 1 = not all tests were run (some error occurred) | ||
\ 2 = some tests failed | ||
: end-of-tests \ -- | ||
all-tests-run? | ||
if | ||
tests-failed @ 0 n:= if 0 else 2 then | ||
else | ||
1 | ||
then | ||
die | ||
; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"binary-search.8th" f:include | ||
needs exercism/test | ||
with: test | ||
11 tests | ||
|
||
"finds a value in an array with one element" | ||
( [6] 6 find ) | ||
0 | ||
equal? | ||
|
||
SKIP-REST-OF-TESTS | ||
|
||
"finds a value in the middle of an array" | ||
( [1, 3, 4, 6, 8, 9, 11] 6 find ) | ||
3 | ||
equal? | ||
|
||
"finds a value at the beginning of an array" | ||
( [1, 3, 4, 6, 8, 9, 11] 1 find ) | ||
0 | ||
equal? | ||
|
||
"finds a value at the end of an array" | ||
( [1, 3, 4, 6, 8, 9, 11] 11 find ) | ||
6 | ||
equal? | ||
|
||
"finds a value in an array of odd length" | ||
( [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634] 144 find ) | ||
9 | ||
equal? | ||
|
||
"finds a value in an array of even length" | ||
( [1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377] 21 find ) | ||
5 | ||
equal? | ||
|
||
"identifies that a value is not included in the array" | ||
( [1, 3, 4, 6, 8, 9, 11] 7 find ) | ||
null? | ||
|
||
"a value smaller than the array's smallest value is not found" | ||
( [1, 3, 4, 6, 8, 9, 11] 0 find ) | ||
null? | ||
|
||
"a value larger than the array's largest value is not found" | ||
( [1, 3, 4, 6, 8, 9, 11] 13 find ) | ||
null? | ||
|
||
"nothing is found in an empty array" | ||
( [] 1 find ) | ||
null? | ||
|
||
"nothing is found when the left and right bounds cross" | ||
( [1, 2] 0 find ) | ||
null? | ||
|
||
end-of-tests | ||
;with |