Skip to content

Commit 5069d46

Browse files
committed
exercises(binary-search): return an optional, not an error union
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` [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], and to reserve the name `buffer` for a variable that is mutated by a function. [1] https://github.com/ziglang/zig/blob/adc6dec26b8b/lib/std/sort.zig#L10 Refs: #229
1 parent 54de267 commit 5069d46

4 files changed

Lines changed: 31 additions & 39 deletions

File tree

config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,16 @@
384384
"name": "Binary Search",
385385
"practices": [
386386
"control-flow",
387-
"error-sets",
388387
"generics",
389388
"functions",
389+
"optionals",
390390
"type-coercion"
391391
],
392392
"prerequisites": [
393393
"control-flow",
394-
"error-sets",
395394
"generics",
396395
"functions",
396+
"optionals",
397397
"type-coercion"
398398
],
399399
"difficulty": 1
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
1-
pub const SearchError = error{
2-
EmptyBuffer,
3-
ValueAbsent,
4-
};
5-
6-
pub fn binarySearch(comptime T: type, target: T, buffer: []const T) SearchError!usize {
7-
if (buffer.len == 0) return SearchError.EmptyBuffer;
8-
1+
pub fn binarySearch(comptime T: type, target: T, items: []const T) ?usize {
92
var left: usize = 0;
10-
var right = buffer.len;
3+
var right = items.len;
114

125
while (left < right) {
136
const mid = left + ((right - left) / 2); // Avoid overflow.
14-
if (buffer[mid] == target) {
7+
if (items[mid] == target) {
158
return mid;
16-
} else if (buffer[mid] < target) {
9+
} else if (items[mid] < target) {
1710
left = mid + 1;
1811
} else {
1912
right = mid;
2013
}
2114
}
22-
return SearchError.ValueAbsent;
15+
return null;
2316
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Take a look at the tests, you might have to change the function arguments
22

3-
pub fn binarySearch(target: usize, buffer: []const usize) SearchError!usize {
3+
pub fn binarySearch(target: usize, items: []const usize) ?usize {
44
_ = target;
5-
_ = buffer;
5+
_ = items;
66
@compileError("please implement the binarySearch function");
77
}

exercises/practice/binary-search/test_binary_search.zig

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,80 @@ const testing = std.testing;
33

44
const binary_search = @import("binary_search.zig");
55
const binarySearch = binary_search.binarySearch;
6-
const SearchError = binary_search.SearchError;
76

87
test "finds a value in an array with one element" {
9-
const expected: usize = 0;
8+
const expected: ?usize = 0;
109
const array = [_]i4{6};
11-
const actual = try binarySearch(i4, 6, &array);
10+
const actual = binarySearch(i4, 6, &array);
1211
try testing.expectEqual(expected, actual);
1312
}
1413

1514
test "finds a value in the middle of an array" {
16-
const expected: usize = 3;
15+
const expected: ?usize = 3;
1716
const array = [_]u4{ 1, 3, 4, 6, 8, 9, 11 };
18-
const actual = try binarySearch(u4, 6, &array);
17+
const actual = binarySearch(u4, 6, &array);
1918
try testing.expectEqual(expected, actual);
2019
}
2120

2221
test "finds a value at the beginning of an array" {
23-
const expected: usize = 0;
22+
const expected: ?usize = 0;
2423
const array = [_]i8{ 1, 3, 4, 6, 8, 9, 11 };
25-
const actual = try binarySearch(i8, 1, &array);
24+
const actual = binarySearch(i8, 1, &array);
2625
try testing.expectEqual(expected, actual);
2726
}
2827

2928
test "finds a value at the end of an array" {
30-
const expected: usize = 6;
29+
const expected: ?usize = 6;
3130
const array = [_]u8{ 1, 3, 4, 6, 8, 9, 11 };
32-
const actual = try binarySearch(u8, 11, &array);
31+
const actual = binarySearch(u8, 11, &array);
3332
try testing.expectEqual(expected, actual);
3433
}
3534

3635
test "finds a value in an array of odd length" {
37-
const expected: usize = 5;
36+
const expected: ?usize = 5;
3837
const array = [_]i16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 };
39-
const actual = try binarySearch(i16, 21, &array);
38+
const actual = binarySearch(i16, 21, &array);
4039
try testing.expectEqual(expected, actual);
4140
}
4241

4342
test "finds a value in an array of even length" {
44-
const expected: usize = 5;
43+
const expected: ?usize = 5;
4544
const array = [_]u16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
46-
const actual = try binarySearch(u16, 21, &array);
45+
const actual = binarySearch(u16, 21, &array);
4746
try testing.expectEqual(expected, actual);
4847
}
4948

5049
test "identifies that a value is not included in the array" {
51-
const expected = SearchError.ValueAbsent;
50+
const expected: ?usize = null;
5251
const array = [_]i32{ 1, 3, 4, 6, 8, 9, 11 };
5352
const actual = binarySearch(i32, 7, &array);
54-
try testing.expectError(expected, actual);
53+
try testing.expectEqual(expected, actual);
5554
}
5655

5756
test "a value smaller than the array's smallest value is not found" {
58-
const expected = SearchError.ValueAbsent;
57+
const expected: ?usize = null;
5958
const array = [_]u32{ 1, 3, 4, 6, 8, 9, 11 };
6059
const actual = binarySearch(u32, 0, &array);
61-
try testing.expectError(expected, actual);
60+
try testing.expectEqual(expected, actual);
6261
}
6362

6463
test "a value larger than the array's largest value is not found" {
65-
const expected = SearchError.ValueAbsent;
64+
const expected: ?usize = null;
6665
const array = [_]i64{ 1, 3, 4, 6, 8, 9, 11 };
6766
const actual = binarySearch(i64, 13, &array);
68-
try testing.expectError(expected, actual);
67+
try testing.expectEqual(expected, actual);
6968
}
7069

7170
test "nothing is found in an empty array" {
72-
const expected = SearchError.EmptyBuffer;
71+
const expected: ?usize = null;
7372
const array = [_]u64{};
7473
const actual = binarySearch(u64, 13, &array);
75-
try testing.expectError(expected, actual);
74+
try testing.expectEqual(expected, actual);
7675
}
7776

7877
test "nothing is found when the left and right bounds cross" {
79-
const expected = SearchError.ValueAbsent;
78+
const expected: ?usize = null;
8079
const array = [_]isize{ 1, 2 };
8180
const actual = binarySearch(isize, 13, &array);
82-
try testing.expectError(expected, actual);
81+
try testing.expectEqual(expected, actual);
8382
}

0 commit comments

Comments
 (0)