mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-11-09 11:40:46 -05:00
Compare commits
2 commits
e06cf45604
...
4192a7fea4
Author | SHA1 | Date | |
---|---|---|---|
4192a7fea4 | |||
8a6106f82b |
9 changed files with 19 additions and 19 deletions
|
@ -131,7 +131,7 @@ pub fn main() !void {
|
|||
for (0..size) |b| {
|
||||
// What formatting is needed here to make our columns
|
||||
// nice and straight?
|
||||
print("{???} ", .{(a + 1) * (b + 1)});
|
||||
print("{d:>3} ", .{(a + 1) * (b + 1)});
|
||||
}
|
||||
|
||||
// After each row we use double line feed:
|
||||
|
|
|
@ -39,7 +39,7 @@ pub fn main() void {
|
|||
const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 };
|
||||
const dec_nums = [_]u8{ 11, 42, 119 };
|
||||
|
||||
for (hex_nums, ???) |hn, ???| {
|
||||
for (hex_nums, dec_nums) |hn, dn| {
|
||||
if (hn != dn) {
|
||||
std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
|
||||
return;
|
||||
|
|
|
@ -51,7 +51,7 @@ pub fn main() void {
|
|||
|
||||
// We would like to number our list starting with 1, not 0.
|
||||
// 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) {
|
||||
.wizard => "Wizard",
|
||||
.thief => "Thief",
|
||||
|
|
|
@ -83,7 +83,7 @@ fn sub(a: f16, b: f16) f16 {
|
|||
// an error that you need
|
||||
// to correct.
|
||||
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);
|
||||
}
|
||||
|
@ -108,5 +108,5 @@ test "divide" {
|
|||
// Now we test if the function returns an error
|
||||
// if we pass a zero as denominator.
|
||||
// But which error needs to be tested?
|
||||
try testing.expectError(error.???, divide(15, 0));
|
||||
try testing.expectError(error.DivisionByZero, divide(15, 0));
|
||||
}
|
||||
|
|
|
@ -136,7 +136,7 @@ pub fn main() !void {
|
|||
;
|
||||
|
||||
// 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
|
||||
var cnt: usize = 0;
|
||||
|
|
|
@ -97,12 +97,12 @@ pub fn main() !void {
|
|||
defer handle.join();
|
||||
|
||||
// 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();
|
||||
|
||||
// Third thread
|
||||
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,
|
||||
// they run in parallel and we can still do some work in between.
|
||||
|
|
|
@ -81,8 +81,8 @@ pub fn main() !void {
|
|||
defer handle1.join();
|
||||
|
||||
// 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.
|
||||
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
|
||||
|
|
|
@ -35,7 +35,7 @@ pub fn main() !void {
|
|||
// by doing nothing
|
||||
//
|
||||
// we want to catch error.PathAlreadyExists and do nothing
|
||||
??? => {},
|
||||
error.PathAlreadyExists => {},
|
||||
// if there's any other unexpected error we just propagate it through
|
||||
else => return e,
|
||||
};
|
||||
|
@ -44,7 +44,7 @@ pub fn main() !void {
|
|||
// wait a minute...
|
||||
// opening a directory might fail!
|
||||
// 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();
|
||||
|
||||
// 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
|
||||
// if only there were a keyword in Zig that
|
||||
// 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!
|
||||
const byte_written = try file.write("It's zigling time!");
|
||||
|
|
|
@ -33,7 +33,7 @@ pub fn main() !void {
|
|||
// initalize an array of u8 with all letter 'A'
|
||||
// we need to pick the size of the array, 64 seems like a good number
|
||||
// fix the initalization below
|
||||
var content = ['A']*64;
|
||||
var content = [_]u8{'A'} ** 64;
|
||||
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
|
||||
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?
|
||||
// https://ziglang.org/documentation/master/std/#std.fs.File
|
||||
// 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.
|
||||
// Can you print only what we read from the file?
|
||||
std.debug.print("Successfully Read {d} bytes: {s}\n", .{
|
||||
bytes_read,
|
||||
content, // change this line only
|
||||
content[0..bytes_read], // change this line only
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue