47-60
This commit is contained in:
parent
68bb75644b
commit
28dc209134
14 changed files with 41 additions and 35 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -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.?});
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
|
|
|
|||
|
|
@ -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", .{});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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}),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
//
|
||||
const std = @import("std");
|
||||
|
||||
const Insect = union(InsectStat) {
|
||||
const Insect = union(enum) {
|
||||
flowers_visited: u16,
|
||||
still_alive: bool,
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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});
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue