-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exercises(binary-search): return an optional, not an error union (#259)
Before this commit, `binary-search` required the user to return an error when the array of input items: - was empty - or did not contain the target Return an optional instead, like `std.sort.binarySearch` does [1]. Now, an empty input array is just another case of "value not found". Also rename `buffer` to `items` for similar consistency with upstream [1]. Elsewhere we have used the name `buffer` only for a variable that is mutated by a function. [1] https://github.com/ziglang/zig/blob/4414f9c46e77/lib/std/sort.zig#L7-L13 Refs: #229
- Loading branch information
Showing
4 changed files
with
31 additions
and
39 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 |
---|---|---|
@@ -1,23 +1,16 @@ | ||
pub const SearchError = error{ | ||
EmptyBuffer, | ||
ValueAbsent, | ||
}; | ||
|
||
pub fn binarySearch(comptime T: type, target: T, buffer: []const T) SearchError!usize { | ||
if (buffer.len == 0) return SearchError.EmptyBuffer; | ||
|
||
pub fn binarySearch(comptime T: type, target: T, items: []const T) ?usize { | ||
var left: usize = 0; | ||
var right = buffer.len; | ||
var right = items.len; | ||
|
||
while (left < right) { | ||
const mid = left + (right - left) / 2; // Avoid overflow. | ||
if (buffer[mid] == target) { | ||
if (items[mid] == target) { | ||
return mid; | ||
} else if (buffer[mid] < target) { | ||
} else if (items[mid] < target) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
return SearchError.ValueAbsent; | ||
return null; | ||
} |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
// Take a look at the tests, you might have to change the function arguments | ||
|
||
pub fn binarySearch(target: usize, buffer: []const usize) SearchError!usize { | ||
pub fn binarySearch(target: usize, items: []const usize) ?usize { | ||
_ = target; | ||
_ = buffer; | ||
_ = items; | ||
@compileError("please implement the binarySearch function"); | ||
} |
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