047-055 completed

This commit is contained in:
Andrew Scott 2024-03-24 19:03:57 -04:00
parent 1a1b7937f6
commit 0b00b44e51
Signed by: a
GPG key ID: 7CD5A5977E4931C1
10 changed files with 32 additions and 26 deletions

View file

@ -88,7 +88,7 @@ pub fn main() void {
for (&aliens) |*alien| { for (&aliens) |*alien| {
// *** Zap the alien with the heat ray here! *** // *** 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 the alien's health is still above 0, it's still alive.
if (alien.health > 0) aliens_alive += 1; if (alien.health > 0) aliens_alive += 1;

View file

@ -54,7 +54,7 @@ fn visitElephants(first_elephant: *Elephant) void {
// This gets the next elephant or stops: // This gets the next elephant or stops:
// which method do we want here? // which method do we want here?
e = if (e.hasTail()) e.??? else break; e = if (e.hasTail()) e.getTail() else break;
} }
} }

View file

@ -27,7 +27,13 @@ const Elephant = struct {
// Your Elephant trunk methods go here! // 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);
}
// --------------------------------------------------- // ---------------------------------------------------

View file

@ -65,10 +65,10 @@ const std = @import("std");
const Err = error{Cthulhu}; const Err = error{Cthulhu};
pub fn main() void { pub fn main() void {
var first_line1: *const [16]u8 = ???; var first_line1: *const [16]u8 = undefined;
first_line1 = "That is not dead"; 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"; first_line2 = "which can eternal lie";
// Note we need the "{!s}" format for the error union string. // Note we need the "{!s}" format for the error union string.
@ -77,8 +77,8 @@ pub fn main() void {
printSecondLine(); printSecondLine();
} }
fn printSecondLine() ??? { fn printSecondLine() void {
var second_line2: ?*const [18]u8 = ???; var second_line2: ?*const [18]u8 = null;
second_line2 = "even death may die"; second_line2 = "even death may die";
std.debug.print("And with strange aeons {s}.\n", .{second_line2.?}); std.debug.print("And with strange aeons {s}.\n", .{second_line2.?});

View file

@ -87,7 +87,7 @@ pub fn main() void {
// Let's assign the std.debug.print function to a const named // Let's assign the std.debug.print function to a const named
// "print" so that we can use this new name later! // "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. // 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}); print("XP before:{}, ", .{glorp.experience});
// Fix 1 of 2 goes here: // Fix 1 of 2 goes here:
levelUp(glorp, reward_xp); levelUp(&glorp, reward_xp);
print("after:{}.\n", .{glorp.experience}); print("after:{}.\n", .{glorp.experience});
} }
// Fix 2 of 2 goes here: // 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; character_access.experience += xp;
} }

View file

@ -32,8 +32,8 @@ pub fn main() void {
var cards = [8]u8{ 'A', '4', 'K', '8', '5', '2', 'Q', 'J' }; 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. // Please put the first 4 cards in hand1 and the rest in hand2.
const hand1: []u8 = cards[???]; const hand1: []u8 = cards[0..4];
const hand2: []u8 = cards[???]; const hand2: []u8 = cards[4..8];
std.debug.print("Hand1: ", .{}); std.debug.print("Hand1: ", .{});
printHand(hand1); printHand(hand1);
@ -43,7 +43,7 @@ pub fn main() void {
} }
// Please lend this function a hand. A u8 slice hand, that is. // Please lend this function a hand. A u8 slice hand, that is.
fn printHand(hand: ???) void { fn printHand(hand: []u8) void {
for (hand) |h| { for (hand) |h| {
std.debug.print("{u} ", .{h}); std.debug.print("{u} ", .{h});
} }

View file

@ -17,19 +17,19 @@ const std = @import("std");
pub fn main() void { pub fn main() void {
const scrambled = "great base for all your justice are belong to us"; const scrambled = "great base for all your justice are belong to us";
const base1: []u8 = scrambled[15..23]; const base1: []const u8 = scrambled[15..23];
const base2: []u8 = scrambled[6..10]; const base2: []const u8 = scrambled[6..10];
const base3: []u8 = scrambled[32..]; const base3: []const u8 = scrambled[32..];
printPhrase(base1, base2, base3); printPhrase(base1, base2, base3);
const justice1: []u8 = scrambled[11..14]; const justice1: []const u8 = scrambled[11..14];
const justice2: []u8 = scrambled[0..5]; const justice2: []const u8 = scrambled[0..5];
const justice3: []u8 = scrambled[24..31]; const justice3: []const u8 = scrambled[24..31];
printPhrase(justice1, justice2, justice3); printPhrase(justice1, justice2, justice3);
std.debug.print("\n", .{}); 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 }); std.debug.print("'{s} {s} {s}.' ", .{ part1, part2, part3 });
} }

View file

@ -33,7 +33,7 @@ pub fn main() void {
// we can CONVERT IT TO A SLICE. (Hint: we do know the length!) // 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: // 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! // Here's the moment of truth!
std.debug.print("{s}\n", .{zen12_string}); std.debug.print("{s}\n", .{zen12_string});

View file

@ -59,8 +59,8 @@ pub fn main() void {
std.debug.print("Insect report! ", .{}); std.debug.print("Insect report! ", .{});
// Oops! We've made a mistake here. // Oops! We've made a mistake here.
printInsect(ant, AntOrBee.c); printInsect(ant, AntOrBee.a);
printInsect(bee, AntOrBee.c); printInsect(bee, AntOrBee.b);
std.debug.print("\n", .{}); std.debug.print("\n", .{});
} }

View file

@ -44,14 +44,14 @@ pub fn main() void {
std.debug.print("Insect report! ", .{}); std.debug.print("Insect report! ", .{});
// Could it really be as simple as just passing the union? // Could it really be as simple as just passing the union?
printInsect(???); printInsect(ant);
printInsect(???); printInsect(bee);
std.debug.print("\n", .{}); std.debug.print("\n", .{});
} }
fn printInsect(insect: Insect) void { fn printInsect(insect: Insect) void {
switch (???) { switch (insect) {
.still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}), .still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}),
.flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}), .flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}),
} }