From 0b00b44e51d6089c1f82dd2c5c9c758459f43726 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Sun, 24 Mar 2024 19:03:57 -0400 Subject: [PATCH] 047-055 completed --- exercises/047_methods.zig | 2 +- exercises/048_methods2.zig | 2 +- exercises/049_quiz6.zig | 8 +++++++- exercises/050_no_value.zig | 8 ++++---- exercises/051_values.zig | 6 +++--- exercises/052_slices.zig | 6 +++--- exercises/053_slices2.zig | 14 +++++++------- exercises/054_manypointers.zig | 2 +- exercises/055_unions.zig | 4 ++-- exercises/056_unions2.zig | 6 +++--- 10 files changed, 32 insertions(+), 26 deletions(-) diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index d2e3ee6..fad36b5 100644 --- a/exercises/047_methods.zig +++ b/exercises/047_methods.zig @@ -88,7 +88,7 @@ pub fn main() void { for (&aliens) |*alien| { // *** Zap the alien with the heat ray here! *** - ???.zap(???); + heat_ray.zap(alien); // If the alien's health is still above 0, it's still alive. if (alien.health > 0) aliens_alive += 1; diff --git a/exercises/048_methods2.zig b/exercises/048_methods2.zig index a1fbe9e..f0ce821 100644 --- a/exercises/048_methods2.zig +++ b/exercises/048_methods2.zig @@ -54,7 +54,7 @@ fn visitElephants(first_elephant: *Elephant) void { // This gets the next elephant or stops: // which method do we want here? - e = if (e.hasTail()) e.??? else break; + e = if (e.hasTail()) e.getTail() else break; } } diff --git a/exercises/049_quiz6.zig b/exercises/049_quiz6.zig index 9dbea51..2da18de 100644 --- a/exercises/049_quiz6.zig +++ b/exercises/049_quiz6.zig @@ -27,7 +27,13 @@ const Elephant = struct { // Your Elephant trunk methods go here! // --------------------------------------------------- - ??? + pub fn getTrunk(self: *Elephant) *Elephant { + return self.trunk.?; + } + + pub fn hasTrunk(self: *Elephant) bool { + return (self.trunk != null); + } // --------------------------------------------------- diff --git a/exercises/050_no_value.zig b/exercises/050_no_value.zig index 8c73ed3..fe78a3f 100644 --- a/exercises/050_no_value.zig +++ b/exercises/050_no_value.zig @@ -65,10 +65,10 @@ const std = @import("std"); const Err = error{Cthulhu}; pub fn main() void { - var first_line1: *const [16]u8 = ???; + var first_line1: *const [16]u8 = undefined; first_line1 = "That is not dead"; - var first_line2: Err!*const [21]u8 = ???; + var first_line2: Err!*const [21]u8 = undefined; first_line2 = "which can eternal lie"; // Note we need the "{!s}" format for the error union string. @@ -77,8 +77,8 @@ pub fn main() void { printSecondLine(); } -fn printSecondLine() ??? { - var second_line2: ?*const [18]u8 = ???; +fn printSecondLine() void { + var second_line2: ?*const [18]u8 = null; second_line2 = "even death may die"; std.debug.print("And with strange aeons {s}.\n", .{second_line2.?}); diff --git a/exercises/051_values.zig b/exercises/051_values.zig index f2653f9..848b6a8 100644 --- a/exercises/051_values.zig +++ b/exercises/051_values.zig @@ -87,7 +87,7 @@ pub fn main() void { // Let's assign the std.debug.print function to a const named // "print" so that we can use this new name later! - const print = ???; + const print = std.debug.print; // Now let's look at assigning and pointing to values in Zig. // @@ -152,13 +152,13 @@ pub fn main() void { print("XP before:{}, ", .{glorp.experience}); // Fix 1 of 2 goes here: - levelUp(glorp, reward_xp); + levelUp(&glorp, reward_xp); print("after:{}.\n", .{glorp.experience}); } // Fix 2 of 2 goes here: -fn levelUp(character_access: Character, xp: u32) void { +fn levelUp(character_access: *Character, xp: u32) void { character_access.experience += xp; } diff --git a/exercises/052_slices.zig b/exercises/052_slices.zig index af5930b..0645e55 100644 --- a/exercises/052_slices.zig +++ b/exercises/052_slices.zig @@ -32,8 +32,8 @@ pub fn main() void { var cards = [8]u8{ 'A', '4', 'K', '8', '5', '2', 'Q', 'J' }; // Please put the first 4 cards in hand1 and the rest in hand2. - const hand1: []u8 = cards[???]; - const hand2: []u8 = cards[???]; + const hand1: []u8 = cards[0..4]; + const hand2: []u8 = cards[4..8]; std.debug.print("Hand1: ", .{}); printHand(hand1); @@ -43,7 +43,7 @@ pub fn main() void { } // Please lend this function a hand. A u8 slice hand, that is. -fn printHand(hand: ???) void { +fn printHand(hand: []u8) void { for (hand) |h| { std.debug.print("{u} ", .{h}); } diff --git a/exercises/053_slices2.zig b/exercises/053_slices2.zig index 545b4da..924f6a3 100644 --- a/exercises/053_slices2.zig +++ b/exercises/053_slices2.zig @@ -17,19 +17,19 @@ const std = @import("std"); pub fn main() void { const scrambled = "great base for all your justice are belong to us"; - const base1: []u8 = scrambled[15..23]; - const base2: []u8 = scrambled[6..10]; - const base3: []u8 = scrambled[32..]; + const base1: []const u8 = scrambled[15..23]; + const base2: []const u8 = scrambled[6..10]; + const base3: []const u8 = scrambled[32..]; printPhrase(base1, base2, base3); - const justice1: []u8 = scrambled[11..14]; - const justice2: []u8 = scrambled[0..5]; - const justice3: []u8 = scrambled[24..31]; + const justice1: []const u8 = scrambled[11..14]; + const justice2: []const u8 = scrambled[0..5]; + const justice3: []const u8 = scrambled[24..31]; printPhrase(justice1, justice2, justice3); std.debug.print("\n", .{}); } -fn printPhrase(part1: []u8, part2: []u8, part3: []u8) void { +fn printPhrase(part1: []const u8, part2: []const u8, part3: []const u8) void { std.debug.print("'{s} {s} {s}.' ", .{ part1, part2, part3 }); } diff --git a/exercises/054_manypointers.zig b/exercises/054_manypointers.zig index ede02df..63a9745 100644 --- a/exercises/054_manypointers.zig +++ b/exercises/054_manypointers.zig @@ -33,7 +33,7 @@ pub fn main() void { // we can CONVERT IT TO A SLICE. (Hint: we do know the length!) // // Please fix this line so the print statement below can print it: - const zen12_string: []const u8 = zen_manyptr; + const zen12_string: []const u8 = zen_manyptr[0..21]; // Here's the moment of truth! std.debug.print("{s}\n", .{zen12_string}); diff --git a/exercises/055_unions.zig b/exercises/055_unions.zig index 794f2df..a88cd2f 100644 --- a/exercises/055_unions.zig +++ b/exercises/055_unions.zig @@ -59,8 +59,8 @@ pub fn main() void { std.debug.print("Insect report! ", .{}); // Oops! We've made a mistake here. - printInsect(ant, AntOrBee.c); - printInsect(bee, AntOrBee.c); + printInsect(ant, AntOrBee.a); + printInsect(bee, AntOrBee.b); std.debug.print("\n", .{}); } diff --git a/exercises/056_unions2.zig b/exercises/056_unions2.zig index c46d133..bebc833 100644 --- a/exercises/056_unions2.zig +++ b/exercises/056_unions2.zig @@ -44,14 +44,14 @@ pub fn main() void { std.debug.print("Insect report! ", .{}); // Could it really be as simple as just passing the union? - printInsect(???); - printInsect(???); + printInsect(ant); + printInsect(bee); std.debug.print("\n", .{}); } fn printInsect(insect: Insect) void { - switch (???) { + switch (insect) { .still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}), .flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}), }