-
-
Notifications
You must be signed in to change notification settings - Fork 101
Add Binary Search #550
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
Merged
Merged
Add Binary Search #550
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,20 @@ | ||
| { | ||
| "authors": [], | ||
| "files": { | ||
| "solution": [ | ||
| "lib/binary_search.dart" | ||
| ], | ||
| "test": [ | ||
| "test/binary_search_test.dart" | ||
| ], | ||
| "example": [ | ||
| ".meta/lib/example.dart" | ||
| ], | ||
| "editor": [ | ||
| "lib/value_not_found_exception.dart" | ||
| ] | ||
| }, | ||
| "blurb": "Implement a binary search algorithm.", | ||
| "source": "Wikipedia", | ||
| "source_url": "https://en.wikipedia.org/wiki/Binary_search_algorithm" | ||
| } | ||
This file contains hidden or 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,22 @@ | ||
| import 'package:binary_search/value_not_found_exception.dart'; | ||
|
|
||
| class BinarySearch { | ||
| List<int> items; | ||
|
|
||
| BinarySearch(this.items); | ||
|
|
||
| int find(int item) { | ||
| int i = 0, j = items.length - 1; | ||
| while (i <= j) { | ||
| int mid = (i + j) ~/ 2; | ||
| if (item == items[mid]) { | ||
| return mid; | ||
| } else if (item < items[mid]) { | ||
| j = mid - 1; | ||
| } else { | ||
| i = mid + 1; | ||
| } | ||
| } | ||
| throw ValueNotFoundException("foo bar"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,18 @@ | ||
| analyzer: | ||
| strong-mode: | ||
| implicit-casts: false | ||
| implicit-dynamic: false | ||
| errors: | ||
| unused_element: error | ||
| unused_import: error | ||
| unused_local_variable: error | ||
| dead_code: error | ||
|
|
||
| linter: | ||
| rules: | ||
| # Error Rules | ||
| - avoid_relative_lib_imports | ||
| - avoid_types_as_parameter_names | ||
| - literal_only_boolean_expressions | ||
| - no_adjacent_strings_in_list | ||
| - valid_regexps |
This file contains hidden or 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,5 @@ | ||
| import 'package:binary_search/value_not_found_exception.dart'; | ||
|
|
||
| class BinarySearch { | ||
| // Your code here. | ||
| } |
4 changes: 4 additions & 0 deletions
4
exercises/practice/binary-search/lib/value_not_found_exception.dart
This file contains hidden or 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,4 @@ | ||
| class ValueNotFoundException implements Exception { | ||
| String message; | ||
| ValueNotFoundException(this.message); | ||
| } |
This file contains hidden or 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,5 @@ | ||
| name: 'binary_search' | ||
| environment: | ||
| sdk: '>=3.2.0 <4.0.0' | ||
| dev_dependencies: | ||
| test: '<2.0.0' |
55 changes: 55 additions & 0 deletions
55
exercises/practice/binary-search/test/binary_search_test.dart
This file contains hidden or 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,55 @@ | ||
| import 'package:binary_search/binary_search.dart'; | ||
| import 'package:binary_search/value_not_found_exception.dart'; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| void main() { | ||
| test("finds a value in an array with one element", () { | ||
| final index = BinarySearch([6]).find(6); | ||
| expect(index, equals(0)); | ||
| }, skip: false); | ||
|
|
||
| test("finds a value in the middle of an array", () { | ||
| final index = BinarySearch([1, 3, 4, 6, 8, 9, 11]).find(6); | ||
| expect(index, equals(3)); | ||
| }, skip: true); | ||
|
|
||
| test("finds a value at the beginning of an array", () { | ||
| final index = BinarySearch([1, 3, 4, 6, 8, 9, 11]).find(1); | ||
| expect(index, equals(0)); | ||
| }, skip: true); | ||
|
|
||
| test("finds a value at the end of an array", () { | ||
| final index = BinarySearch([1, 3, 4, 6, 8, 9, 11]).find(11); | ||
| expect(index, equals(6)); | ||
| }, skip: true); | ||
|
|
||
| test("finds a value in an array of odd length", () { | ||
| final index = BinarySearch([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634]).find(144); | ||
| expect(index, equals(9)); | ||
| }, skip: true); | ||
|
|
||
| test("finds a value in an array of even length", () { | ||
| final index = BinarySearch([1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]).find(21); | ||
| expect(index, equals(5)); | ||
| }, skip: true); | ||
|
|
||
| test("identifies that a value is not included in the array", () { | ||
| expect(() => BinarySearch([1, 3, 4, 6, 8, 9, 11]).find(7), throwsA(isA<ValueNotFoundException>())); | ||
| }, skip: true); | ||
|
|
||
| test("a value smaller than the array's smallest value is not found", () { | ||
| expect(() => BinarySearch([1, 3, 4, 6, 8, 9, 11]).find(0), throwsA(isA<ValueNotFoundException>())); | ||
| }, skip: true); | ||
|
|
||
| test("a value larger than the array's largest value is not found", () { | ||
| expect(() => BinarySearch([1, 3, 4, 6, 8, 9, 11]).find(13), throwsA(isA<ValueNotFoundException>())); | ||
| }, skip: true); | ||
|
|
||
| test("nothing is found in an empty array", () { | ||
| expect(() => BinarySearch([]).find(1), throwsA(isA<ValueNotFoundException>())); | ||
| }, skip: true); | ||
|
|
||
| test("nothing is found when the left and right bounds cross", () { | ||
| expect(() => BinarySearch([1, 2]).find(0), throwsA(isA<ValueNotFoundException>())); | ||
| }, skip: true); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing author
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created an issue for configlet.