summaryrefslogtreecommitdiffstatshomepage
path: root/exercises
diff options
context:
space:
mode:
authorDave Gauer <dave@ratfactor.com>2021-02-28 13:51:33 -0500
committerDave Gauer <dave@ratfactor.com>2021-02-28 13:51:33 -0500
commit0552a62896696804957af77a94188dbc6eec2be0 (patch)
treec9523b67ec4b53470c1746d023e34c2bc6fc409a /exercises
parent5f7e9389d5a8d5829f9a7a7fe50153e3d82abf7f (diff)
Added quiz 6 (and the trumpeting sounds grow louder)
Diffstat (limited to 'exercises')
-rw-r--r--exercises/48_methods2.zig6
-rw-r--r--exercises/49_quiz6.zig91
2 files changed, 94 insertions, 3 deletions
diff --git a/exercises/48_methods2.zig b/exercises/48_methods2.zig
index b9477da..37b182c 100644
--- a/exercises/48_methods2.zig
+++ b/exercises/48_methods2.zig
@@ -23,9 +23,9 @@ const Elephant = struct {
}
pub fn print(self: *Elephant) void {
- // Prints elephant letter and (V)isited or (U)nvisited.
- var v: u8 = if (self.visited) 'V' else 'U';
- std.debug.print("Elephant {u} ({u}). ", .{ self.letter, v});
+ // Prints elephant letter and [v]isited
+ var v: u8 = if (self.visited) 'v' else ' ';
+ std.debug.print("{u}{u} ", .{ self.letter, v });
}
};
diff --git a/exercises/49_quiz6.zig b/exercises/49_quiz6.zig
new file mode 100644
index 0000000..a1a1dec
--- /dev/null
+++ b/exercises/49_quiz6.zig
@@ -0,0 +1,91 @@
+//
+// "Trunks and tails
+// Are handy things"
+
+// from Holding Hands
+// by Lenore M. Link
+//
+// Now that we have tails all figured out, can you implement trunks?
+//
+const std = @import("std");
+
+const Elephant = struct {
+ letter: u8,
+ tail: ?*Elephant = null,
+ trunk: ?*Elephant = null,
+ visited: bool = false,
+
+ // Elephant tail methods!
+ pub fn getTail(self: *Elephant) *Elephant {
+ return self.tail.?; // Remember, this is means "orelse unreachable"
+ }
+
+ pub fn hasTail(self: *Elephant) bool {
+ return (self.tail != null);
+ }
+
+ // Your Elephant trunk methods go here!
+ // ---------------------------------------------------
+
+ ???
+
+ // ---------------------------------------------------
+
+ pub fn visit(self: *Elephant) void {
+ self.visited = true;
+ }
+
+ pub fn print(self: *Elephant) void {
+ // Prints elephant letter and [v]isited
+ var v: u8 = if (self.visited) 'v' else ' ';
+ std.debug.print("{u}{u} ", .{ self.letter, v });
+ }
+};
+
+pub fn main() void {
+ var elephantA = Elephant{ .letter = 'A' };
+ var elephantB = Elephant{ .letter = 'B' };
+ var elephantC = Elephant{ .letter = 'C' };
+
+ // Link the elephants so that each tail "points" to the next.
+ elephantA.tail = &elephantB;
+ elephantB.tail = &elephantC;
+
+ // And link the elephants so that each trunk "points" to the previous.
+ elephantB.trunk = &elephantA;
+ elephantC.trunk = &elephantB;
+
+ visitElephants(&elephantA);
+
+ std.debug.print("\n", .{});
+}
+
+// This function visits all elephants twice, tails to trunks.
+fn visitElephants(first_elephant: *Elephant) void {
+ var e = first_elephant;
+
+ // Follow the tails!
+ while (true) {
+ e.print();
+ e.visit();
+
+ // Get the next elephant or stop.
+ if (e.hasTail()) {
+ e = e.getTail();
+ } else {
+ break;
+ }
+ }
+
+ // Follow the trunks!
+ while (true) {
+ e.print();
+
+ // Get the previous elephant or stop.
+ if (e.hasTrunk()) {
+ e = e.getTrunk();
+ } else {
+ break;
+ }
+ }
+}