From 28dc20913490890cd8e3b833d446da12eddc248d Mon Sep 17 00:00:00 2001 From: mitteneer Date: Sun, 21 Jul 2024 23:09:03 -0400 Subject: [PATCH] 47-60 --- 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 +++--- exercises/057_unions3.zig | 2 +- exercises/058_quiz7.zig | 8 ++++---- exercises/059_integers.zig | 6 +++--- exercises/060_floats.zig | 2 +- 14 files changed, 41 insertions(+), 35 deletions(-) diff --git a/exercises/047_methods.zig b/exercises/047_methods.zig index 3221ca2..3a2bac5 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..80d20eb 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 = Err.Cthulhu; 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 4e98d8e..debd521 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. // @@ -163,13 +163,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}), } diff --git a/exercises/057_unions3.zig b/exercises/057_unions3.zig index a5017d2..0c3c3b2 100644 --- a/exercises/057_unions3.zig +++ b/exercises/057_unions3.zig @@ -15,7 +15,7 @@ // const std = @import("std"); -const Insect = union(InsectStat) { +const Insect = union(enum) { flowers_visited: u16, still_alive: bool, }; diff --git a/exercises/058_quiz7.zig b/exercises/058_quiz7.zig index cf32fc3..1fc5d85 100644 --- a/exercises/058_quiz7.zig +++ b/exercises/058_quiz7.zig @@ -192,8 +192,8 @@ const TripItem = union(enum) { // Oops! The hermit forgot how to capture the union values // in a switch statement. Please capture both values as // 'p' so the print statements work! - .place => print("{s}", .{p.name}), - .path => print("--{}->", .{p.dist}), + .place => |p| print("{s}", .{p.name}), + .path => |p| print("--{}->", .{p.dist}), } } }; @@ -255,7 +255,7 @@ const HermitsNotebook = struct { // dereference and optional value "unwrapping" look // together. Remember that you return the address with the // "&" operator. - if (place == entry.*.?.place) return entry; + if (place == entry.*.?.place) return &entry.*.?; // Try to make your answer this long:__________; } return null; @@ -309,7 +309,7 @@ const HermitsNotebook = struct { // // Looks like the hermit forgot something in the return value of // this function. What could that be? - fn getTripTo(self: *HermitsNotebook, trip: []?TripItem, dest: *Place) void { + fn getTripTo(self: *HermitsNotebook, trip: []?TripItem, dest: *Place) TripError!void { // We start at the destination entry. const destination_entry = self.getEntry(dest); diff --git a/exercises/059_integers.zig b/exercises/059_integers.zig index ae65790..51b90cc 100644 --- a/exercises/059_integers.zig +++ b/exercises/059_integers.zig @@ -20,9 +20,9 @@ const print = @import("std").debug.print; pub fn main() void { const zig = [_]u8{ - 0o131, // octal - 0b1101000, // binary - 0x66, // hex + 'Z', // octal + 'i', // binary + 'g', // hex }; print("{s} is cool.\n", .{zig}); diff --git a/exercises/060_floats.zig b/exercises/060_floats.zig index 6f341ad..0754323 100644 --- a/exercises/060_floats.zig +++ b/exercises/060_floats.zig @@ -43,7 +43,7 @@ pub fn main() void { // // We'll convert this weight from pound to kilograms at a // conversion of 0.453592kg to the pound. - const shuttle_weight: f16 = 0.453592 * 4480e6; + const shuttle_weight: f32 = 0.453592 * 4480e3; // By default, float values are formatted in scientific // notation. Try experimenting with '{d}' and '{d:.3}' to see