Compare commits
25 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
e8270de928 | ||
![]() |
776316e60b | ||
![]() |
efe2d19c69 | ||
![]() |
49c54fb075 | ||
![]() |
67f87a76c2 | ||
![]() |
dfdaf03d99 | ||
![]() |
5da194ba9c | ||
![]() |
8345e839b0 | ||
![]() |
6c23f2682e | ||
![]() |
19bd8745e4 | ||
![]() |
1ac46d7a42 | ||
![]() |
165cc199ca | ||
![]() |
e182d1f19d | ||
![]() |
09e2f37a50 | ||
![]() |
27db3112f9 | ||
![]() |
8cb2a5aa3c | ||
![]() |
c936c5e123 | ||
![]() |
5c2354a1bf | ||
![]() |
4dbd056100 | ||
![]() |
9ce4a7d6f0 | ||
![]() |
b484be9ac1 | ||
![]() |
a2a0a6e891 | ||
![]() |
e7eaa080b6 | ||
![]() |
ce0a6cc91c | ||
![]() |
c94eb33e44 |
53 changed files with 163 additions and 129 deletions
31
.devcontainer/devcontainer.json
Normal file
31
.devcontainer/devcontainer.json
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
|
||||||
|
// README at: https://github.com/devcontainers/templates/tree/main/src/debian
|
||||||
|
{
|
||||||
|
"name": "Ziglings",
|
||||||
|
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile
|
||||||
|
"image": "mcr.microsoft.com/devcontainers/base:bullseye",
|
||||||
|
"features": {
|
||||||
|
"ghcr.io/devcontainers-contrib/features/zig:1": {
|
||||||
|
"version": "master"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"customizations": {
|
||||||
|
"vscode": {
|
||||||
|
"extensions": [
|
||||||
|
"ziglang.vscode-zig"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Features to add to the dev container. More info: https://containers.dev/features.
|
||||||
|
// "features": {},
|
||||||
|
|
||||||
|
// Use 'forwardPorts' to make a list of ports inside the container available locally.
|
||||||
|
// "forwardPorts": [],
|
||||||
|
|
||||||
|
// Configure tool-specific properties.
|
||||||
|
// "customizations": {},
|
||||||
|
|
||||||
|
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
|
||||||
|
// "remoteUser": "root"
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
steps:
|
steps:
|
||||||
- name: eowyn
|
- name: eowyn
|
||||||
image: ziglings/ziglang
|
image: ziglings/ziglang:latest
|
||||||
|
pull: true
|
||||||
commands:
|
commands:
|
||||||
- sh ./patches/eowyn.sh
|
- sh ./patches/eowyn.sh
|
||||||
when:
|
when:
|
||||||
|
|
|
@ -46,7 +46,7 @@ Verify the installation and build number of `zig` like so:
|
||||||
|
|
||||||
```
|
```
|
||||||
$ zig version
|
$ zig version
|
||||||
0.12.0-dev.xxxx+xxxxxxxxx
|
0.13.0-dev.xxxx+xxxxxxxxx
|
||||||
```
|
```
|
||||||
|
|
||||||
Clone this repository with Git:
|
Clone this repository with Git:
|
||||||
|
@ -74,8 +74,8 @@ the appropriate tag.
|
||||||
The Zig language is under very active development. In order to be
|
The Zig language is under very active development. In order to be
|
||||||
current, Ziglings tracks **development** builds of the Zig
|
current, Ziglings tracks **development** builds of the Zig
|
||||||
compiler rather than versioned **release** builds. The last
|
compiler rather than versioned **release** builds. The last
|
||||||
stable release was `0.11.0`, but Ziglings needs a dev build with
|
stable release was `0.12.0`, but Ziglings needs a dev build with
|
||||||
pre-release version "0.12.0" and a build number at least as high
|
pre-release version "0.13.0" and a build number at least as high
|
||||||
as that shown in the example version check above.
|
as that shown in the example version check above.
|
||||||
|
|
||||||
It is likely that you'll download a build which is _greater_ than
|
It is likely that you'll download a build which is _greater_ than
|
||||||
|
|
|
@ -1128,7 +1128,7 @@ const exercises = [_]Exercise{
|
||||||
.main_file = "107_files2.zig",
|
.main_file = "107_files2.zig",
|
||||||
.output =
|
.output =
|
||||||
\\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
\\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
||||||
\\Successfully Read 18 byte: It's zigling time!
|
\\Successfully Read 18 bytes: It's zigling time!
|
||||||
,
|
,
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
|
|
|
@ -16,6 +16,6 @@
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
fn main() void {
|
pub fn main() void {
|
||||||
std.debug.print("Hello world!\n", .{});
|
std.debug.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
// Please complete the import below:
|
// Please complete the import below:
|
||||||
//
|
//
|
||||||
|
|
||||||
??? = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
std.debug.print("Standard Library.\n", .{});
|
std.debug.print("Standard Library.\n", .{});
|
||||||
|
|
|
@ -34,12 +34,12 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const n: u8 = 50;
|
var n: u8 = 50;
|
||||||
n = n + 5;
|
n = n + 5;
|
||||||
|
|
||||||
const pi: u8 = 314159;
|
const pi: u32 = 314159;
|
||||||
|
|
||||||
const negative_eleven: u8 = -11;
|
const negative_eleven: i8 = -11;
|
||||||
|
|
||||||
// There are no errors in the next line, just explanation:
|
// There are no errors in the next line, just explanation:
|
||||||
// Perhaps you noticed before that the print function takes two
|
// Perhaps you noticed before that the print function takes two
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub fn main() void {
|
||||||
// (Problem 1)
|
// (Problem 1)
|
||||||
// This "const" is going to cause a problem later - can you see what it is?
|
// This "const" is going to cause a problem later - can you see what it is?
|
||||||
// How do we fix it?
|
// How do we fix it?
|
||||||
const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
||||||
|
|
||||||
// Individual values can be set with '[]' notation.
|
// Individual values can be set with '[]' notation.
|
||||||
// Example: This line changes the first prime to 2 (which is correct):
|
// Example: This line changes the first prime to 2 (which is correct):
|
||||||
|
@ -40,11 +40,11 @@ pub fn main() void {
|
||||||
// (Problem 2)
|
// (Problem 2)
|
||||||
// Looks like we need to complete this expression. Use the example
|
// Looks like we need to complete this expression. Use the example
|
||||||
// above to set "fourth" to the fourth element of the some_primes array:
|
// above to set "fourth" to the fourth element of the some_primes array:
|
||||||
const fourth = some_primes[???];
|
const fourth = some_primes[3];
|
||||||
|
|
||||||
// (Problem 3)
|
// (Problem 3)
|
||||||
// Use the len property to get the length of the array:
|
// Use the len property to get the length of the array:
|
||||||
const length = some_primes.???;
|
const length = some_primes.len;
|
||||||
|
|
||||||
std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
|
std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
|
||||||
first, fourth, length,
|
first, fourth, length,
|
||||||
|
|
|
@ -25,12 +25,12 @@ pub fn main() void {
|
||||||
// (Problem 1)
|
// (Problem 1)
|
||||||
// Please set this array concatenating the two arrays above.
|
// Please set this array concatenating the two arrays above.
|
||||||
// It should result in: 1 3 3 7
|
// It should result in: 1 3 3 7
|
||||||
const leet = ???;
|
const leet = le ++ et;
|
||||||
|
|
||||||
// (Problem 2)
|
// (Problem 2)
|
||||||
// Please set this array using repetition.
|
// Please set this array using repetition.
|
||||||
// It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
|
// It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
|
||||||
const bit_pattern = [_]u8{ ??? } ** 3;
|
const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3;
|
||||||
|
|
||||||
// Okay, that's all of the problems. Let's see the results.
|
// Okay, that's all of the problems. Let's see the results.
|
||||||
//
|
//
|
||||||
|
|
|
@ -24,18 +24,18 @@ pub fn main() void {
|
||||||
// (Problem 1)
|
// (Problem 1)
|
||||||
// Use array square bracket syntax to get the letter 'd' from
|
// Use array square bracket syntax to get the letter 'd' from
|
||||||
// the string "stardust" above.
|
// the string "stardust" above.
|
||||||
const d: u8 = ziggy[???];
|
const d: u8 = ziggy[4];
|
||||||
|
|
||||||
// (Problem 2)
|
// (Problem 2)
|
||||||
// Use the array repeat '**' operator to make "ha ha ha ".
|
// Use the array repeat '**' operator to make "ha ha ha ".
|
||||||
const laugh = "ha " ???;
|
const laugh = "ha " ** 3;
|
||||||
|
|
||||||
// (Problem 3)
|
// (Problem 3)
|
||||||
// Use the array concatenation '++' operator to make "Major Tom".
|
// Use the array concatenation '++' operator to make "Major Tom".
|
||||||
// (You'll need to add a space as well!)
|
// (You'll need to add a space as well!)
|
||||||
const major = "Major";
|
const major = "Major";
|
||||||
const tom = "Tom";
|
const tom = "Tom";
|
||||||
const major_tom = major ??? tom;
|
const major_tom = major ++ " " ++ tom;
|
||||||
|
|
||||||
// That's all the problems. Let's see our results:
|
// That's all the problems. Let's see our results:
|
||||||
std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom });
|
std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom });
|
||||||
|
|
|
@ -15,9 +15,9 @@ const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const lyrics =
|
const lyrics =
|
||||||
Ziggy played guitar
|
\\Ziggy played guitar
|
||||||
Jamming good with Andrew Kelley
|
\\Jamming good with Andrew Kelley
|
||||||
And the Spiders from Mars
|
\\And the Spiders from Mars
|
||||||
;
|
;
|
||||||
|
|
||||||
std.debug.print("{s}\n", .{lyrics});
|
std.debug.print("{s}\n", .{lyrics});
|
||||||
|
|
|
@ -19,7 +19,7 @@ pub fn main() void {
|
||||||
// the idiomatic type to use for array indexing.
|
// the idiomatic type to use for array indexing.
|
||||||
//
|
//
|
||||||
// There IS a problem on this line, but 'usize' isn't it.
|
// There IS a problem on this line, but 'usize' isn't it.
|
||||||
const x: usize = 1;
|
var x: usize = 1;
|
||||||
|
|
||||||
// Note: When you want to declare memory (an array in this
|
// Note: When you want to declare memory (an array in this
|
||||||
// case) without putting anything in it, you can set it to
|
// case) without putting anything in it, you can set it to
|
||||||
|
@ -33,10 +33,10 @@ pub fn main() void {
|
||||||
lang[0] = letters[x];
|
lang[0] = letters[x];
|
||||||
|
|
||||||
x = 3;
|
x = 3;
|
||||||
lang[???] = letters[x];
|
lang[1] = letters[x];
|
||||||
|
|
||||||
x = ???;
|
x = 5;
|
||||||
lang[2] = letters[???];
|
lang[2] = letters[x];
|
||||||
|
|
||||||
// We want to "Program in Zig!" of course:
|
// We want to "Program in Zig!" of course:
|
||||||
std.debug.print("Program in {s}!\n", .{lang});
|
std.debug.print("Program in {s}!\n", .{lang});
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub fn main() void {
|
||||||
const foo = 1;
|
const foo = 1;
|
||||||
|
|
||||||
// Please fix this condition:
|
// Please fix this condition:
|
||||||
if (foo) {
|
if (foo == 1) {
|
||||||
// We want our program to print this message!
|
// We want our program to print this message!
|
||||||
std.debug.print("Foo is 1!\n", .{});
|
std.debug.print("Foo is 1!\n", .{});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -10,7 +10,7 @@ pub fn main() void {
|
||||||
|
|
||||||
// Please use an if...else expression to set "price".
|
// Please use an if...else expression to set "price".
|
||||||
// If discount is true, the price should be $17, otherwise $20:
|
// If discount is true, the price should be $17, otherwise $20:
|
||||||
const price: u8 = if ???;
|
const price: u8 = if (discount) 17 else 20;
|
||||||
|
|
||||||
std.debug.print("With the discount, the price is ${}.\n", .{price});
|
std.debug.print("With the discount, the price is ${}.\n", .{price});
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ pub fn main() void {
|
||||||
var n: u32 = 2;
|
var n: u32 = 2;
|
||||||
|
|
||||||
// Please use a condition that is true UNTIL "n" reaches 1024:
|
// Please use a condition that is true UNTIL "n" reaches 1024:
|
||||||
while (???) {
|
while (n < 1024) {
|
||||||
// Print the current number
|
// Print the current number
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub fn main() void {
|
||||||
|
|
||||||
// Please set the continue expression so that we get the desired
|
// Please set the continue expression so that we get the desired
|
||||||
// results in the print statement below.
|
// results in the print statement below.
|
||||||
while (n < 1000) : ??? {
|
while (n < 1000) : (n *= 2) {
|
||||||
// Print the current number
|
// Print the current number
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,8 @@ pub fn main() void {
|
||||||
while (n <= 20) : (n += 1) {
|
while (n <= 20) : (n += 1) {
|
||||||
// The '%' symbol is the "modulo" operator and it
|
// The '%' symbol is the "modulo" operator and it
|
||||||
// returns the remainder after division.
|
// returns the remainder after division.
|
||||||
if (n % 3 == 0) ???;
|
if (n % 3 == 0) continue;
|
||||||
if (n % 5 == 0) ???;
|
if (n % 5 == 0) continue;
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub fn main() void {
|
||||||
// Oh dear! This while loop will go forever?!
|
// Oh dear! This while loop will go forever?!
|
||||||
// Please fix this so the print statement below gives the desired output.
|
// Please fix this so the print statement below gives the desired output.
|
||||||
while (true) : (n += 1) {
|
while (true) : (n += 1) {
|
||||||
if (???) ???;
|
if (n > 3) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result: we want n=4
|
// Result: we want n=4
|
||||||
|
|
|
@ -15,7 +15,7 @@ pub fn main() void {
|
||||||
|
|
||||||
std.debug.print("A Dramatic Story: ", .{});
|
std.debug.print("A Dramatic Story: ", .{});
|
||||||
|
|
||||||
for (???) |???| {
|
for (story) |scene| {
|
||||||
if (scene == 'h') std.debug.print(":-) ", .{});
|
if (scene == 'h') std.debug.print(":-) ", .{});
|
||||||
if (scene == 's') std.debug.print(":-( ", .{});
|
if (scene == 's') std.debug.print(":-( ", .{});
|
||||||
if (scene == 'n') std.debug.print(":-| ", .{});
|
if (scene == 'n') std.debug.print(":-| ", .{});
|
||||||
|
|
|
@ -25,7 +25,7 @@ pub fn main() void {
|
||||||
// the value of the place as a power of two for each bit.
|
// the value of the place as a power of two for each bit.
|
||||||
//
|
//
|
||||||
// See if you can figure out the missing pieces:
|
// See if you can figure out the missing pieces:
|
||||||
for (bits, ???) |bit, ???| {
|
for (bits, 0..) |bit, i| {
|
||||||
// Note that we convert the usize i to a u32 with
|
// Note that we convert the usize i to a u32 with
|
||||||
// @intCast(), a builtin function just like @import().
|
// @intCast(), a builtin function just like @import().
|
||||||
// We'll learn about these properly in a later exercise.
|
// We'll learn about these properly in a later exercise.
|
||||||
|
|
|
@ -9,18 +9,18 @@
|
||||||
// Let's go from 1 to 16. This has been started for you, but there
|
// Let's go from 1 to 16. This has been started for you, but there
|
||||||
// are some problems. :-(
|
// are some problems. :-(
|
||||||
//
|
//
|
||||||
const std = import standard library;
|
const std = @import("std");
|
||||||
|
|
||||||
function main() void {
|
pub fn main() void {
|
||||||
var i: u8 = 1;
|
var i: u8 = 1;
|
||||||
const stop_at: u8 = 16;
|
const stop_at: u8 = 16;
|
||||||
|
|
||||||
// What kind of loop is this? A 'for' or a 'while'?
|
// What kind of loop is this? A 'for' or a 'while'?
|
||||||
??? (i <= stop_at) : (i += 1) {
|
while (i <= stop_at) : (i += 1) {
|
||||||
if (i % 3 == 0) std.debug.print("Fizz", .{});
|
if (i % 3 == 0) std.debug.print("Fizz", .{});
|
||||||
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
||||||
if (!(i % 3 == 0) and !(i % 5 == 0)) {
|
if (!(i % 3 == 0) and !(i % 5 == 0)) {
|
||||||
std.debug.print("{}", .{???});
|
std.debug.print("{}", .{i});
|
||||||
}
|
}
|
||||||
std.debug.print(", ", .{});
|
std.debug.print(", ", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,6 @@ pub fn main() void {
|
||||||
// We're just missing a couple things. One thing we're NOT missing is the
|
// We're just missing a couple things. One thing we're NOT missing is the
|
||||||
// keyword "pub", which is not needed here. Can you guess why?
|
// keyword "pub", which is not needed here. Can you guess why?
|
||||||
//
|
//
|
||||||
??? deepThought() ??? {
|
fn deepThought() u8 {
|
||||||
return 42; // Number courtesy Douglas Adams
|
return 42; // Number courtesy Douglas Adams
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ pub fn main() void {
|
||||||
// You'll need to figure out the parameter name and type that we're
|
// You'll need to figure out the parameter name and type that we're
|
||||||
// expecting. The output type has already been specified for you.
|
// expecting. The output type has already been specified for you.
|
||||||
//
|
//
|
||||||
fn twoToThe(???) u32 {
|
fn twoToThe(my_number: u8) u32 {
|
||||||
return std.math.pow(u32, 2, my_number);
|
return std.math.pow(u32, 2, my_number);
|
||||||
// std.math.pow(type, a, b) takes a numeric type and two
|
// std.math.pow(type, a, b) takes a numeric type and two
|
||||||
// numbers of that type (or that can coerce to that type) and
|
// numbers of that type (or that can coerce to that type) and
|
||||||
|
|
|
@ -21,8 +21,8 @@ pub fn main() void {
|
||||||
//
|
//
|
||||||
// This function prints, but does not return anything.
|
// This function prints, but does not return anything.
|
||||||
//
|
//
|
||||||
fn printPowersOfTwo(numbers: [4]u16) ??? {
|
fn printPowersOfTwo(numbers: [4]u16) void {
|
||||||
loop (numbers) |n| {
|
for (numbers) |n| {
|
||||||
std.debug.print("{} ", .{twoToThe(n)});
|
std.debug.print("{} ", .{twoToThe(n)});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,13 +31,13 @@ fn printPowersOfTwo(numbers: [4]u16) ??? {
|
||||||
// exercise. But don't be fooled! This one does the math without the aid
|
// exercise. But don't be fooled! This one does the math without the aid
|
||||||
// of the standard library!
|
// of the standard library!
|
||||||
//
|
//
|
||||||
fn twoToThe(number: u16) ??? {
|
fn twoToThe(number: u16) u32 {
|
||||||
var n: u16 = 0;
|
var n: u16 = 0;
|
||||||
var total: u16 = 1;
|
var total: u16 = 1;
|
||||||
|
|
||||||
loop (n < number) : (n += 1) {
|
while (n < number) : (n += 1) {
|
||||||
total *= 2;
|
total *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ???;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
// "TooSmall". Please add it where needed!
|
// "TooSmall". Please add it where needed!
|
||||||
const MyNumberError = error{
|
const MyNumberError = error{
|
||||||
TooBig,
|
TooBig,
|
||||||
???,
|
TooSmall,
|
||||||
TooFour,
|
TooFour,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -26,7 +26,7 @@ pub fn main() void {
|
||||||
if (number_error == MyNumberError.TooBig) {
|
if (number_error == MyNumberError.TooBig) {
|
||||||
std.debug.print(">4. ", .{});
|
std.debug.print(">4. ", .{});
|
||||||
}
|
}
|
||||||
if (???) {
|
if (number_error == MyNumberError.TooSmall) {
|
||||||
std.debug.print("<4. ", .{});
|
std.debug.print("<4. ", .{});
|
||||||
}
|
}
|
||||||
if (number_error == MyNumberError.TooFour) {
|
if (number_error == MyNumberError.TooFour) {
|
||||||
|
|
|
@ -19,7 +19,7 @@ const std = @import("std");
|
||||||
const MyNumberError = error{TooSmall};
|
const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
var my_number: ??? = 5;
|
var my_number: MyNumberError!u8 = 5;
|
||||||
|
|
||||||
// Looks like my_number will need to either store a number OR
|
// Looks like my_number will need to either store a number OR
|
||||||
// an error. Can you set the type correctly above?
|
// an error. Can you set the type correctly above?
|
||||||
|
|
|
@ -12,14 +12,14 @@ const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const a: u32 = addTwenty(44) catch 22;
|
const a: u32 = addTwenty(44) catch 22;
|
||||||
const b: u32 = addTwenty(4) ??? 22;
|
const b: u32 = addTwenty(4) catch 22;
|
||||||
|
|
||||||
std.debug.print("a={}, b={}\n", .{ a, b });
|
std.debug.print("a={}, b={}\n", .{ a, b });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Please provide the return type from this function.
|
// Please provide the return type from this function.
|
||||||
// Hint: it'll be an error union.
|
// Hint: it'll be an error union.
|
||||||
fn addTwenty(n: u32) ??? {
|
fn addTwenty(n: u32) MyNumberError!u32 {
|
||||||
if (n < 5) {
|
if (n < 5) {
|
||||||
return MyNumberError.TooSmall;
|
return MyNumberError.TooSmall;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -59,7 +59,12 @@ fn fixTooSmall(n: u32) MyNumberError!u32 {
|
||||||
// If we get a TooSmall error, we should return 10.
|
// If we get a TooSmall error, we should return 10.
|
||||||
// If we get any other error, we should return that error.
|
// If we get any other error, we should return that error.
|
||||||
// Otherwise, we return the u32 number.
|
// Otherwise, we return the u32 number.
|
||||||
return detectProblems(n) ???;
|
return detectProblems(n) catch |err| {
|
||||||
|
if (err == MyNumberError.TooSmall) {
|
||||||
|
return 10;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn detectProblems(n: u32) MyNumberError!u32 {
|
fn detectProblems(n: u32) MyNumberError!u32 {
|
||||||
|
|
|
@ -26,7 +26,7 @@ fn addFive(n: u32) MyNumberError!u32 {
|
||||||
// This function needs to return any error which might come back from detect().
|
// This function needs to return any error which might come back from detect().
|
||||||
// Please use a "try" statement rather than a "catch".
|
// Please use a "try" statement rather than a "catch".
|
||||||
//
|
//
|
||||||
const x = detect(n);
|
const x = try detect(n);
|
||||||
|
|
||||||
return x + 5;
|
return x + 5;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,5 +23,5 @@ pub fn main() !void {
|
||||||
// to be able to pass it up as a return value of main().
|
// to be able to pass it up as a return value of main().
|
||||||
//
|
//
|
||||||
// We just learned of a single statement which can accomplish this.
|
// We just learned of a single statement which can accomplish this.
|
||||||
stdout.print("Hello world!\n", .{});
|
try stdout.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,6 @@ const std = @import("std");
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
// Without changing anything else, please add a 'defer' statement
|
// Without changing anything else, please add a 'defer' statement
|
||||||
// to this code so that our program prints "One Two\n":
|
// to this code so that our program prints "One Two\n":
|
||||||
std.debug.print("Two\n", .{});
|
defer std.debug.print("Two\n", .{});
|
||||||
std.debug.print("One ", .{});
|
std.debug.print("One ", .{});
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ pub fn main() void {
|
||||||
fn printAnimal(animal: u8) void {
|
fn printAnimal(animal: u8) void {
|
||||||
std.debug.print("(", .{});
|
std.debug.print("(", .{});
|
||||||
|
|
||||||
std.debug.print(") ", .{}); // <---- how?!
|
defer std.debug.print(") ", .{}); // <---- how?!
|
||||||
|
|
||||||
if (animal == 'g') {
|
if (animal == 'g') {
|
||||||
std.debug.print("Goat", .{});
|
std.debug.print("Goat", .{});
|
||||||
|
|
|
@ -32,7 +32,7 @@ fn makeNumber() MyErr!u32 {
|
||||||
|
|
||||||
// Please make the "failed" message print ONLY if the makeNumber()
|
// Please make the "failed" message print ONLY if the makeNumber()
|
||||||
// function exits with an error:
|
// function exits with an error:
|
||||||
std.debug.print("failed!\n", .{});
|
errdefer std.debug.print("failed!\n", .{});
|
||||||
|
|
||||||
var num = try getNumber(); // <-- This could fail!
|
var num = try getNumber(); // <-- This could fail!
|
||||||
|
|
||||||
|
|
|
@ -46,6 +46,7 @@ pub fn main() void {
|
||||||
// match for every possible value). Please add an "else"
|
// match for every possible value). Please add an "else"
|
||||||
// to this switch to print a question mark "?" when c is
|
// to this switch to print a question mark "?" when c is
|
||||||
// not one of the existing matches.
|
// not one of the existing matches.
|
||||||
|
else => std.debug.print("?", .{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -31,6 +31,7 @@ pub fn main() void {
|
||||||
26 => 'Z',
|
26 => 'Z',
|
||||||
// As in the last exercise, please add the 'else' clause
|
// As in the last exercise, please add the 'else' clause
|
||||||
// and this time, have it return an exclamation mark '!'.
|
// and this time, have it return an exclamation mark '!'.
|
||||||
|
else => '!',
|
||||||
};
|
};
|
||||||
|
|
||||||
std.debug.print("{c}", .{real_char});
|
std.debug.print("{c}", .{real_char});
|
||||||
|
|
|
@ -35,6 +35,7 @@ pub fn main() void {
|
||||||
3 => {
|
3 => {
|
||||||
current_value *= current_value;
|
current_value *= current_value;
|
||||||
},
|
},
|
||||||
|
else => unreachable,
|
||||||
}
|
}
|
||||||
|
|
||||||
std.debug.print("{} ", .{current_value});
|
std.debug.print("{} ", .{current_value});
|
||||||
|
|
|
@ -39,7 +39,7 @@ pub fn main() void {
|
||||||
std.debug.print("={}. ", .{value});
|
std.debug.print("={}. ", .{value});
|
||||||
} else |err| switch (err) {
|
} else |err| switch (err) {
|
||||||
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
|
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
|
||||||
// Please add a match for TooSmall here and have it print: "<4. "
|
MyNumberError.TooSmall => std.debug.print("<4. ", .{}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,11 @@
|
||||||
// my_num=42
|
// my_num=42
|
||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const NumError = error{IllegalNumber};
|
const NumError = error{IllegalNumber};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() !void {
|
||||||
const stdout = std.io.getStdOut().writer();
|
const stdout = std.io.getStdOut().writer();
|
||||||
|
const my_num: u32 = try getNumber();
|
||||||
const my_num: u32 = getNumber();
|
|
||||||
|
|
||||||
try stdout.print("my_num={}\n", .{my_num});
|
try stdout.print("my_num={}\n", .{my_num});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
// Please complete the enum!
|
// Please complete the enum!
|
||||||
const Ops = enum { ??? };
|
const Ops = enum { inc, dec, pow };
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const operations = [_]Ops{
|
const operations = [_]Ops{
|
||||||
|
|
|
@ -31,7 +31,7 @@ const std = @import("std");
|
||||||
const Color = enum(u32) {
|
const Color = enum(u32) {
|
||||||
red = 0xff0000,
|
red = 0xff0000,
|
||||||
green = 0x00ff00,
|
green = 0x00ff00,
|
||||||
blue = ???,
|
blue = 0x0000ff,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
|
@ -53,12 +53,12 @@ pub fn main() void {
|
||||||
\\<p>
|
\\<p>
|
||||||
\\ <span style="color: #{x:0>6}">Red</span>
|
\\ <span style="color: #{x:0>6}">Red</span>
|
||||||
\\ <span style="color: #{x:0>6}">Green</span>
|
\\ <span style="color: #{x:0>6}">Green</span>
|
||||||
\\ <span style="color: #{}">Blue</span>
|
\\ <span style="color: #{x:0>6}">Blue</span>
|
||||||
\\</p>
|
\\</p>
|
||||||
\\
|
\\
|
||||||
, .{
|
, .{
|
||||||
@intFromEnum(Color.red),
|
@intFromEnum(Color.red),
|
||||||
@intFromEnum(Color.green),
|
@intFromEnum(Color.green),
|
||||||
@intFromEnum(???), // Oops! We're missing something!
|
@intFromEnum(Color.blue), // Oops! We're missing something!
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,8 +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) ???;
|
e = e.tail ???
|
||||||
|
|
||||||
e = e.tail.?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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",
|
||||||
|
|
|
@ -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 constant sized slice, this
|
// Instead of a simple integer or a slice with a constant size,
|
||||||
// program requires a slice to be allocated that is the same size as
|
// this program requires allocating a slice that is the same size
|
||||||
// an input array.
|
// as 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
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
//
|
//
|
||||||
// Bit manipulations is a very powerful tool just also from Zig.
|
// Bit manipulation is a very powerful tool, 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.
|
||||||
//
|
//
|
||||||
// Often it is not easy to understand at first glance what exactly these
|
// At first glance, it is often not easy to understand what exactly these
|
||||||
// algorithms do when only "numbers" in memory areas change outwardly.
|
// algorithms do when only "numbers" in memory areas change outwardly.
|
||||||
// But it must never be forgotten that the numbers only represent the
|
// However, it should never be forgotten that the numbers only represent
|
||||||
// interpretation of the bit sequences.
|
// the 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
|
||||||
// 10.1 "Table of Operators".
|
// "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:
|
||||||
//
|
//
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
//
|
//
|
||||||
// Another useful practice for bit manipulation is setting bits as flags.
|
// Another useful application 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 take a simpler way, very similar in principle, we define
|
// fast. Instead we choose a simpler approach that is very similar in principle:
|
||||||
// a variable with at least 26 bits (e.g. u32) and also set the bit for each
|
// We define a variable with at least 26 bits (e.g. u32) and set the bit for
|
||||||
// letter found at the corresponding position.
|
// each letter that is found in 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 uses a 32 bit variable of which we need 26 bits
|
// we use 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
|
||||||
|
|
|
@ -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 typically to convert
|
// These can be used in different ways, but generally to convert
|
||||||
// numerical values into various text representations. The
|
// numerical values into various text representations. The results
|
||||||
// results can be used for direct output to a terminal or stored
|
// can be used for direct output to a terminal or stored for
|
||||||
// for later use or written to a file. The latter is useful when
|
// 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.
|
||||||
|
|
|
@ -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 tasks, which always reaches its limits when pure computing
|
// the started and running tasks, which always reaches its limits when
|
||||||
// power is called up.
|
// pure computing 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
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
//
|
//
|
||||||
// Now that we are familiar with the principles of multi threading, we
|
// Now that we are familiar with the principles of multi-threading,
|
||||||
// boldly venture into a practical example from mathematics.
|
// let's 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 diggits.
|
// otherwise you will not be able to see the additional digits.
|
||||||
|
|
|
@ -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 is a good habit to close a file after you are done with it
|
||||||
// so that other program can read it and prevent data corruption
|
// so that other programs 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 are a keyword in zig that
|
// if only there were a keyword in zig that
|
||||||
// allow you "defer" code execute to the end of scope...
|
// allows you "defer" code execute to the end of scope...
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
// !you are not allow to switch this two lines to before file closing line!
|
// !you are not allowed to switch 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!");
|
||||||
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 command
|
// 2. print the content of the file in the console with the following 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 option can you uses with those function?
|
// - what kind of options can you use with those functions?
|
||||||
|
|
|
@ -5,14 +5,14 @@
|
||||||
// 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 wrote a program to read the content of the file that we just created.
|
// let's write a program to read the content of the file that we just created.
|
||||||
//
|
//
|
||||||
// I am assuming you've created the appropriate files for this to work.
|
// I am assuming that 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 character with all letter 'A', and print it
|
// - then, we initalize an array of characters with all letter 'A', and print it
|
||||||
// - After that, we read the content of the file to the array
|
// - After 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
|
||||||
|
|
||||||
|
@ -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 a 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 = ['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, seem like threat of violence is not the answer in this case
|
// okay, seems like a 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 answer that are both vaild in this case
|
// hint: you might find two answers that are both vaild in this case
|
||||||
const byte_read = zig_read_the_file_or_i_will_fight_you(&content);
|
const bytes_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} byte: {s}\n", .{
|
std.debug.print("Successfully Read {d} bytes: {s}\n", .{
|
||||||
byte_read,
|
bytes_read,
|
||||||
content, // change this line only
|
content, // change this line only
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
--- exercises/046_optionals2.zig 2023-10-03 22:15:22.122241138 +0200
|
--- exercises/046_optionals2.zig 2024-05-10 23:11:25.796632478 +0200
|
||||||
+++ answers/046_optionals2.zig 2023-10-05 20:04:07.049433424 +0200
|
+++ answers/046_optionals2.zig 2024-05-10 23:10:16.115335668 +0200
|
||||||
@@ -21,7 +21,7 @@
|
@@ -21,7 +21,7 @@
|
||||||
|
|
||||||
const Elephant = struct {
|
const Elephant = struct {
|
||||||
|
@ -9,12 +9,11 @@
|
||||||
visited: bool = false,
|
visited: bool = false,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@
|
@@ -51,6 +51,6 @@
|
||||||
// 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) ???;
|
- e = e.tail ???
|
||||||
+ if (e.tail == null) break;
|
+ e = e.tail orelse break;
|
||||||
|
|
||||||
e = e.tail.?;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
--- exercises/106_files.zig 2024-03-27 16:52:05.660910200 +0800
|
--- exercises/106_files.zig 2024-05-05 00:48:25.808548611 +0200
|
||||||
+++ answers/106_files.zig 2024-03-27 16:52:09.649422200 +0800
|
+++ answers/106_files.zig 2024-05-05 01:00:40.742969819 +0200
|
||||||
@@ -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 are a keyword in zig that
|
// if only there were a keyword in zig that
|
||||||
// allow you "defer" code execute to the end of scope...
|
// allows you "defer" code execute to the end of scope...
|
||||||
- file.close();
|
- file.close();
|
||||||
+ defer file.close();
|
+ defer file.close();
|
||||||
|
|
||||||
// !you are not allow to switch this two lines to before file closing line!
|
// !you are not allowed to switch 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!");
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
--- exercises/107_files2.zig 2024-03-27 16:51:56.199719600 +0800
|
--- exercises/107_files2.zig 2024-05-05 00:48:25.808548611 +0200
|
||||||
+++ answers/107_files2.zig 2024-03-27 16:52:01.650935300 +0800
|
+++ answers/107_files2.zig 2024-05-05 01:14:03.866062288 +0200
|
||||||
@@ -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 a 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 = ['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 answer that are both vaild in this case
|
// hint: you might find two answers that are both vaild in this case
|
||||||
- const byte_read = zig_read_the_file_or_i_will_fight_you(&content);
|
- const bytes_read = zig_read_the_file_or_i_will_fight_you(&content);
|
||||||
+ const byte_read = try file.read(&content);
|
+ const bytes_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} byte: {s}\n", .{
|
std.debug.print("Successfully Read {d} bytes: {s}\n", .{
|
||||||
byte_read,
|
bytes_read,
|
||||||
- content, // change this line only
|
- content, // change this line only
|
||||||
+ content[0..byte_read], // change this line only
|
+ content[0..bytes_read], // change this line only
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue