Add examples for Zig language (#242)
* add zig examples * improved zig binary search This commit improves the binary search code in zig. The function has been made generic and the logic has been cleaned up a bit. The code has been updated to work with zig versions >= 0.9 * simplify zig selection sort This commit simplifies the logic of the zig selection sort. It now swaps in place the elements of the array instead of creating another array. This avoids allocating heap memory. The code has also been upgraded to zig version 0.9.1 * make zig recursion examples generic This commit modifies the zig examples for the recursion chapter to be generic. It also updates the code to zig version 0.9.1 * update chapter 4 examples This commit updates the zig examples in chapter 4. In particular examples have been made generic where possible. The code has been updated to zig version 0.9.1 * update zig hash table examples This commit updates the examples for the chapter 5 about hash tables. Some improvements have been done (using a set instead of a map). The code has been updated to zig version 0.9.1 * update breadth first search zig example This commit updates the zig example for the breadth first search algorithm. It adds a unit test and updates the code to zig version 0.9.1 * revamp zig dijkstra example * add comments in dijkstra zig * fix zig greedy algorithm * add test for zig dijkstra * add test for zig greedy algorithm * improve zig chapter 9 exercise This commit improves the zig exercise to comput the longest common subsequence. A main function has been added and the allocator code has been extracted from the `subsequence` function.
This commit is contained in:
13
03_recursion/zig/01_countdown.zig
Normal file
13
03_recursion/zig/01_countdown.zig
Normal file
@@ -0,0 +1,13 @@
|
||||
const print = @import("std").debug.print;
|
||||
|
||||
fn countdown(comptime T: type, i: T) void {
|
||||
print("{} ", .{i});
|
||||
if (i <= 0) {
|
||||
print("\n", .{});
|
||||
return;
|
||||
} else countdown(T, i - 1);
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
countdown(u32, 5);
|
||||
}
|
||||
20
03_recursion/zig/02_greet.zig
Normal file
20
03_recursion/zig/02_greet.zig
Normal file
@@ -0,0 +1,20 @@
|
||||
const print = @import("std").debug.print;
|
||||
|
||||
pub fn main() void {
|
||||
greet("adit");
|
||||
}
|
||||
|
||||
fn bye() void {
|
||||
print("ok bye!\n", .{});
|
||||
}
|
||||
|
||||
fn greet(name: []const u8) void {
|
||||
print("hello, {s}!\n", .{name});
|
||||
greet2(name);
|
||||
print("getting ready to say bye...\n", .{});
|
||||
bye();
|
||||
}
|
||||
|
||||
fn greet2(name: []const u8) void {
|
||||
print("how are you, {s}?\n", .{name});
|
||||
}
|
||||
11
03_recursion/zig/03_factorial.zig
Normal file
11
03_recursion/zig/03_factorial.zig
Normal file
@@ -0,0 +1,11 @@
|
||||
const print = @import("std").debug.print;
|
||||
|
||||
fn fact(comptime T: type, x: T) T {
|
||||
if (x == 1) {
|
||||
return x;
|
||||
} else return x * fact(T, x - 1);
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
print("{}\n", .{fact(i32, 5)});
|
||||
}
|
||||
40
03_recursion/zig/04_count.zig
Normal file
40
03_recursion/zig/04_count.zig
Normal file
@@ -0,0 +1,40 @@
|
||||
const print = @import("std").debug.print;
|
||||
const expect = @import("std").testing.expect;
|
||||
|
||||
pub fn main() void {
|
||||
var arr = [_]i32{ 4, 3, 2, 1 };
|
||||
print("{}\n", .{count(i32, arr[0..])});
|
||||
}
|
||||
|
||||
fn count(comptime T: type, arr: []T) T {
|
||||
if (arr.len == 0) {
|
||||
return 0;
|
||||
} else return 1 + count(T, arr[1..]);
|
||||
}
|
||||
|
||||
test "count" {
|
||||
var arr0 = [_]i32{};
|
||||
var arr1 = [_]i32{42};
|
||||
var arr2 = [_]i32{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
var tests = [_]struct {
|
||||
arr: []i32,
|
||||
exp: i32,
|
||||
}{
|
||||
.{
|
||||
.arr = &arr0,
|
||||
.exp = 0,
|
||||
},
|
||||
.{
|
||||
.arr = &arr1,
|
||||
.exp = 1,
|
||||
},
|
||||
.{
|
||||
.arr = &arr2,
|
||||
.exp = 9,
|
||||
},
|
||||
};
|
||||
|
||||
for (tests) |t| {
|
||||
try expect(count(@TypeOf(t.exp), t.arr) == t.exp);
|
||||
}
|
||||
}
|
||||
59
03_recursion/zig/05_binary_search_recursive.zig
Normal file
59
03_recursion/zig/05_binary_search_recursive.zig
Normal file
@@ -0,0 +1,59 @@
|
||||
const print = @import("std").debug.print;
|
||||
const expect = @import("std").testing.expect;
|
||||
|
||||
pub fn main() void {
|
||||
print("{}\n", .{binarySearch(i32, &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 2)});
|
||||
}
|
||||
|
||||
fn binarySearch(comptime T: type, arr: []const T, target: T) bool {
|
||||
switch (arr.len) {
|
||||
0 => return false,
|
||||
1 => return arr[0] == target,
|
||||
else => {
|
||||
const mid = arr.len / 2;
|
||||
if (arr[mid] > target) {
|
||||
return binarySearch(T, arr[0..mid], target);
|
||||
} else {
|
||||
return binarySearch(T, arr[mid..], target);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test "binary search recursive" {
|
||||
const tests = [_]struct {
|
||||
arr: []const i32,
|
||||
target: i32,
|
||||
exp: bool,
|
||||
}{
|
||||
.{
|
||||
.arr = &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
|
||||
.target = 7,
|
||||
.exp = true,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
|
||||
.target = 42,
|
||||
.exp = false,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{42},
|
||||
.target = 42,
|
||||
.exp = true,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{1},
|
||||
.target = 42,
|
||||
.exp = false,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{},
|
||||
.target = 42,
|
||||
.exp = false,
|
||||
},
|
||||
};
|
||||
|
||||
for (tests) |t| {
|
||||
try expect(binarySearch(@TypeOf(t.target), t.arr, t.target) == t.exp);
|
||||
}
|
||||
}
|
||||
47
03_recursion/zig/06_find_max.zig
Normal file
47
03_recursion/zig/06_find_max.zig
Normal file
@@ -0,0 +1,47 @@
|
||||
const print = @import("std").debug.print;
|
||||
const expect = @import("std").testing.expect;
|
||||
|
||||
pub fn main() void {
|
||||
print("{}\n", .{findMax(i32, &[_]i32{ 1, 2, 3, 4 })});
|
||||
}
|
||||
|
||||
fn findMax(comptime T: type, arr: []const T) T {
|
||||
switch (arr.len) {
|
||||
0 => return 0,
|
||||
1 => return arr[0],
|
||||
else => {
|
||||
const x = findMax(T, arr[1..]);
|
||||
if (arr[0] > x) {
|
||||
return arr[0];
|
||||
} else return x;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test "find max" {
|
||||
const tests = [_]struct {
|
||||
arr: []const i32,
|
||||
exp: i32,
|
||||
}{
|
||||
.{
|
||||
.arr = &[_]i32{ 1, 2, 3, 4 },
|
||||
.exp = 4,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{ 8, 42, 3, 1 },
|
||||
.exp = 42,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{42},
|
||||
.exp = 42,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{},
|
||||
.exp = 0,
|
||||
},
|
||||
};
|
||||
|
||||
for (tests) |t| {
|
||||
try expect(findMax(@TypeOf(t.exp), t.arr) == t.exp);
|
||||
}
|
||||
}
|
||||
38
03_recursion/zig/07_sum_array.zig
Normal file
38
03_recursion/zig/07_sum_array.zig
Normal file
@@ -0,0 +1,38 @@
|
||||
const print = @import("std").debug.print;
|
||||
const expect = @import("std").testing.expect;
|
||||
|
||||
pub fn main() void {
|
||||
print("{}\n", .{sumArray(i32, &[_]i32{ 1, 2, 3, 4 })});
|
||||
}
|
||||
|
||||
fn sumArray(comptime T: type, arr: []const T) T {
|
||||
switch (arr.len) {
|
||||
0 => return 0,
|
||||
1 => return arr[0],
|
||||
else => return arr[0] + sumArray(T, arr[1..]),
|
||||
}
|
||||
}
|
||||
|
||||
test "sum array" {
|
||||
const tests = [_]struct {
|
||||
arr: []const i32,
|
||||
exp: i32,
|
||||
}{
|
||||
.{
|
||||
.arr = &[_]i32{ 1, 2, 3, 4 },
|
||||
.exp = 10,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{42},
|
||||
.exp = 42,
|
||||
},
|
||||
.{
|
||||
.arr = &[_]i32{},
|
||||
.exp = 0,
|
||||
},
|
||||
};
|
||||
|
||||
for (tests) |t| {
|
||||
try expect(sumArray(@TypeOf(t.exp), t.arr) == t.exp);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user