Skip to content

Commit 8ddc217

Browse files
authored
Add binary-search (#193)
1 parent 3d20f7c commit 8ddc217

File tree

10 files changed

+246
-0
lines changed

10 files changed

+246
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@
8282
"prerequisites": [],
8383
"difficulty": 2
8484
},
85+
{
86+
"slug": "binary-search",
87+
"name": "Binary Search",
88+
"uuid": "f9d2463b-1519-4b98-895f-fba505d13add",
89+
"practices": [],
90+
"prerequisites": [],
91+
"difficulty": 2
92+
},
8593
{
8694
"slug": "collatz-conjecture",
8795
"name": "Collatz Conjecture",
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Instructions
2+
3+
Your task is to implement a binary search algorithm.
4+
5+
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.
6+
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.
7+
8+
~~~~exercism/caution
9+
Binary search only works when a list has been sorted.
10+
~~~~
11+
12+
The algorithm looks like this:
13+
14+
- Find the middle element of a _sorted_ list and compare it with the item we're looking for.
15+
- If the middle element is our item, then we're done!
16+
- If the middle element is greater than our item, we can eliminate that element and all the elements **after** it.
17+
- If the middle element is less than our item, we can eliminate that element and all the elements **before** it.
18+
- If every element of the list has been eliminated then the item is not in the list.
19+
- Otherwise, repeat the process on the part of the list that has not been eliminated.
20+
21+
Here's an example:
22+
23+
Let's say we're looking for the number 23 in the following sorted list: `[4, 8, 12, 16, 23, 28, 32]`.
24+
25+
- We start by comparing 23 with the middle element, 16.
26+
- Since 23 is greater than 16, we can eliminate the left half of the list, leaving us with `[23, 28, 32]`.
27+
- We then compare 23 with the new middle element, 28.
28+
- Since 23 is less than 28, we can eliminate the right half of the list: `[23]`.
29+
- We've found our item.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Introduction
2+
3+
You have stumbled upon a group of mathematicians who are also singer-songwriters.
4+
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]).
5+
6+
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.
7+
Fortunately, they have organized their songs in a playlist sorted by the title — which is simply the number that the song is about.
8+
9+
You realize that you can use a binary search algorithm to quickly find a song given the title.
10+
11+
[zero]: https://en.wikipedia.org/wiki/0
12+
[seventy-three]: https://en.wikipedia.org/wiki/73_(number)
13+
[kaprekars-constant]: https://en.wikipedia.org/wiki/6174_(number)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"binary-search.wren"
8+
],
9+
"test": [
10+
"binary-search.spec.wren"
11+
],
12+
"example": [
13+
".meta/proof.ci.wren"
14+
]
15+
},
16+
"blurb": "Implement a binary search algorithm.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Binary_search_algorithm"
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class BinarySearch {
2+
static find(values, target) {
3+
var start = 0
4+
var stop = values.count - 1
5+
6+
while (start <= stop) {
7+
var index = ((start + stop) / 2).truncate
8+
var value = values[index]
9+
10+
if (value == target) {
11+
return index
12+
}
13+
14+
if (value < target) {
15+
start = index + 1
16+
} else {
17+
stop = index - 1
18+
}
19+
}
20+
21+
Fiber.abort("value not in list")
22+
}
23+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[b55c24a9-a98d-4379-a08c-2adcf8ebeee8]
13+
description = "finds a value in an array with one element"
14+
15+
[73469346-b0a0-4011-89bf-989e443d503d]
16+
description = "finds a value in the middle of an array"
17+
18+
[327bc482-ab85-424e-a724-fb4658e66ddb]
19+
description = "finds a value at the beginning of an array"
20+
21+
[f9f94b16-fe5e-472c-85ea-c513804c7d59]
22+
description = "finds a value at the end of an array"
23+
24+
[f0068905-26e3-4342-856d-ad153cadb338]
25+
description = "finds a value in an array of odd length"
26+
27+
[fc316b12-c8b3-4f5e-9e89-532b3389de8c]
28+
description = "finds a value in an array of even length"
29+
30+
[da7db20a-354f-49f7-a6a1-650a54998aa6]
31+
description = "identifies that a value is not included in the array"
32+
33+
[95d869ff-3daf-4c79-b622-6e805c675f97]
34+
description = "a value smaller than the array's smallest value is not found"
35+
36+
[8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba]
37+
description = "a value larger than the array's largest value is not found"
38+
39+
[f439a0fa-cf42-4262-8ad1-64bf41ce566a]
40+
description = "nothing is found in an empty array"
41+
42+
[2c353967-b56d-40b8-acff-ce43115eed64]
43+
description = "nothing is found when the left and right bounds cross"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import "./binary-search" for BinarySearch
2+
import "wren-testie/testie" for Testie, Expect
3+
4+
Testie.test("BinarySearch") { |do, skip|
5+
do.test("finds a value in a list with one element") {
6+
var actual = BinarySearch.find([6], 6)
7+
var expected = 0
8+
Expect.value(actual).toEqual(expected)
9+
}
10+
11+
do.skip("finds a value in the middle of a list") {
12+
var actual = BinarySearch.find([1, 3, 4, 6, 8, 9, 11], 6)
13+
var expected = 3
14+
Expect.value(actual).toEqual(expected)
15+
}
16+
17+
do.skip("finds a value at the beginning of a list") {
18+
var actual = BinarySearch.find([1, 3, 4, 6, 8, 9, 11], 1)
19+
var expected = 0
20+
Expect.value(actual).toEqual(expected)
21+
}
22+
23+
do.skip("finds a value at the end of a list") {
24+
var actual = BinarySearch.find([1, 3, 4, 6, 8, 9, 11], 11)
25+
var expected = 6
26+
Expect.value(actual).toEqual(expected)
27+
}
28+
29+
do.skip("finds a value in a list of odd length") {
30+
var actual = BinarySearch.find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144)
31+
var expected = 9
32+
Expect.value(actual).toEqual(expected)
33+
}
34+
35+
do.skip("finds a value in a list of even length") {
36+
var actual = BinarySearch.find([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21)
37+
var expected = 5
38+
Expect.value(actual).toEqual(expected)
39+
}
40+
41+
do.skip("identifies that a value is not included in the list") {
42+
Expect.that {
43+
BinarySearch.find([1, 3, 4, 6, 8, 9, 11], 7)
44+
}.abortsWith("value not in list")
45+
}
46+
47+
do.skip("a value smaller than the list's smallest value is not found") {
48+
Expect.that {
49+
BinarySearch.find([1, 3, 4, 6, 8, 9, 11], 0)
50+
}.abortsWith("value not in list")
51+
}
52+
53+
do.skip("a value larger than the list's largest value is not found") {
54+
Expect.that {
55+
BinarySearch.find([1, 3, 4, 6, 8, 9, 11], 13)
56+
}.abortsWith("value not in list")
57+
}
58+
59+
do.skip("nothing is found in an empty list") {
60+
Expect.that {
61+
BinarySearch.find([], 1)
62+
}.abortsWith("value not in list")
63+
}
64+
65+
do.skip("nothing is found when the left and right bounds cross") {
66+
Expect.that {
67+
BinarySearch.find([1, 2], 0)
68+
}.abortsWith("value not in list")
69+
}
70+
}
71+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class BinarySearch {
2+
static find(values, target) {
3+
Fiber.abort("Remove this statement and implement this function")
4+
}
5+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import "wren-package" for WrenPackage, Dependency
2+
import "os" for Process
3+
4+
class Package is WrenPackage {
5+
construct new() {}
6+
name { "exercism/binary-search" }
7+
dependencies {
8+
return [
9+
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
10+
]
11+
}
12+
}
13+
14+
Package.new().default()

0 commit comments

Comments
 (0)