Compare commits

...

2 commits

Author SHA1 Message Date
4192a7fea4
106-107 completed 2024-06-21 09:21:03 -04:00
8a6106f82b
099-105 completed 2024-06-21 08:49:10 -04:00
9 changed files with 19 additions and 19 deletions

View file

@ -131,7 +131,7 @@ pub fn main() !void {
for (0..size) |b| { for (0..size) |b| {
// What formatting is needed here to make our columns // What formatting is needed here to make our columns
// nice and straight? // nice and straight?
print("{???} ", .{(a + 1) * (b + 1)}); print("{d:>3} ", .{(a + 1) * (b + 1)});
} }
// After each row we use double line feed: // After each row we use double line feed:

View file

@ -39,7 +39,7 @@ pub fn main() void {
const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 }; const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 };
const dec_nums = [_]u8{ 11, 42, 119 }; const dec_nums = [_]u8{ 11, 42, 119 };
for (hex_nums, ???) |hn, ???| { for (hex_nums, dec_nums) |hn, dn| {
if (hn != dn) { if (hn != dn) {
std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn }); std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
return; return;

View file

@ -51,7 +51,7 @@ pub fn main() void {
// We would like to number our list starting with 1, not 0. // We would like to number our list starting with 1, not 0.
// How do we do that? // How do we do that?
for (roles, gold, experience, ???) |c, g, e, i| { for (roles, gold, experience, 1..) |c, g, e, i| {
const role_name = switch (c) { const role_name = switch (c) {
.wizard => "Wizard", .wizard => "Wizard",
.thief => "Thief", .thief => "Thief",

View file

@ -83,7 +83,7 @@ fn sub(a: f16, b: f16) f16 {
// an error that you need // an error that you need
// to correct. // to correct.
test "sub" { test "sub" {
try testing.expect(sub(10, 5) == 6); try testing.expect(sub(10, 5) == 5);
try testing.expect(sub(3, 1.5) == 1.5); try testing.expect(sub(3, 1.5) == 1.5);
} }
@ -108,5 +108,5 @@ test "divide" {
// Now we test if the function returns an error // Now we test if the function returns an error
// if we pass a zero as denominator. // if we pass a zero as denominator.
// But which error needs to be tested? // But which error needs to be tested?
try testing.expectError(error.???, divide(15, 0)); try testing.expectError(error.DivisionByZero, divide(15, 0));
} }

View file

@ -119,9 +119,9 @@
// after all we need some practice. Suppose we want to count the words // after all we need some practice. Suppose we want to count the words
// of this little poem: // of this little poem:
// //
// My name is Ozymandias, King of Kings; // My name is Ozymandias, King of Kings;
// Look on my Works, ye Mighty, and despair! // Look on my Works, ye Mighty, and despair!
// by Percy Bysshe Shelley // by Percy Bysshe Shelley
// //
// //
const std = @import("std"); const std = @import("std");
@ -136,7 +136,7 @@ pub fn main() !void {
; ;
// now the tokenizer, but what do we need here? // now the tokenizer, but what do we need here?
var it = std.mem.tokenizeAny(u8, poem, ???); var it = std.mem.tokenizeAny(u8, poem, " ,;!\n");
// print all words and count them // print all words and count them
var cnt: usize = 0; var cnt: usize = 0;

View file

@ -97,12 +97,12 @@ pub fn main() !void {
defer handle.join(); defer handle.join();
// Second thread // Second thread
const handle2 = try std.Thread.spawn(.{}, thread_function, .{-4}); // that can't be right? const handle2 = try std.Thread.spawn(.{}, thread_function, .{2}); // that can't be right?
defer handle2.join(); defer handle2.join();
// Third thread // Third thread
const handle3 = try std.Thread.spawn(.{}, thread_function, .{3}); const handle3 = try std.Thread.spawn(.{}, thread_function, .{3});
defer ??? // <-- something is missing defer handle3.join(); // <-- something is missing
// After the threads have been started, // After the threads have been started,
// they run in parallel and we can still do some work in between. // they run in parallel and we can still do some work in between.

View file

@ -81,8 +81,8 @@ pub fn main() !void {
defer handle1.join(); defer handle1.join();
// Second thread to calculate the minus numbers. // Second thread to calculate the minus numbers.
??? const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
defer handle2.join();
} }
// Here we add up the results. // Here we add up the results.
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus}); std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});

View file

@ -35,7 +35,7 @@ pub fn main() !void {
// by doing nothing // by doing nothing
// //
// we want to catch error.PathAlreadyExists and do nothing // we want to catch error.PathAlreadyExists and do nothing
??? => {}, error.PathAlreadyExists => {},
// if there's any other unexpected error we just propagate it through // if there's any other unexpected error we just propagate it through
else => return e, else => return e,
}; };
@ -44,7 +44,7 @@ pub fn main() !void {
// wait a minute... // wait a minute...
// opening a directory might fail! // opening a directory might fail!
// what should we do here? // what should we do here?
var output_dir: std.fs.Dir = cwd.openDir("output", .{}); var output_dir: std.fs.Dir = try cwd.openDir("output", .{});
defer output_dir.close(); defer output_dir.close();
// we try to open the file `zigling.txt`, // we try to open the file `zigling.txt`,
@ -55,7 +55,7 @@ pub fn main() !void {
// but here we are not yet done writing to the file // but here we are not yet done writing to the file
// if only there were a keyword in Zig that // if only there were a keyword in Zig that
// allowed you to "defer" code execution to the end of the scope... // allowed you to "defer" code execution to the end of the scope...
file.close(); defer file.close();
// you are not allowed to move these two lines above the file closing line! // you are not allowed to move these two lines above the file closing line!
const byte_written = try file.write("It's zigling time!"); const byte_written = try file.write("It's zigling time!");

View file

@ -33,7 +33,7 @@ pub fn main() !void {
// initalize an array of u8 with all letter 'A' // initalize an array of u8 with all letter 'A'
// we need to pick the size of the array, 64 seems like a good number // we need to pick the size of the array, 64 seems like a good number
// fix the initalization below // fix the initalization below
var content = ['A']*64; var content = [_]u8{'A'} ** 64;
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
std.debug.print("{s}\n", .{content}); std.debug.print("{s}\n", .{content});
@ -41,12 +41,12 @@ pub fn main() !void {
// can you go here to find a way to read the content? // can you go here to find a way to read the content?
// https://ziglang.org/documentation/master/std/#std.fs.File // https://ziglang.org/documentation/master/std/#std.fs.File
// hint: you might find two answers that are both vaild in this case // hint: you might find two answers that are both vaild in this case
const bytes_read = zig_read_the_file_or_i_will_fight_you(&content); const bytes_read = try file.readAll(&content);
// Woah, too screamy. I know you're excited for zigling time but tone it down a bit. // Woah, too screamy. I know you're excited for zigling time but tone it down a bit.
// Can you print only what we read from the file? // Can you print only what we read from the file?
std.debug.print("Successfully Read {d} bytes: {s}\n", .{ std.debug.print("Successfully Read {d} bytes: {s}\n", .{
bytes_read, bytes_read,
content, // change this line only content[0..bytes_read], // change this line only
}); });
} }