Skip to content

Commit

Permalink
Zig format everything
Browse files Browse the repository at this point in the history
Zig, ya dingus. For the longest time I assumed `zig fmt` naturally
produced ugly code. Maybe I, a maladroit bottom feeder, simply
failed to understand the glory that is congested lines of code.

I searched up "zig fmt ugly." I came across: ziglang/zig#3057

It turns out that dangling a comma at the end of a sequence hints to `zig fmt` that it should vertically space out code. This fixes some of the uneditable and unreadable lines of code I had prior to this commit.
  • Loading branch information
joshuamegnauth54 committed Dec 31, 2023
1 parent 327bba2 commit 6bb15c1
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 26 deletions.
30 changes: 26 additions & 4 deletions Zig/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,36 @@ pub fn build(b: *Build) void {
const optimize = b.standardOptimizeOption(.{});

// Modules
const traits = b.addModule("traits", .{ .source_file = .{ .path = "src/traits.zig" } });
const traits = b.addModule("traits", .{
.source_file = .{
.path = "src/traits.zig",
},
});

const search = b.addModule("search", .{ .source_file = .{ .path = "src/search.zig" }, .dependencies = &.{.{ .name = "traits", .module = traits }} });
const search = b.addModule("search", .{
.source_file = .{ .path = "src/search.zig" },
.dependencies = &.{
.{
.name = "traits",
.module = traits,
},
},
});

const neet = b.addModule("neet", .{ .source_file = .{ .path = "src/neet.zig" } });
const neet = b.addModule("neet", .{
.source_file = .{
.path = "src/neet.zig",
},
});

// Tests
const tests_exe = b.addTest("src/tests.zig", .{ .target = target, .optimize = optimize });
const tests_exe = b.addTest(
"src/tests.zig",
.{
.target = target,
.optimize = optimize,
},
);
tests_exe.setMainPkgPath("src");
tests_exe.addModule(traits);
tests_exe.addModule(search);
Expand Down
33 changes: 30 additions & 3 deletions Zig/src/neet/group_anagrams.zig
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,40 @@ fn group_anagram_print(groups: []ArrayList([]const u8)) void {
}

test "group anagrams correct groups" {
const words = [_][]const u8{ "tacos", "star", "angle", "angel", "coast", "asleep", "introduces", "please", "arts", "space", "cats", "dog", "god", "rosiest", "resort", "stories" };
const words = [_][]const u8{
"tacos",
"star",
"angle",
"angel",
"coast",
"asleep",
"introduces",
"please",
"arts",
"space",
"cats",
"dog",
"god",
"rosiest",
"resort",
"stories",
};

const groups = try group_anagrams(std.testing.allocator, &words);
defer group_anagram_deinit(std.testing.allocator, groups);

// FIXME: Zig format mangles this code...
const expected = [10][]const []const u8{ &[2][]const u8{ "angle", "angel" }, &[1][]const u8{"introduces"}, &[1][]const u8{"resort"}, &[2][]const u8{ "asleep", "please" }, &[2][]const u8{ "rosiest", "stories" }, &[2][]const u8{ "tacos", "coast" }, &[1][]const u8{"cats"}, &[2][]const u8{ "star", "arts" }, &[2][]const u8{ "dog", "god" }, &[1][]const u8{"space"} };
const expected = [10][]const []const u8{
&[2][]const u8{ "angle", "angel" },
&[1][]const u8{"introduces"},
&[1][]const u8{"resort"},
&[2][]const u8{ "asleep", "please" },
&[2][]const u8{ "rosiest", "stories" },
&[2][]const u8{ "tacos", "coast" },
&[1][]const u8{"cats"},
&[2][]const u8{ "star", "arts" },
&[2][]const u8{ "dog", "god" },
&[1][]const u8{"space"},
};

try expectEqual(expected.len, groups.len);
group_anagram_print(groups);
Expand Down
8 changes: 7 additions & 1 deletion Zig/src/neet/k_largest.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ const PriorityQueue = std.PriorityQueue;
const Order = std.math.Order;
const expectEqual = std.testing.expectEqual;

pub fn k_largest(comptime T: type, allocator: Allocator, haystack: []const T, k: usize, comptime compfunc: ?fn (void, T, T) Order) !?T {
pub fn k_largest(
comptime T: type,
allocator: Allocator,
haystack: []const T,
k: usize,
comptime compfunc: ?fn (void, T, T) Order,
) !?T {
const compfunc_def = comptime switch (@typeInfo(T)) {
.Int, .ComptimeInt, .Float, .ComptimeFloat => opcomp: {
if (compfunc) |f| {
Expand Down
9 changes: 8 additions & 1 deletion Zig/src/neet/longest_prefix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ fn desc_str(_: void, a: []const u8, b: []const u8) bool {
}

test "longest prefix found" {
const words = [_][]const u8{ "good", "goofy", "goober", "gopher", "going", "gone" };
const words = [_][]const u8{
"good",
"goofy",
"goober",
"gopher",
"going",
"gone",
};
var result = try longest_prefix(std.testing.allocator, &words);

if (result) |actual| {
Expand Down
6 changes: 5 additions & 1 deletion Zig/src/neet/replace_vals_right.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ const expect = std.testing.expect;
/// Copy `nums` with each value replaced with that of the maximum value to its right.
///
/// Caller owns the memory if the length isn't zero.
pub fn replace_values_right(comptime T: type, allocator: Allocator, nums: []const T) ![]T {
pub fn replace_values_right(
comptime T: type,
allocator: Allocator,
nums: []const T,
) ![]T {
// T may be any signed int
comptime switch (@typeInfo(T)) {
.Int => |int| {
Expand Down
85 changes: 74 additions & 11 deletions Zig/src/neet/three_sum.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ fn clean_map(comptime T: type, map: *AutoHashMap(T, ArrayList(usize))) void {
}
}

pub fn three_sum_set(comptime T: type, allocator: Allocator, haystack: []const T, target: T) !?[][3]T {
pub fn three_sum_set(
comptime T: type,
allocator: Allocator,
haystack: []const T,
target: T,
) !?[][3]T {
comptime if (@typeInfo(T) != .Int and @typeInfo(T) != .ComptimeInt) {
@compileError("T must be an integer");
};
Expand Down Expand Up @@ -108,7 +113,12 @@ pub fn three_sum_set(comptime T: type, allocator: Allocator, haystack: []const T
return sums_out.toOwnedSlice();
}

pub fn three_sum_ptr(comptime T: type, allocator: Allocator, haystack: []const T, target: T) !?[][3]T {
pub fn three_sum_ptr(
comptime T: type,
allocator: Allocator,
haystack: []const T,
target: T,
) !?[][3]T {
comptime if (@typeInfo(T) != .Int and @typeInfo(T) != .ComptimeInt) {
@compileError("T must be an integer");
};
Expand Down Expand Up @@ -191,7 +201,18 @@ fn test_print_triplets(comptime T: type, triplets: []const [3]T) void {
const ThreeSumError = error{OutOfMemory};

// Convenience function to test 3sum
fn test_three_sum(comptime T: type, haystack: []const T, target: T, expected: []const [3]T, comptime three_sum: fn (comptime T: type, allocator: Allocator, haystack: []const T, target: T) ThreeSumError!?[][3]T) !void {
fn test_three_sum(
comptime T: type,
haystack: []const T,
target: T,
expected: []const [3]T,
comptime three_sum: fn (
comptime T: type,
allocator: Allocator,
haystack: []const T,
target: T,
) ThreeSumError!?[][3]T,
) !void {
const res = try three_sum(T, std.testing.allocator, haystack, target);
if (res) |sums| {
// test_print_triplets(T, sums);
Expand All @@ -215,47 +236,89 @@ fn test_three_sum(comptime T: type, haystack: []const T, target: T, expected: []
test "3sum set example 1" {
const haystack = [_]i8{ -1, 0, 1, 2, -1, -4 };
const target = 0;
const expected = [_][3]i8{ [3]i8{ -1, -1, 2 }, [3]i8{ -1, 0, 1 } };
const expected = [_][3]i8{
[3]i8{ -1, -1, 2 },
[3]i8{ -1, 0, 1 },
};

try test_three_sum(i8, &haystack, target, &expected, three_sum_set);
try test_three_sum(
i8,
&haystack,
target,
&expected,
three_sum_set,
);
}

test "3sum set example 2" {
const haystack = [_]i8{ 0, 1, 1 };
const target = 0;
const expected = [0][3]i8{};

try test_three_sum(i8, &haystack, target, &expected, three_sum_set);
try test_three_sum(
i8,
&haystack,
target,
&expected,
three_sum_set,
);
}

test "3sum set example 3" {
const haystack = [_]i8{ 0, 0, 0 };
const target = 0;
const expected = [1][3]i8{[3]i8{ 0, 0, 0 }};

try test_three_sum(i8, &haystack, target, &expected, three_sum_set);
try test_three_sum(
i8,
&haystack,
target,
&expected,
three_sum_set,
);
}

test "3sum pointer example 1" {
const haystack = [_]i8{ -1, 0, 1, 2, -1, -4 };
const target = 0;
const expected = [_][3]i8{ [3]i8{ -1, -1, 2 }, [3]i8{ -1, 0, 1 } };
const expected = [_][3]i8{
[3]i8{ -1, -1, 2 },
[3]i8{ -1, 0, 1 },
};

try test_three_sum(i8, &haystack, target, &expected, three_sum_ptr);
try test_three_sum(
i8,
&haystack,
target,
&expected,
three_sum_ptr,
);
}

test "3sum pointer example 2" {
const haystack = [_]i8{ 0, 1, 1 };
const target = 0;
const expected = [0][3]i8{};

try test_three_sum(i8, &haystack, target, &expected, three_sum_ptr);
try test_three_sum(
i8,
&haystack,
target,
&expected,
three_sum_ptr,
);
}

test "3sum pointer example 3" {
const haystack = [_]i8{ 0, 0, 0 };
const target = 0;
const expected = [1][3]i8{[3]i8{ 0, 0, 0 }};

try test_three_sum(i8, &haystack, target, &expected, three_sum_ptr);
try test_three_sum(
i8,
&haystack,
target,
&expected,
three_sum_ptr,
);
}
23 changes: 20 additions & 3 deletions Zig/src/neet/two_sum_sorted.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ const expectEqual = std.testing.expectEqual;

const ResTwoSum = @import("two_sum.zig").ResTwoSum;

pub fn two_sum_sorted_search(comptime T: type, haystack: []T, target: T) ?ResTwoSum {
pub fn two_sum_sorted_search(
comptime T: type,
haystack: []T,
target: T,
) ?ResTwoSum {
comptime if (@typeInfo(T) != .Int and @typeInfo(T) != .ComptimeInt) {
@compileError("T must be an integer of any precision");
};
Expand Down Expand Up @@ -42,7 +46,11 @@ pub fn two_sum_sorted_search(comptime T: type, haystack: []T, target: T) ?ResTwo
return null;
}

pub fn two_sum_sorted_pointer(comptime T: type, haystack: []T, target: T) ?ResTwoSum {
pub fn two_sum_sorted_pointer(
comptime T: type,
haystack: []T,
target: T,
) ?ResTwoSum {
comptime if (@typeInfo(T) != .Int and @typeInfo(T) != .ComptimeInt) {
@compileError("T must be an integer of any precision");
};
Expand Down Expand Up @@ -73,7 +81,16 @@ pub fn two_sum_sorted_pointer(comptime T: type, haystack: []T, target: T) ?ResTw
return null;
}

fn test_two_sum(comptime T: type, haystack: []T, target: T, comptime two_sum: fn (comptime T: type, haystack: []T, target: T) ?ResTwoSum) void {
fn test_two_sum(
comptime T: type,
haystack: []T,
target: T,
comptime two_sum: fn (
comptime T: type,
haystack: []T,
target: T,
) ?ResTwoSum,
) void {
const res = two_sum(T, haystack, target);
if (res) |indices| {
const lower = haystack[indices.i];
Expand Down
14 changes: 12 additions & 2 deletions Zig/src/neet/valid_palindrome.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ pub fn valid_palindrome(s: []const u8) bool {
}

test "valid palindromes should succeed" {
const palindromes = [_][]const u8{ "level", "repaper", "peeweep", "tattarrattat" };
const palindromes = [_][]const u8{
"level",
"repaper",
"peeweep",
"tattarrattat",
};

for (palindromes) |palindrome| {
try expect(valid_palindrome(palindrome));
Expand All @@ -42,7 +47,12 @@ test "invalid palindromes should fail" {
}

test "words that aren't palindromes should fail" {
const not_palins = [_][]const u8{ "fat", "cat", "on a", "mat" };
const not_palins = [_][]const u8{
"fat",
"cat",
"on a",
"mat",
};

for (not_palins) |s| {
try expect(!valid_palindrome(s));
Expand Down

0 comments on commit 6bb15c1

Please sign in to comment.