From 2f5b658c3dd2a3bab990cea2b2db23b1038c656d Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Thu, 21 Sep 2023 12:37:01 +0200 Subject: [PATCH 1/3] exercises(matching-brackets): example: avoid using allocator The user is allowed to add conditions like this to the doc comment of a tested function. --- .../matching-brackets/.meta/example.zig | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/exercises/practice/matching-brackets/.meta/example.zig b/exercises/practice/matching-brackets/.meta/example.zig index 12f3aeeb..c128d7b8 100644 --- a/exercises/practice/matching-brackets/.meta/example.zig +++ b/exercises/practice/matching-brackets/.meta/example.zig @@ -1,20 +1,34 @@ const std = @import("std"); const mem = std.mem; +fn toOpening(c: u8) u8 { + return switch (c) { + ')' => '(', + ']' => '[', + '}' => '{', + else => unreachable, + }; +} + /// Returns whether the characters `(`, `[`, and `{` are matched and correctly nested in `s`. -pub fn isBalanced(allocator: mem.Allocator, s: []const u8) mem.Allocator.Error!bool { - var stack = std.ArrayList(u8).init(allocator); - defer stack.deinit(); +/// Caller guarantees that the nesting depth of those characters in `s` is at most 10. +// The tests use `try`, so this function returns `!bool` even though it cannot return an error. +pub fn isBalanced(_: mem.Allocator, s: []const u8) !bool { + var stack: [10]u8 = undefined; + var i: usize = 0; for (s) |c| { switch (c) { - '(', '[', '{' => try stack.append(c), - ')' => if (stack.items.len == 0 or stack.pop() != '(') return false, - ']' => if (stack.items.len == 0 or stack.pop() != '[') return false, - '}' => if (stack.items.len == 0 or stack.pop() != '{') return false, + '(', '[', '{' => { + stack[i] = c; + i += 1; + }, + ')', ']', '}' => { + if (i > 0 and stack[i - 1] == toOpening(c)) i -= 1 else return false; + }, else => continue, } } - return stack.items.len == 0; + return i == 0; } From 3d6c4628f0122f2375309597ff48f956611a4200 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:00:01 +0200 Subject: [PATCH 2/3] exercises(matching-brackets): increase maximum supported nesting depth --- exercises/practice/matching-brackets/.meta/example.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/matching-brackets/.meta/example.zig b/exercises/practice/matching-brackets/.meta/example.zig index c128d7b8..59329e3a 100644 --- a/exercises/practice/matching-brackets/.meta/example.zig +++ b/exercises/practice/matching-brackets/.meta/example.zig @@ -11,10 +11,10 @@ fn toOpening(c: u8) u8 { } /// Returns whether the characters `(`, `[`, and `{` are matched and correctly nested in `s`. -/// Caller guarantees that the nesting depth of those characters in `s` is at most 10. +/// Caller guarantees that the nesting depth of those characters in `s` is at most 100. // The tests use `try`, so this function returns `!bool` even though it cannot return an error. pub fn isBalanced(_: mem.Allocator, s: []const u8) !bool { - var stack: [10]u8 = undefined; + var stack: [100]u8 = undefined; var i: usize = 0; for (s) |c| { From e97b760ed9862f7c1e1a3af08a2c21e52d9b7183 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 23 Sep 2023 20:00:02 +0200 Subject: [PATCH 3/3] exercises(matching-brackets): change toOpening to toClosing Add the closing character to the stack, not the opening one. This makes the body of the helper function look more natural, and reduces the complexity of the longer line in isBalanced. --- .../practice/matching-brackets/.meta/example.zig | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/exercises/practice/matching-brackets/.meta/example.zig b/exercises/practice/matching-brackets/.meta/example.zig index 59329e3a..dc4bbe86 100644 --- a/exercises/practice/matching-brackets/.meta/example.zig +++ b/exercises/practice/matching-brackets/.meta/example.zig @@ -1,11 +1,11 @@ const std = @import("std"); const mem = std.mem; -fn toOpening(c: u8) u8 { +fn toClosing(c: u8) u8 { return switch (c) { - ')' => '(', - ']' => '[', - '}' => '{', + '(' => ')', + '[' => ']', + '{' => '}', else => unreachable, }; } @@ -20,11 +20,11 @@ pub fn isBalanced(_: mem.Allocator, s: []const u8) !bool { for (s) |c| { switch (c) { '(', '[', '{' => { - stack[i] = c; + stack[i] = toClosing(c); i += 1; }, ')', ']', '}' => { - if (i > 0 and stack[i - 1] == toOpening(c)) i -= 1 else return false; + if (i > 0 and stack[i - 1] == c) i -= 1 else return false; }, else => continue, }