diff options
author | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
---|---|---|
committer | Dave Gauer <dave@ratfactor.com> | 2021-03-12 18:59:46 -0500 |
commit | 6ad9774189fbd64b2f2c9519f4513ab34b0c3809 (patch) | |
tree | d6c90700131d5b28e898881f13e2a05612e4703f /exercises/49_quiz6.zig | |
parent | be36352572ddb18218e1830e49316c259dea5e8c (diff) | |
download | ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.gz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.bz2 ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.tar.xz ziglings-6ad9774189fbd64b2f2c9519f4513ab34b0c3809.zip |
"999 is enough for anybody" triple-zero padding (#18)
When I hit 999 exercises, I will finally have reached the ultimate
state of soteriological release and no more exercises will be needed.
The cycle will be complete. All that will be left is perfect quietude,
freedom, and highest happiness.
Diffstat (limited to 'exercises/49_quiz6.zig')
-rw-r--r-- | exercises/49_quiz6.zig | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/exercises/49_quiz6.zig b/exercises/49_quiz6.zig deleted file mode 100644 index a1a1dec..0000000 --- a/exercises/49_quiz6.zig +++ /dev/null @@ -1,91 +0,0 @@ -// -// "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; - } - } -} |