diff --git a/config.json b/config.json index 1abd4b45..bb2bf575 100644 --- a/config.json +++ b/config.json @@ -384,16 +384,16 @@ "name": "Binary Search", "practices": [ "control-flow", - "error-sets", "generics", "functions", + "optionals", "type-coercion" ], "prerequisites": [ "control-flow", - "error-sets", "generics", "functions", + "optionals", "type-coercion" ], "difficulty": 1 diff --git a/exercises/practice/binary-search/.meta/example.zig b/exercises/practice/binary-search/.meta/example.zig index c79acda7..1ec34ccc 100644 --- a/exercises/practice/binary-search/.meta/example.zig +++ b/exercises/practice/binary-search/.meta/example.zig @@ -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; } diff --git a/exercises/practice/binary-search/binary_search.zig b/exercises/practice/binary-search/binary_search.zig index 005dbbb2..bcecc879 100644 --- a/exercises/practice/binary-search/binary_search.zig +++ b/exercises/practice/binary-search/binary_search.zig @@ -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"); } diff --git a/exercises/practice/binary-search/test_binary_search.zig b/exercises/practice/binary-search/test_binary_search.zig index 84a889f3..e1c1f914 100644 --- a/exercises/practice/binary-search/test_binary_search.zig +++ b/exercises/practice/binary-search/test_binary_search.zig @@ -3,81 +3,80 @@ const testing = std.testing; const binary_search = @import("binary_search.zig"); const binarySearch = binary_search.binarySearch; -const SearchError = binary_search.SearchError; test "finds a value in an array with one element" { - const expected: usize = 0; + const expected: ?usize = 0; const array = [_]i4{6}; - const actual = try binarySearch(i4, 6, &array); + const actual = binarySearch(i4, 6, &array); try testing.expectEqual(expected, actual); } test "finds a value in the middle of an array" { - const expected: usize = 3; + const expected: ?usize = 3; const array = [_]u4{ 1, 3, 4, 6, 8, 9, 11 }; - const actual = try binarySearch(u4, 6, &array); + const actual = binarySearch(u4, 6, &array); try testing.expectEqual(expected, actual); } test "finds a value at the beginning of an array" { - const expected: usize = 0; + const expected: ?usize = 0; const array = [_]i8{ 1, 3, 4, 6, 8, 9, 11 }; - const actual = try binarySearch(i8, 1, &array); + const actual = binarySearch(i8, 1, &array); try testing.expectEqual(expected, actual); } test "finds a value at the end of an array" { - const expected: usize = 6; + const expected: ?usize = 6; const array = [_]u8{ 1, 3, 4, 6, 8, 9, 11 }; - const actual = try binarySearch(u8, 11, &array); + const actual = binarySearch(u8, 11, &array); try testing.expectEqual(expected, actual); } test "finds a value in an array of odd length" { - const expected: usize = 5; + const expected: ?usize = 5; const array = [_]i16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634 }; - const actual = try binarySearch(i16, 21, &array); + const actual = binarySearch(i16, 21, &array); try testing.expectEqual(expected, actual); } test "finds a value in an array of even length" { - const expected: usize = 5; + const expected: ?usize = 5; const array = [_]u16{ 1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 }; - const actual = try binarySearch(u16, 21, &array); + const actual = binarySearch(u16, 21, &array); try testing.expectEqual(expected, actual); } test "identifies that a value is not included in the array" { - const expected = SearchError.ValueAbsent; + const expected: ?usize = null; const array = [_]i32{ 1, 3, 4, 6, 8, 9, 11 }; const actual = binarySearch(i32, 7, &array); - try testing.expectError(expected, actual); + try testing.expectEqual(expected, actual); } test "a value smaller than the array's smallest value is not found" { - const expected = SearchError.ValueAbsent; + const expected: ?usize = null; const array = [_]u32{ 1, 3, 4, 6, 8, 9, 11 }; const actual = binarySearch(u32, 0, &array); - try testing.expectError(expected, actual); + try testing.expectEqual(expected, actual); } test "a value larger than the array's largest value is not found" { - const expected = SearchError.ValueAbsent; + const expected: ?usize = null; const array = [_]i64{ 1, 3, 4, 6, 8, 9, 11 }; const actual = binarySearch(i64, 13, &array); - try testing.expectError(expected, actual); + try testing.expectEqual(expected, actual); } test "nothing is found in an empty array" { - const expected = SearchError.EmptyBuffer; + const expected: ?usize = null; const array = [_]u64{}; const actual = binarySearch(u64, 13, &array); - try testing.expectError(expected, actual); + try testing.expectEqual(expected, actual); } test "nothing is found when the left and right bounds cross" { - const expected = SearchError.ValueAbsent; + const expected: ?usize = null; const array = [_]isize{ 1, 2 }; const actual = binarySearch(isize, 13, &array); - try testing.expectError(expected, actual); + try testing.expectEqual(expected, actual); }