Skip to content

Commit

Permalink
Add examples for Zig language (egonSchiele#242)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
kind84 authored Nov 18, 2022
1 parent f03841e commit 5d9ae51
Show file tree
Hide file tree
Showing 22 changed files with 1,165 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ highlights/node_modules
highlights/atom-language-perl6/
.DS_store
highlights/package-lock.json
zig-cache

# IDE specific
.scala_dependencies
Expand All @@ -41,4 +42,4 @@ highlights/package-lock.json
/.env
atlassian-ide-plugin.xml
__pycache__
.vscode
.vscode
36 changes: 36 additions & 0 deletions 01_introduction_to_algorithms/zig/binary-search.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const std = @import("std");
const print = std.debug.print;
const expect = std.testing.expect;

pub fn main() void {
const my_list = &[_]i8{ 1, 3, 5, 7, 9 };

print("{?}\n", .{binarySearch(i8, my_list, 3)});
print("{?}\n", .{binarySearch(i8, my_list, -1)});
}

fn binarySearch(comptime T: type, list: []const T, item: T) ?usize {
var low: i32 = 0;
var high: i32 = @intCast(i32, list.len) - 1;

return while (low <= high) {
var mid = @divTrunc((low + high), 2);
var m = @intCast(usize, mid);
var guess = list[m];
if (guess == item) break m;
if (guess > item) {
high = mid - 1;
} else low = mid + 1;
} else null;
}

test "binarySearch" {
const my_list = &[_]i8{ 1, 3, 5, 7, 9 };

var i = binarySearch(i8, my_list, 3);
try expect(i != null);
try expect(i.? == 1);

i = binarySearch(i8, my_list, -1);
try expect(i == null);
}
35 changes: 35 additions & 0 deletions 02_selection_sort/zig/selection_sort.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const std = @import("std");
const print = std.debug.print;
const expect = std.testing.expect;

pub fn main() !void {
var s = [_]i32{ 5, 3, 6, 2, 10 };

selectionSort(i32, s[0..]);
print("{d}\n", .{s});
}

fn selectionSort(comptime T: type, list: []T) void {
for (list) |_, i| {
var j = i + 1;
while (j < list.len) : (j += 1) {
if (list[i] > list[j]) {
// swap
var tmp = list[i];
list[i] = list[j];
list[j] = tmp;
}
}
}
}

test "selectionSort" {
var s = [_]i32{ 5, 3, 6, 2, 10 };
const exp = [_]i32{ 2, 3, 5, 6, 10 };

selectionSort(i32, s[0..]);

try expect(s.len == exp.len);
for (s) |e, i|
try expect(e == exp[i]);
}
13 changes: 13 additions & 0 deletions 03_recursion/zig/01_countdown.zig
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions 03_recursion/zig/02_greet.zig
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions 03_recursion/zig/03_factorial.zig
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions 03_recursion/zig/04_count.zig
Original file line number Diff line number Diff line change
@@ -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 changes: 59 additions & 0 deletions 03_recursion/zig/05_binary_search_recursive.zig
Original file line number Diff line number Diff line change
@@ -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 changes: 47 additions & 0 deletions 03_recursion/zig/06_find_max.zig
Original file line number Diff line number Diff line change
@@ -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 changes: 38 additions & 0 deletions 03_recursion/zig/07_sum_array.zig
Original file line number Diff line number Diff line change
@@ -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);
}
}
38 changes: 38 additions & 0 deletions 04_quicksort/zig/01_loop_sum.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const print = @import("std").debug.print;
const expect = @import("std").testing.expect;

pub fn main() void {
var arr = [_]i32{ 1, 2, 3, 4 };
print("{}\n", .{sum(i32, &arr)});
}

fn sum(comptime T: type, arr: []T) T {
var total: T = 0;
for (arr) |x| {
total += x;
}
return total;
}

test "sum" {
var arr0 = [_]i32{ 1, 2, 3, 4 };
var arr1 = [_]i32{};
var tests = [_]struct {
arr: []i32,
exp: i32,
}{
.{
.arr = &arr0,
.exp = 10,
},
.{
.arr = &arr1,
.exp = 0,
},
};

for (tests) |t| {
var n = sum(@TypeOf(t.exp), t.arr);
try expect(n == t.exp);
}
}
37 changes: 37 additions & 0 deletions 04_quicksort/zig/02_recursive_sum.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const print = @import("std").debug.print;
const expect = @import("std").testing.expect;

pub fn main() void {
var list = [_]i32{ 1, 2, 3, 4 };
print("{}\n", .{sum(i32, &list)});
}

fn sum(comptime T: type, list: []T) T {
if (list.len == 0) {
return 0;
}
return list[0] + sum(T, list[1..]);
}

test "sum" {
var arr0 = [_]i32{ 1, 2, 3, 4 };
var arr1 = [_]i32{};
var tests = [_]struct {
arr: []i32,
exp: i32,
}{
.{
.arr = &arr0,
.exp = 10,
},
.{
.arr = &arr1,
.exp = 0,
},
};

for (tests) |t| {
var n = sum(@TypeOf(t.exp), t.arr);
try expect(n == t.exp);
}
}
Loading

0 comments on commit 5d9ae51

Please sign in to comment.