Update examples for Zig (#287)

* update zig in chapters 1-6

* fix zig dijkstras algo

* fix zig greedy algo

* fix longest_common_subsequence in zig

* cleanup

* test: use testing allocator
This commit is contained in:
Paolo Grisoli
2024-12-07 14:29:48 +01:00
committed by GitHub
parent 177581a9a4
commit 8a13efde83
13 changed files with 357 additions and 260 deletions

View File

@@ -11,12 +11,13 @@ pub fn main() void {
fn binarySearch(comptime T: type, list: []const T, item: T) ?usize {
var low: i32 = 0;
var high: i32 = @intCast(i32, list.len) - 1;
const u_high: u32 = @truncate(list.len);
var high: i32 = @intCast(u_high - 1);
return while (low <= high) {
var mid = @divTrunc((low + high), 2);
var m = @intCast(usize, mid);
var guess = list[m];
const mid = @divTrunc((low + high), 2);
const m: usize = @intCast(mid);
const guess = list[m];
if (guess == item) break m;
if (guess > item) {
high = mid - 1;