Skip to content

Commit 872d3d9

Browse files
authored
Add Binary Search (#550)
* Add binary-search exercise * add editor file * add author
1 parent f88f5de commit 872d3d9

File tree

11 files changed

+224
-0
lines changed

11 files changed

+224
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,14 @@
638638
"prerequisites": [],
639639
"difficulty": 3
640640
},
641+
{
642+
"slug": "binary-search",
643+
"name": "Binary Search",
644+
"uuid": "ef5a20e3-5c5a-4c93-82c3-e82998ca3f39",
645+
"practices": [],
646+
"prerequisites": [],
647+
"difficulty": 3
648+
},
641649
{
642650
"slug": "protein-translation",
643651
"name": "Protein Translation",
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"lib/binary_search.dart"
8+
],
9+
"test": [
10+
"test/binary_search_test.dart"
11+
],
12+
"example": [
13+
".meta/lib/example.dart"
14+
],
15+
"editor": [
16+
"lib/value_not_found_exception.dart"
17+
]
18+
},
19+
"blurb": "Implement a binary search algorithm.",
20+
"source": "Wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Binary_search_algorithm"
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import 'package:binary_search/value_not_found_exception.dart';
2+
3+
class BinarySearch {
4+
List<int> items;
5+
6+
BinarySearch(this.items);
7+
8+
int find(int item) {
9+
int i = 0, j = items.length - 1;
10+
while (i <= j) {
11+
int mid = (i + j) ~/ 2;
12+
if (item == items[mid]) {
13+
return mid;
14+
} else if (item < items[mid]) {
15+
j = mid - 1;
16+
} else {
17+
i = mid + 1;
18+
}
19+
}
20+
throw ValueNotFoundException("foo bar");
21+
}
22+
}
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
analyzer:
2+
strong-mode:
3+
implicit-casts: false
4+
implicit-dynamic: false
5+
errors:
6+
unused_element: error
7+
unused_import: error
8+
unused_local_variable: error
9+
dead_code: error
10+
11+
linter:
12+
rules:
13+
# Error Rules
14+
- avoid_relative_lib_imports
15+
- avoid_types_as_parameter_names
16+
- literal_only_boolean_expressions
17+
- no_adjacent_strings_in_list
18+
- valid_regexps
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import 'package:binary_search/value_not_found_exception.dart';
2+
3+
class BinarySearch {
4+
// Your code here.
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class ValueNotFoundException implements Exception {
2+
String message;
3+
ValueNotFoundException(this.message);
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'binary_search'
2+
environment:
3+
sdk: '>=3.2.0 <4.0.0'
4+
dev_dependencies:
5+
test: '<2.0.0'

0 commit comments

Comments
 (0)