Compare commits

..

No commits in common. "b22b03d9f6dc689f498a52d218827a9c84273c67" and "7108f89eb3f7351cc3a76801f390b59c9c016c81" have entirely different histories.

17 changed files with 67 additions and 67 deletions

View file

@ -1128,7 +1128,7 @@ const exercises = [_]Exercise{
.main_file = "107_files2.zig", .main_file = "107_files2.zig",
.output = .output =
\\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA \\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
\\Successfully Read 18 bytes: It's zigling time! \\Successfully Read 18 byte: It's zigling time!
, ,
}, },
.{ .{

View file

@ -51,7 +51,6 @@ fn visitElephants(first_elephant: *Elephant) void {
// We should stop once we encounter a tail that // We should stop once we encounter a tail that
// does NOT point to another element. What can // does NOT point to another element. What can
// we put here to make that happen? // we put here to make that happen?
if (e.tail == null) break; if (e.tail == null) break;
e = e.tail.?; e = e.tail.?;

View file

@ -20,9 +20,9 @@ const print = @import("std").debug.print;
pub fn main() void { pub fn main() void {
const zig = [_]u8{ const zig = [_]u8{
0o132, // octal 0o131, // octal
0b1101001, // binary 0b1101000, // binary
0x67, // hex 0x66, // hex
}; };
print("{s} is cool.\n", .{zig}); print("{s} is cool.\n", .{zig});

View file

@ -43,7 +43,7 @@ pub fn main() void {
// //
// We'll convert this weight from pound to kilograms at a // We'll convert this weight from pound to kilograms at a
// conversion of 0.453592kg to the pound. // conversion of 0.453592kg to the pound.
const shuttle_weight: f32 = 0.453592 * 4480e+3; const shuttle_weight: f16 = 0.453592 * 4480e6;
// By default, float values are formatted in scientific // By default, float values are formatted in scientific
// notation. Try experimenting with '{d}' and '{d:.3}' to see // notation. Try experimenting with '{d}' and '{d:.3}' to see

View file

@ -67,7 +67,7 @@ const print = @import("std").debug.print;
pub fn main() void { pub fn main() void {
var letter: u8 = 'A'; var letter: u8 = 'A';
const my_letter: ?*[1]u8 = &letter; const my_letter: ??? = &letter;
// ^^^^^^^ // ^^^^^^^
// Your type here. // Your type here.
// Must coerce from &letter (which is a *u8). // Must coerce from &letter (which is a *u8).

View file

@ -13,7 +13,7 @@
// How could we do that? A good method is to use the modulo function. // How could we do that? A good method is to use the modulo function.
// But if we write "765.2 % 360", it only works with float values // But if we write "765.2 % 360", it only works with float values
// that are known at compile time. // that are known at compile time.
// In Zig, we would use @mod(a, b) instead. // In Zig, we would use %mod(a, b) instead.
// //
// Let us now assume that we cannot do this in Zig, but only with // Let us now assume that we cannot do this in Zig, but only with
// a C function from the standard library. In the library "math", // a C function from the standard library. In the library "math",

View file

@ -30,9 +30,9 @@
// std.debug.print("slice_ptr={*}\n", .{slice_ptr}); // std.debug.print("slice_ptr={*}\n", .{slice_ptr});
// } // }
// Instead of a simple integer or a slice with a constant size, // Instead of a simple integer or a constant sized slice, this
// this program requires allocating a slice that is the same size // program requires a slice to be allocated that is the same size as
// as an input array. // an input array.
// Given a series of numbers, take the running average. In other // Given a series of numbers, take the running average. In other
// words, each item N should contain the average of the last N // words, each item N should contain the average of the last N

View file

@ -1,5 +1,5 @@
// //
// Bit manipulation is a very powerful tool, also from Zig. // Bit manipulations is a very powerful tool just also from Zig.
// Since the dawn of the computer age, numerous algorithms have been // Since the dawn of the computer age, numerous algorithms have been
// developed that solve tasks solely by moving, setting, or logically // developed that solve tasks solely by moving, setting, or logically
// combining bits. // combining bits.
@ -8,10 +8,10 @@
// functions where possible. And it is often possible with calculations // functions where possible. And it is often possible with calculations
// based on integers. // based on integers.
// //
// At first glance, it is often not easy to understand what exactly these // Often it is not easy to understand at first glance what exactly these
// algorithms do when only "numbers" in memory areas change outwardly. // algorithms do when only "numbers" in memory areas change outwardly.
// However, it should never be forgotten that the numbers only represent // But it must never be forgotten that the numbers only represent the
// the interpretation of the bit sequences. // interpretation of the bit sequences.
// //
// Quasi the reversed case we have otherwise, namely that we represent // Quasi the reversed case we have otherwise, namely that we represent
// numbers in bit sequences. // numbers in bit sequences.
@ -21,7 +21,7 @@
// Zig provides all the necessary functions to change the bits inside // Zig provides all the necessary functions to change the bits inside
// a variable. It is distinguished whether the bit change leads to an // a variable. It is distinguished whether the bit change leads to an
// overflow or not. The details are in the Zig documentation in section // overflow or not. The details are in the Zig documentation in section
// "Table of Operators". // 10.1 "Table of Operators".
// //
// Here are some examples of how the bits of variables can be changed: // Here are some examples of how the bits of variables can be changed:
// //

View file

@ -1,5 +1,5 @@
// //
// Another useful application for bit manipulation is setting bits as flags. // Another useful practice for bit manipulation is setting bits as flags.
// This is especially useful when processing lists of something and storing // This is especially useful when processing lists of something and storing
// the states of the entries, e.g. a list of numbers and for each prime // the states of the entries, e.g. a list of numbers and for each prime
// number a flag is set. // number a flag is set.
@ -19,9 +19,9 @@
// For example, you could take an array of bool and set the value to 'true' // For example, you could take an array of bool and set the value to 'true'
// for each letter in the order of the alphabet (a=0; b=1; etc.) found in // for each letter in the order of the alphabet (a=0; b=1; etc.) found in
// the sentence. However, this is neither memory efficient nor particularly // the sentence. However, this is neither memory efficient nor particularly
// fast. Instead we choose a simpler approach that is very similar in principle: // fast. Instead we take a simpler way, very similar in principle, we define
// We define a variable with at least 26 bits (e.g. u32) and set the bit for // a variable with at least 26 bits (e.g. u32) and also set the bit for each
// each letter that is found in the corresponding position. // letter found at the corresponding position.
// //
// Zig provides functions for this in the standard library, but we prefer to // Zig provides functions for this in the standard library, but we prefer to
// solve it without these extras, after all we want to learn something. // solve it without these extras, after all we want to learn something.
@ -39,7 +39,7 @@ fn isPangram(str: []const u8) bool {
// first we check if the string has at least 26 characters // first we check if the string has at least 26 characters
if (str.len < 26) return false; if (str.len < 26) return false;
// we use a 32 bit variable of which we need 26 bits // we uses a 32 bit variable of which we need 26 bits
var bits: u32 = 0; var bits: u32 = 0;
// loop about all characters in the string // loop about all characters in the string

View file

@ -19,10 +19,10 @@
// https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L29 // https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L29
// //
// Zig already has a very nice selection of formatting options. // Zig already has a very nice selection of formatting options.
// These can be used in different ways, but generally to convert // These can be used in different ways, but typically to convert
// numerical values into various text representations. The results // numerical values into various text representations. The
// can be used for direct output to a terminal or stored for // results can be used for direct output to a terminal or stored
// later use or written to a file. The latter is useful when // for later use or written to a file. The latter is useful when
// large amounts of data are to be processed by other programs. // large amounts of data are to be processed by other programs.
// //
// In Ziglings, we are concerned with the output to the console. // In Ziglings, we are concerned with the output to the console.

View file

@ -4,8 +4,8 @@
// one possibility, namely asynchronous processes, in Exercises 84-91. // one possibility, namely asynchronous processes, in Exercises 84-91.
// //
// However, the computing power of the processor is only distributed to // However, the computing power of the processor is only distributed to
// the started and running tasks, which always reaches its limits when // the started tasks, which always reaches its limits when pure computing
// pure computing power is called up. // power is called up.
// //
// For example, in blockchains based on proof of work, the miners have // For example, in blockchains based on proof of work, the miners have
// to find a nonce for a certain character string so that the first m bits // to find a nonce for a certain character string so that the first m bits

View file

@ -1,6 +1,6 @@
// //
// Now that we are familiar with the principles of multi-threading, // Now that we are familiar with the principles of multi threading, we
// let's boldly venture into a practical example from mathematics. // boldly venture into a practical example from mathematics.
// We will determine the circle number PI with sufficient accuracy. // We will determine the circle number PI with sufficient accuracy.
// //
// There are different methods for this, and some of them are several // There are different methods for this, and some of them are several
@ -104,4 +104,4 @@ fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
// to such an extent that seconds become minutes during execution. // to such an extent that seconds become minutes during execution.
// //
// And you should remove the formatting restriction in "print", // And you should remove the formatting restriction in "print",
// otherwise you will not be able to see the additional digits. // otherwise you will not be able to see the additional diggits.

View file

@ -50,20 +50,20 @@ pub fn main() !void {
// we try to open the file `zigling.txt`, // we try to open the file `zigling.txt`,
// and propagate the error up if there are any errors // and propagate the error up if there are any errors
const file: std.fs.File = try output_dir.createFile("zigling.txt", .{}); const file: std.fs.File = try output_dir.createFile("zigling.txt", .{});
// it is a good habit to close a file after you are done with it // it is a good habit to close a file after you are done with
// so that other programs can read it and prevent data corruption // so that other program can read it and prevent data corruption
// 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 are a keyword in zig that
// allows you "defer" code execute to the end of scope... // allow you "defer" code execute to the end of scope...
file.close(); file.close();
// !you are not allowed to switch these two lines above the file closing line! // !you are not allow to switch this two lines to before file closing line!
const byte_written = try file.write("It's zigling time!"); const byte_written = try file.write("It's zigling time!");
std.debug.print("Successfully wrote {d} bytes.\n", .{byte_written}); std.debug.print("Successfully wrote {d} bytes.\n", .{byte_written});
} }
// to check if you actually write to the file, you can either, // to check if you actually write to the file, you can either,
// 1. open the file on your text editor, or // 1. open the file on your text editor, or
// 2. print the content of the file in the console with the following command // 2. print the content of the file in the console with command
// >> cat ./output/zigling.txt // >> cat ./output/zigling.txt
// //
// //
@ -89,4 +89,4 @@ pub fn main() !void {
// - go to documentation of the struct `std.fs.Dir` here // - go to documentation of the struct `std.fs.Dir` here
// https://ziglang.org/documentation/master/std/#std.fs.Dir // https://ziglang.org/documentation/master/std/#std.fs.Dir
// - can you find a function for opening a file? how about deleting a file? // - can you find a function for opening a file? how about deleting a file?
// - what kind of options can you use with those functions? // - what kind of option can you uses with those function?

View file

@ -5,15 +5,15 @@
// with content `It's zigling time!`(18 byte total) // with content `It's zigling time!`(18 byte total)
// //
// Now there no point in writing to a file if we don't read from it am I right? // Now there no point in writing to a file if we don't read from it am I right?
// let's write a program to read the content of the file that we just created. // let's wrote a program to read the content of the file that we just created.
// //
// I am assuming that you've created the appropriate files for this to work. // I am assuming you've created the appropriate files for this to work.
// //
// Alright, bud, lean in close here's the game plan. // Alright, bud, lean in close here's the game plan.
// - First, we open the {project_root}/output/ directory // - First, we open the {project_root}/output/ directory
// - Secondly, we open file `zigling.txt` in that directory // - Secondly, we open file `zigling.txt` in that directory
// - then, we initalize an array of characters with all letter 'A', and print it // - then, we initalize an array of character with all letter 'A', and print it
// - After that, we read the content of the file to the array // - Afte that, we read the content of the file to the array
// - Finally, we print out the read content // - Finally, we print out the read content
const std = @import("std"); const std = @import("std");
@ -31,22 +31,22 @@ pub fn main() !void {
defer file.close(); defer file.close();
// 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 a size of the array, 64 seems like a good number.
// fix the initalization below // fix the initalization below
var content = ['A']*64; var content = ['A']*64;
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
std.debug.print("{s}\n", .{content}); std.debug.print("{s}\n", .{content});
// okay, seems like a threat of violence is not the answer in this case // okay, seem like threat of violence is not the answer in this case
// 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 answer that are both vaild in this case
const bytes_read = zig_read_the_file_or_i_will_fight_you(&content); const byte_read = zig_read_the_file_or_i_will_fight_you(&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} byte: {s}\n", .{
bytes_read, byte_read,
content, // change this line only content, // change this line only
}); });
} }

View file

@ -1,5 +1,5 @@
--- exercises/046_optionals2.zig 2024-05-10 23:11:25.796632478 +0200 --- exercises/046_optionals2.zig 2023-10-03 22:15:22.122241138 +0200
+++ answers/046_optionals2.zig 2024-05-10 23:10:16.115335668 +0200 +++ answers/046_optionals2.zig 2023-10-05 20:04:07.049433424 +0200
@@ -21,7 +21,7 @@ @@ -21,7 +21,7 @@
const Elephant = struct { const Elephant = struct {
@ -9,11 +9,12 @@
visited: bool = false, visited: bool = false,
}; };
@@ -51,6 +51,6 @@ @@ -51,7 +51,7 @@
// We should stop once we encounter a tail that // We should stop once we encounter a tail that
// does NOT point to another element. What can // does NOT point to another element. What can
// we put here to make that happen? // we put here to make that happen?
- e = e.tail ??? - if (e.tail == null) ???;
+ e = e.tail orelse break; + if (e.tail == null) break;
}
e = e.tail.?;
} }

View file

@ -1,5 +1,5 @@
--- exercises/106_files.zig 2024-05-05 00:48:25.808548611 +0200 --- exercises/106_files.zig 2024-03-27 16:52:05.660910200 +0800
+++ answers/106_files.zig 2024-05-05 01:00:40.742969819 +0200 +++ answers/106_files.zig 2024-03-27 16:52:09.649422200 +0800
@@ -35,7 +35,7 @@ @@ -35,7 +35,7 @@
// by doing nothing // by doing nothing
// //
@ -20,10 +20,10 @@
// we try to open the file `zigling.txt`, // we try to open the file `zigling.txt`,
@@ -55,7 +55,7 @@ @@ -55,7 +55,7 @@
// 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 are a keyword in zig that
// allows you "defer" code execute to the end of scope... // allow you "defer" code execute to the end of scope...
- file.close(); - file.close();
+ defer file.close(); + defer file.close();
// !you are not allowed to switch these two lines above the file closing line! // !you are not allow to switch this two lines to before 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

@ -1,8 +1,8 @@
--- exercises/107_files2.zig 2024-05-05 00:48:25.808548611 +0200 --- exercises/107_files2.zig 2024-03-27 16:51:56.199719600 +0800
+++ answers/107_files2.zig 2024-05-05 01:14:03.866062288 +0200 +++ answers/107_files2.zig 2024-03-27 16:52:01.650935300 +0800
@@ -33,7 +33,7 @@ @@ -33,7 +33,7 @@
// 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 a size of the array, 64 seems like a good number.
// fix the initalization below // fix the initalization below
- var content = ['A']*64; - var content = ['A']*64;
+ var content = [_]u8{'A'} ** 64; + var content = [_]u8{'A'} ** 64;
@ -12,15 +12,15 @@
@@ -41,12 +41,12 @@ @@ -41,12 +41,12 @@
// 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 answer that are both vaild in this case
- const bytes_read = zig_read_the_file_or_i_will_fight_you(&content); - const byte_read = zig_read_the_file_or_i_will_fight_you(&content);
+ const bytes_read = try file.read(&content); + const byte_read = try file.read(&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} byte: {s}\n", .{
bytes_read, byte_read,
- content, // change this line only - content, // change this line only
+ content[0..bytes_read], // change this line only + content[0..byte_read], // change this line only
}); });
} }