mirror of
https://codeberg.org/andyscott/ziglings.git
synced 2024-12-21 21:53:11 -05:00
changed the patch files that we can also use them with busybox for testing in Woodpecker
This commit is contained in:
parent
992323ac6c
commit
7491e3df91
109 changed files with 1606 additions and 770 deletions
|
@ -22,7 +22,9 @@ then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Which version we have?
|
# Which version we have?
|
||||||
echo "I am in version 23.4.25.1, let's try our magic power."
|
echo "Zig version" $(zig version)
|
||||||
|
echo "Eowyn version 23.10.5.1, let's try our magic power."
|
||||||
|
echo ""
|
||||||
|
|
||||||
# Create directory of healing if it doesn't already exist.
|
# Create directory of healing if it doesn't already exist.
|
||||||
mkdir -p patches/healed
|
mkdir -p patches/healed
|
||||||
|
@ -39,7 +41,9 @@ do
|
||||||
# Apply the bandages to the wounds, grow new limbs, let
|
# Apply the bandages to the wounds, grow new limbs, let
|
||||||
# new life spring into the broken bodies of the fallen.
|
# new life spring into the broken bodies of the fallen.
|
||||||
echo Healing "$true_name"...
|
echo Healing "$true_name"...
|
||||||
./test/patch --output="patches/healed/$true_name.zig" "$broken" "$patch_name"
|
cp "$broken" "patches/healed/$true_name.zig"
|
||||||
|
patch -i "$patch_name" "patches/healed/$true_name.zig"
|
||||||
|
|
||||||
else
|
else
|
||||||
echo Cannot heal "$true_name". No patch found.
|
echo Cannot heal "$true_name". No patch found.
|
||||||
fi
|
fi
|
||||||
|
|
60
patches/frodo.sh
Executable file
60
patches/frodo.sh
Executable file
|
@ -0,0 +1,60 @@
|
||||||
|
#!/bin/bash
|
||||||
|
#
|
||||||
|
# "How do you pick up the threads of an old life?
|
||||||
|
# How do you go on, when in your heart you begin
|
||||||
|
# to understand... there is no going back?
|
||||||
|
# There are some things that time cannot mend.
|
||||||
|
# Some hurts that go too deep, that have taken hold."
|
||||||
|
# Frodo, The Return of the King
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# This script shall repair the patches for the little
|
||||||
|
# broken programs using the old patches in this directory
|
||||||
|
# first, to heal them and then create new and better
|
||||||
|
# patches, with Gollum's help.
|
||||||
|
#
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# We check ourselves before we wreck ourselves.
|
||||||
|
if [ ! -f patches/frodo.sh ]
|
||||||
|
then
|
||||||
|
echo "But I must be run from the project root directory."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create directory of answers if it doesn't already exist.
|
||||||
|
mkdir -p answers
|
||||||
|
|
||||||
|
# Cycle through all the little broken Zig applications.
|
||||||
|
i=0
|
||||||
|
for broken in exercises/*.zig
|
||||||
|
do
|
||||||
|
((i=i+1))
|
||||||
|
|
||||||
|
# Remove the dir and extension, rendering the True Name.
|
||||||
|
true_name=$(basename "$broken" .zig)
|
||||||
|
patch_name="patches/patches/$true_name.patch"
|
||||||
|
healed_name="answers/$true_name.zig"
|
||||||
|
cp "$broken" "$healed_name"
|
||||||
|
# echo "$patch_name"
|
||||||
|
|
||||||
|
if [ -f "$patch_name" ]
|
||||||
|
then
|
||||||
|
# Apply the bandages to the wounds, grow new limbs, let
|
||||||
|
# new life spring into the broken bodies of the fallen.
|
||||||
|
echo Healing "$true_name"...
|
||||||
|
patch -i "$patch_name" "$healed_name"
|
||||||
|
|
||||||
|
# Create new prescriptions...
|
||||||
|
echo Repairing "$patch_name"...
|
||||||
|
if [ "$true_name.patch" = "999_the_end.patch" ]
|
||||||
|
then
|
||||||
|
i=999
|
||||||
|
fi
|
||||||
|
# with gollum's help!
|
||||||
|
./patches/gollum.sh $i
|
||||||
|
else
|
||||||
|
echo Cannot repair "$true_name". No patch found.
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
|
@ -26,6 +26,6 @@ echo "Hissss! before: '$b'"
|
||||||
echo " after: '$a'"
|
echo " after: '$a'"
|
||||||
echo " patch: '$p'"
|
echo " patch: '$p'"
|
||||||
|
|
||||||
diff $b $a > $p
|
diff -u $b $a > $p
|
||||||
|
|
||||||
cat $p
|
cat $p
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
19c19
|
--- exercises/001_hello.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< fn main() void {
|
+++ answers/001_hello.zig 2023-10-05 20:04:06.846096282 +0200
|
||||||
---
|
@@ -16,6 +16,6 @@
|
||||||
> pub fn main() void {
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
-fn main() void {
|
||||||
|
+pub fn main() void {
|
||||||
|
std.debug.print("Hello world!\n", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
14c14
|
--- exercises/002_std.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< ??? = @import("std");
|
+++ answers/002_std.zig 2023-10-05 20:04:06.849429678 +0200
|
||||||
---
|
@@ -11,7 +11,7 @@
|
||||||
> const std = @import("std");
|
// Please complete the import below:
|
||||||
|
//
|
||||||
|
|
||||||
|
-??? = @import("std");
|
||||||
|
+const std = @import("std");
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
std.debug.print("Standard Library.\n", .{});
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
37c37
|
--- exercises/003_assignment.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const n: u8 = 50;
|
+++ answers/003_assignment.zig 2023-10-05 20:04:06.856096469 +0200
|
||||||
---
|
@@ -34,12 +34,12 @@
|
||||||
> var n: u8 = 50;
|
const std = @import("std");
|
||||||
40c40
|
|
||||||
< const pi: u8 = 314159;
|
pub fn main() void {
|
||||||
---
|
- const n: u8 = 50;
|
||||||
> const pi: u32 = 314159;
|
+ var n: u8 = 50;
|
||||||
42c42
|
n = n + 5;
|
||||||
< const negative_eleven: u8 = -11;
|
|
||||||
---
|
- const pi: u8 = 314159;
|
||||||
> const negative_eleven: i8 = -11;
|
+ const pi: u32 = 314159;
|
||||||
|
|
||||||
|
- const negative_eleven: u8 = -11;
|
||||||
|
+ const negative_eleven: i8 = -11;
|
||||||
|
|
||||||
|
// There are no errors in the next line, just explanation:
|
||||||
|
// Perhaps you noticed before that the print function takes two
|
||||||
|
|
|
@ -1,12 +1,25 @@
|
||||||
30c30
|
--- exercises/004_arrays.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
+++ answers/004_arrays.zig 2023-10-05 20:04:06.859429866 +0200
|
||||||
---
|
@@ -27,7 +27,7 @@
|
||||||
> var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
// (Problem 1)
|
||||||
43c43
|
// This "const" is going to cause a problem later - can you see what it is?
|
||||||
< const fourth = some_primes[???];
|
// How do we fix it?
|
||||||
---
|
- const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
||||||
> const fourth = some_primes[3];
|
+ var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
|
||||||
47c47
|
|
||||||
< const length = some_primes.???;
|
// Individual values can be set with '[]' notation.
|
||||||
---
|
// Example: This line changes the first prime to 2 (which is correct):
|
||||||
> const length = some_primes.len;
|
@@ -40,11 +40,11 @@
|
||||||
|
// (Problem 2)
|
||||||
|
// Looks like we need to complete this expression. Use the example
|
||||||
|
// above to set "fourth" to the fourth element of the some_primes array:
|
||||||
|
- const fourth = some_primes[???];
|
||||||
|
+ const fourth = some_primes[3];
|
||||||
|
|
||||||
|
// (Problem 3)
|
||||||
|
// 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", .{
|
||||||
|
first, fourth, length,
|
||||||
|
|
|
@ -1,8 +1,17 @@
|
||||||
28c28
|
--- exercises/005_arrays2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const leet = ???;
|
+++ answers/005_arrays2.zig 2023-10-05 20:04:06.862763262 +0200
|
||||||
---
|
@@ -25,12 +25,12 @@
|
||||||
> const leet = le ++ et;
|
// (Problem 1)
|
||||||
33c33
|
// Please set this array concatenating the two arrays above.
|
||||||
< const bit_pattern = [_]u8{ ??? } ** 3;
|
// It should result in: 1 3 3 7
|
||||||
---
|
- const leet = ???;
|
||||||
> const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3;
|
+ const leet = le ++ et;
|
||||||
|
|
||||||
|
// (Problem 2)
|
||||||
|
// Please set this array using repetition.
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
|
|
@ -1,12 +1,24 @@
|
||||||
27c27
|
--- exercises/006_strings.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const d: u8 = ziggy[???];
|
+++ answers/006_strings.zig 2023-10-05 20:04:06.869430053 +0200
|
||||||
---
|
@@ -24,18 +24,18 @@
|
||||||
> const d: u8 = ziggy[4];
|
// (Problem 1)
|
||||||
31c31
|
// Use array square bracket syntax to get the letter 'd' from
|
||||||
< const laugh = "ha " ???;
|
// the string "stardust" above.
|
||||||
---
|
- const d: u8 = ziggy[???];
|
||||||
> const laugh = "ha " ** 3;
|
+ const d: u8 = ziggy[4];
|
||||||
38c38
|
|
||||||
< const major_tom = major ??? tom;
|
// (Problem 2)
|
||||||
---
|
// Use the array repeat '**' operator to make "ha ha ha ".
|
||||||
> const major_tom = major ++ " " ++ tom;
|
- const laugh = "ha " ???;
|
||||||
|
+ const laugh = "ha " ** 3;
|
||||||
|
|
||||||
|
// (Problem 3)
|
||||||
|
// Use the array concatenation '++' operator to make "Major Tom".
|
||||||
|
// (You'll need to add a space as well!)
|
||||||
|
const major = "Major";
|
||||||
|
const tom = "Tom";
|
||||||
|
- const major_tom = major ??? tom;
|
||||||
|
+ const major_tom = major ++ " " ++ tom;
|
||||||
|
|
||||||
|
// That's all the problems. Let's see our results:
|
||||||
|
std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom });
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
18,20c18,20
|
--- exercises/007_strings2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< Ziggy played guitar
|
+++ answers/007_strings2.zig 2023-10-05 20:04:06.872763449 +0200
|
||||||
< Jamming good with Andrew Kelley
|
@@ -15,9 +15,9 @@
|
||||||
< And the Spiders from Mars
|
|
||||||
---
|
pub fn main() void {
|
||||||
> \\Ziggy played guitar
|
const lyrics =
|
||||||
> \\Jamming good with Andrew Kelley
|
- Ziggy played guitar
|
||||||
> \\And the Spiders from Mars
|
- Jamming good with Andrew Kelley
|
||||||
|
- And the Spiders from Mars
|
||||||
|
+ \\Ziggy played guitar
|
||||||
|
+ \\Jamming good with Andrew Kelley
|
||||||
|
+ \\And the Spiders from Mars
|
||||||
|
;
|
||||||
|
|
||||||
|
std.debug.print("{s}\n", .{lyrics});
|
||||||
|
|
|
@ -1,18 +1,30 @@
|
||||||
22c22
|
--- exercises/008_quiz.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const x: usize = 1;
|
+++ answers/008_quiz.zig 2023-10-05 20:04:06.879430240 +0200
|
||||||
---
|
@@ -19,11 +19,11 @@
|
||||||
> var x: usize = 1;
|
// the idiomatic type to use for array indexing.
|
||||||
26c26
|
//
|
||||||
< // 'undefined'. There is no problem on this line.
|
// There IS a problem on this line, but 'usize' isn't it.
|
||||||
---
|
- const x: usize = 1;
|
||||||
> // 'undefined'. There is no error here.
|
+ var x: usize = 1;
|
||||||
36c36
|
|
||||||
< lang[???] = letters[x];
|
// Note: When you want to declare memory (an array in this
|
||||||
---
|
// case) without putting anything in it, you can set it to
|
||||||
> lang[1] = letters[x];
|
- // 'undefined'. There is no problem on this line.
|
||||||
38,39c38,39
|
+ // 'undefined'. There is no error here.
|
||||||
< x = ???;
|
var lang: [3]u8 = undefined;
|
||||||
< lang[2] = letters[???];
|
|
||||||
---
|
// The following lines attempt to put 'Z', 'i', and 'g' into the
|
||||||
> x = 5;
|
@@ -33,10 +33,10 @@
|
||||||
> lang[2] = letters[x];
|
lang[0] = letters[x];
|
||||||
|
|
||||||
|
x = 3;
|
||||||
|
- lang[???] = letters[x];
|
||||||
|
+ lang[1] = letters[x];
|
||||||
|
|
||||||
|
- x = ???;
|
||||||
|
- lang[2] = letters[???];
|
||||||
|
+ x = 5;
|
||||||
|
+ lang[2] = letters[x];
|
||||||
|
|
||||||
|
// We want to "Program in Zig!" of course:
|
||||||
|
std.debug.print("Program in {s}!\n", .{lang});
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
27c27
|
--- exercises/009_if.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< if (foo) {
|
+++ answers/009_if.zig 2023-10-05 20:04:06.882763636 +0200
|
||||||
---
|
@@ -24,7 +24,7 @@
|
||||||
> if (foo == 1) {
|
const foo = 1;
|
||||||
|
|
||||||
|
// Please fix this condition:
|
||||||
|
- if (foo) {
|
||||||
|
+ if (foo == 1) {
|
||||||
|
// We want our program to print this message!
|
||||||
|
std.debug.print("Foo is 1!\n", .{});
|
||||||
|
} else {
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
13c13
|
--- exercises/010_if2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const price: u8 = if ???;
|
+++ answers/010_if2.zig 2023-10-05 20:04:06.886097032 +0200
|
||||||
---
|
@@ -10,7 +10,7 @@
|
||||||
> const price: u8 = if (discount) 17 else 20;
|
|
||||||
|
// Please use an if...else expression to set "price".
|
||||||
|
// 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});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
24c24
|
--- exercises/011_while.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< while (???) {
|
+++ answers/011_while.zig 2023-10-05 20:04:06.892763823 +0200
|
||||||
---
|
@@ -21,7 +21,7 @@
|
||||||
> while (n < 1024) {
|
var n: u32 = 2;
|
||||||
|
|
||||||
|
// Please use a condition that is true UNTIL "n" reaches 1024:
|
||||||
|
- while (???) {
|
||||||
|
+ while (n < 1024) {
|
||||||
|
// Print the current number
|
||||||
|
std.debug.print("{} ", .{n});
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
28c28
|
--- exercises/012_while2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< while (n < 1000) : ??? {
|
+++ answers/012_while2.zig 2023-10-05 20:04:06.896097219 +0200
|
||||||
---
|
@@ -25,7 +25,7 @@
|
||||||
> while (n < 1000) : (n *= 2) {
|
|
||||||
|
// Please set the continue expression so that we get the desired
|
||||||
|
// results in the print statement below.
|
||||||
|
- while (n < 1000) : ??? {
|
||||||
|
+ while (n < 1000) : (n *= 2) {
|
||||||
|
// Print the current number
|
||||||
|
std.debug.print("{} ", .{n});
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
27,28c27,28
|
--- exercises/013_while3.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< if (n % 3 == 0) ???;
|
+++ answers/013_while3.zig 2023-10-05 20:04:06.899430616 +0200
|
||||||
< if (n % 5 == 0) ???;
|
@@ -24,8 +24,8 @@
|
||||||
---
|
while (n <= 20) : (n += 1) {
|
||||||
> if (n % 3 == 0) continue;
|
// The '%' symbol is the "modulo" operator and it
|
||||||
> if (n % 5 == 0) continue;
|
// returns the remainder after division.
|
||||||
|
- if (n % 3 == 0) ???;
|
||||||
|
- if (n % 5 == 0) ???;
|
||||||
|
+ if (n % 3 == 0) continue;
|
||||||
|
+ if (n % 5 == 0) continue;
|
||||||
|
std.debug.print("{} ", .{n});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
21c21
|
--- exercises/014_while4.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< if (???) ???;
|
+++ answers/014_while4.zig 2023-10-05 20:04:06.906097406 +0200
|
||||||
---
|
@@ -18,7 +18,7 @@
|
||||||
> if (n == 4) break;
|
// Oh dear! This while loop will go forever?!
|
||||||
|
// Please fix this so the print statement below gives the desired output.
|
||||||
|
while (true) : (n += 1) {
|
||||||
|
- if (???) ???;
|
||||||
|
+ if (n == 4) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result: we want n=4
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
18c18
|
--- exercises/015_for.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< for (???) |???| {
|
+++ answers/015_for.zig 2023-10-05 20:04:06.909430803 +0200
|
||||||
---
|
@@ -15,7 +15,7 @@
|
||||||
> for (story) |scene| {
|
|
||||||
|
std.debug.print("A Dramatic Story: ", .{});
|
||||||
|
|
||||||
|
- for (???) |???| {
|
||||||
|
+ for (story) |scene| {
|
||||||
|
if (scene == 'h') std.debug.print(":-) ", .{});
|
||||||
|
if (scene == 's') std.debug.print(":-( ", .{});
|
||||||
|
if (scene == 'n') std.debug.print(":-| ", .{});
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
28c28
|
--- exercises/016_for2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< for (bits, ???) |bit, ???| {
|
+++ answers/016_for2.zig 2023-10-05 20:04:06.912764197 +0200
|
||||||
---
|
@@ -25,7 +25,7 @@
|
||||||
> for (bits, 0..) |bit, i| {
|
// the value of the place as a power of two for each bit.
|
||||||
|
//
|
||||||
|
// 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
|
||||||
|
// @intCast(), a builtin function just like @import().
|
||||||
|
// We'll learn about these properly in a later exercise.
|
||||||
|
|
|
@ -1,16 +1,25 @@
|
||||||
12c12
|
--- exercises/017_quiz2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const std = import standard library;
|
+++ answers/017_quiz2.zig 2023-10-05 20:04:06.919430989 +0200
|
||||||
---
|
@@ -9,18 +9,18 @@
|
||||||
> const std = @import("std");
|
// Let's go from 1 to 16. This has been started for you, but there
|
||||||
14c14
|
// are some problems. :-(
|
||||||
< function main() void {
|
//
|
||||||
---
|
-const std = import standard library;
|
||||||
> pub fn main() void {
|
+const std = @import("std");
|
||||||
19c19
|
|
||||||
< ??? (i <= stop_at) : (i += 1) {
|
-function main() void {
|
||||||
---
|
+pub fn main() void {
|
||||||
> while (i <= stop_at) : (i += 1) {
|
var i: u8 = 1;
|
||||||
23c23
|
const stop_at: u8 = 16;
|
||||||
< std.debug.print("{}", .{???});
|
|
||||||
---
|
// What kind of loop is this? A 'for' or a 'while'?
|
||||||
> std.debug.print("{}", .{i});
|
- ??? (i <= stop_at) : (i += 1) {
|
||||||
|
+ while (i <= stop_at) : (i += 1) {
|
||||||
|
if (i % 3 == 0) std.debug.print("Fizz", .{});
|
||||||
|
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
||||||
|
if (!(i % 3 == 0) and !(i % 5 == 0)) {
|
||||||
|
- std.debug.print("{}", .{???});
|
||||||
|
+ std.debug.print("{}", .{i});
|
||||||
|
}
|
||||||
|
std.debug.print(", ", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
28c28
|
--- exercises/018_functions.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< ??? deepThought() ??? {
|
+++ answers/018_functions.zig 2023-10-05 20:04:06.922764386 +0200
|
||||||
---
|
@@ -25,6 +25,6 @@
|
||||||
> fn deepThought() u8 {
|
// 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?
|
||||||
|
//
|
||||||
|
-??? deepThought() ??? {
|
||||||
|
+fn deepThought() u8 {
|
||||||
|
return 42; // Number courtesy Douglas Adams
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
25c25
|
--- exercises/019_functions2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< fn twoToThe(???) u32 {
|
+++ answers/019_functions2.zig 2023-10-05 20:04:06.926097780 +0200
|
||||||
---
|
@@ -22,7 +22,7 @@
|
||||||
> fn twoToThe(my_number: u32) u32 {
|
// You'll need to figure out the parameter name and type that we're
|
||||||
|
// expecting. The output type has already been specified for you.
|
||||||
|
//
|
||||||
|
-fn twoToThe(???) u32 {
|
||||||
|
+fn twoToThe(my_number: u32) u32 {
|
||||||
|
return std.math.pow(u32, 2, my_number);
|
||||||
|
// std.math.pow(type, a, b) takes a numeric type and two
|
||||||
|
// numbers of that type (or that can coerce to that type) and
|
||||||
|
|
|
@ -1,18 +1,30 @@
|
||||||
24,25c24,25
|
--- exercises/020_quiz3.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< fn printPowersOfTwo(numbers: [4]u16) ??? {
|
+++ answers/020_quiz3.zig 2023-10-05 20:04:06.932764573 +0200
|
||||||
< loop (numbers) |n| {
|
@@ -21,8 +21,8 @@
|
||||||
---
|
//
|
||||||
> fn printPowersOfTwo(numbers: [4]u16) void {
|
// This function prints, but does not return anything.
|
||||||
> for (numbers) |n| {
|
//
|
||||||
34c34
|
-fn printPowersOfTwo(numbers: [4]u16) ??? {
|
||||||
< fn twoToThe(number: u16) ??? {
|
- loop (numbers) |n| {
|
||||||
---
|
+fn printPowersOfTwo(numbers: [4]u16) void {
|
||||||
> fn twoToThe(number: u16) u16 {
|
+ for (numbers) |n| {
|
||||||
38c38
|
std.debug.print("{} ", .{twoToThe(n)});
|
||||||
< loop (n < number) : (n += 1) {
|
}
|
||||||
---
|
}
|
||||||
> while (n < number) : (n += 1) {
|
@@ -31,13 +31,13 @@
|
||||||
42c42
|
// exercise. But don't be fooled! This one does the math without the aid
|
||||||
< return ???;
|
// of the standard library!
|
||||||
---
|
//
|
||||||
> return total;
|
-fn twoToThe(number: u16) ??? {
|
||||||
|
+fn twoToThe(number: u16) u16 {
|
||||||
|
var n: u16 = 0;
|
||||||
|
var total: u16 = 1;
|
||||||
|
|
||||||
|
- loop (n < number) : (n += 1) {
|
||||||
|
+ while (n < number) : (n += 1) {
|
||||||
|
total *= 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
- return ???;
|
||||||
|
+ return total;
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,20 @@
|
||||||
12c12
|
--- exercises/021_errors.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< ???,
|
+++ answers/021_errors.zig 2023-10-05 20:04:06.936097967 +0200
|
||||||
---
|
@@ -9,7 +9,7 @@
|
||||||
> TooSmall,
|
// "TooSmall". Please add it where needed!
|
||||||
29c29
|
const MyNumberError = error{
|
||||||
< if (???) {
|
TooBig,
|
||||||
---
|
- ???,
|
||||||
> if (number_error == MyNumberError.TooSmall) {
|
+ TooSmall,
|
||||||
|
TooFour,
|
||||||
|
};
|
||||||
|
|
||||||
|
@@ -26,7 +26,7 @@
|
||||||
|
if (number_error == MyNumberError.TooBig) {
|
||||||
|
std.debug.print(">4. ", .{});
|
||||||
|
}
|
||||||
|
- if (???) {
|
||||||
|
+ if (number_error == MyNumberError.TooSmall) {
|
||||||
|
std.debug.print("<4. ", .{});
|
||||||
|
}
|
||||||
|
if (number_error == MyNumberError.TooFour) {
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
22c22
|
--- exercises/022_errors2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< var my_number: ??? = 5;
|
+++ answers/022_errors2.zig 2023-10-05 20:04:06.939431363 +0200
|
||||||
---
|
@@ -19,7 +19,7 @@
|
||||||
> var my_number: MyNumberError!u8 = 5;
|
const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
|
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
|
||||||
|
// an error. Can you set the type correctly above?
|
||||||
|
|
|
@ -1,8 +1,19 @@
|
||||||
15c15
|
--- exercises/023_errors3.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const b: u32 = addTwenty(4) ??? 22;
|
+++ answers/023_errors3.zig 2023-10-05 20:04:06.946098156 +0200
|
||||||
---
|
@@ -12,14 +12,14 @@
|
||||||
> const b: u32 = addTwenty(4) catch 22;
|
|
||||||
22c22
|
pub fn main() void {
|
||||||
< fn addTwenty(n: u32) ??? {
|
const a: u32 = addTwenty(44) catch 22;
|
||||||
---
|
- const b: u32 = addTwenty(4) ??? 22;
|
||||||
> fn addTwenty(n: u32) MyNumberError!u32 {
|
+ const b: u32 = addTwenty(4) catch 22;
|
||||||
|
|
||||||
|
std.debug.print("a={}, b={}\n", .{ a, b });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Please provide the return type from this function.
|
||||||
|
// Hint: it'll be an error union.
|
||||||
|
-fn addTwenty(n: u32) ??? {
|
||||||
|
+fn addTwenty(n: u32) MyNumberError!u32 {
|
||||||
|
if (n < 5) {
|
||||||
|
return MyNumberError.TooSmall;
|
||||||
|
} else {
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
62c62,68
|
--- exercises/024_errors4.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< return detectProblems(n) ???;
|
+++ answers/024_errors4.zig 2023-10-05 20:04:06.949431550 +0200
|
||||||
---
|
@@ -59,7 +59,13 @@
|
||||||
> return detectProblems(n) catch |err| {
|
// If we get a TooSmall error, we should return 10.
|
||||||
> if (err == MyNumberError.TooSmall) {
|
// If we get any other error, we should return that error.
|
||||||
> return 10;
|
// Otherwise, we return the u32 number.
|
||||||
> }
|
- return detectProblems(n) ???;
|
||||||
>
|
+ return detectProblems(n) catch |err| {
|
||||||
> return err;
|
+ if (err == MyNumberError.TooSmall) {
|
||||||
> };
|
+ return 10;
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ return err;
|
||||||
|
+ };
|
||||||
|
}
|
||||||
|
|
||||||
|
fn detectProblems(n: u32) MyNumberError!u32 {
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
29c29
|
--- exercises/025_errors5.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< var x = detect(n);
|
+++ answers/025_errors5.zig 2023-10-05 20:04:06.952764946 +0200
|
||||||
---
|
@@ -26,7 +26,7 @@
|
||||||
> var x = try detect(n);
|
// This function needs to return any error which might come back from detect().
|
||||||
|
// Please use a "try" statement rather than a "catch".
|
||||||
|
//
|
||||||
|
- var x = detect(n);
|
||||||
|
+ var x = try detect(n);
|
||||||
|
|
||||||
|
return x + 5;
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
26c26
|
--- exercises/026_hello2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< stdout.print("Hello world!\n", .{});
|
+++ answers/026_hello2.zig 2023-10-05 20:04:06.959431737 +0200
|
||||||
---
|
@@ -23,5 +23,5 @@
|
||||||
> try stdout.print("Hello world!\n", .{});
|
// to be able to pass it up as a return value of main().
|
||||||
|
//
|
||||||
|
// We just learned of a single statement which can accomplish this.
|
||||||
|
- stdout.print("Hello world!\n", .{});
|
||||||
|
+ try stdout.print("Hello world!\n", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
23c23
|
--- exercises/027_defer.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< std.debug.print("Two\n", .{});
|
+++ answers/027_defer.zig 2023-10-05 20:04:06.962765133 +0200
|
||||||
---
|
@@ -20,6 +20,6 @@
|
||||||
> defer std.debug.print("Two\n", .{});
|
pub fn main() void {
|
||||||
|
// Without changing anything else, please add a 'defer' statement
|
||||||
|
// 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 ", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
21c21
|
--- exercises/028_defer2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< std.debug.print(") ", .{}); // <---- how?!
|
+++ answers/028_defer2.zig 2023-10-05 20:04:06.966098530 +0200
|
||||||
---
|
@@ -18,7 +18,7 @@
|
||||||
> defer std.debug.print(") ", .{}); // <---- how?!
|
fn printAnimal(animal: u8) void {
|
||||||
|
std.debug.print("(", .{});
|
||||||
|
|
||||||
|
- std.debug.print(") ", .{}); // <---- how?!
|
||||||
|
+ defer std.debug.print(") ", .{}); // <---- how?!
|
||||||
|
|
||||||
|
if (animal == 'g') {
|
||||||
|
std.debug.print("Goat", .{});
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
35c35
|
--- exercises/029_errdefer.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< std.debug.print("failed!\n", .{});
|
+++ answers/029_errdefer.zig 2023-10-05 20:04:06.972765320 +0200
|
||||||
---
|
@@ -32,7 +32,7 @@
|
||||||
> errdefer std.debug.print("failed!\n", .{});
|
|
||||||
|
// Please make the "failed" message print ONLY if the makeNumber()
|
||||||
|
// function exits with an error:
|
||||||
|
- std.debug.print("failed!\n", .{});
|
||||||
|
+ errdefer std.debug.print("failed!\n", .{});
|
||||||
|
|
||||||
|
var num = try getNumber(); // <-- This could fail!
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,10 @@
|
||||||
48a49
|
--- exercises/030_switch.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> else => std.debug.print("?", .{}),
|
+++ answers/030_switch.zig 2023-10-05 20:04:06.976098717 +0200
|
||||||
|
@@ -46,6 +46,7 @@
|
||||||
|
// match for every possible value). Please add an "else"
|
||||||
|
// to this switch to print a question mark "?" when c is
|
||||||
|
// not one of the existing matches.
|
||||||
|
+ else => std.debug.print("?", .{}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,10 @@
|
||||||
33a34
|
--- exercises/031_switch2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> else => '!',
|
+++ answers/031_switch2.zig 2023-10-05 20:04:06.979432113 +0200
|
||||||
|
@@ -31,6 +31,7 @@
|
||||||
|
26 => 'Z',
|
||||||
|
// As in the last exercise, please add the 'else' clause
|
||||||
|
// and this time, have it return an exclamation mark '!'.
|
||||||
|
+ else => '!',
|
||||||
|
};
|
||||||
|
|
||||||
|
std.debug.print("{c}", .{real_char});
|
||||||
|
|
|
@ -1,2 +1,10 @@
|
||||||
37a38
|
--- exercises/032_unreachable.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> else => unreachable,
|
+++ answers/032_unreachable.zig 2023-10-05 20:04:06.986098904 +0200
|
||||||
|
@@ -35,6 +35,7 @@
|
||||||
|
3 => {
|
||||||
|
current_value *= current_value;
|
||||||
|
},
|
||||||
|
+ else => unreachable,
|
||||||
|
}
|
||||||
|
|
||||||
|
std.debug.print("{} ", .{current_value});
|
||||||
|
|
|
@ -1,2 +1,10 @@
|
||||||
41a42
|
--- exercises/033_iferror.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> MyNumberError.TooSmall => std.debug.print("<4. ", .{}),
|
+++ answers/033_iferror.zig 2023-10-05 20:04:06.989432300 +0200
|
||||||
|
@@ -39,6 +39,7 @@
|
||||||
|
std.debug.print("={}. ", .{value});
|
||||||
|
} else |err| switch (err) {
|
||||||
|
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
|
||||||
|
+ MyNumberError.TooSmall => std.debug.print("<4. ", .{}),
|
||||||
|
// Please add a match for TooSmall here and have it print: "<4. "
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
12c12
|
--- exercises/034_quiz4.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< pub fn main() void {
|
+++ answers/034_quiz4.zig 2023-10-05 20:04:06.996099091 +0200
|
||||||
---
|
@@ -9,10 +9,10 @@
|
||||||
> pub fn main() !void {
|
|
||||||
15c15
|
const NumError = error{IllegalNumber};
|
||||||
< const my_num: u32 = getNumber();
|
|
||||||
---
|
-pub fn main() void {
|
||||||
> const my_num: u32 = try getNumber();
|
+pub fn main() !void {
|
||||||
|
const stdout = std.io.getStdOut().writer();
|
||||||
|
|
||||||
|
- const my_num: u32 = getNumber();
|
||||||
|
+ const my_num: u32 = try getNumber();
|
||||||
|
|
||||||
|
try stdout.print("my_num={}\n", .{my_num});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
23c23
|
--- exercises/035_enums.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const Ops = enum { ??? };
|
+++ answers/035_enums.zig 2023-10-05 20:04:06.999432487 +0200
|
||||||
---
|
@@ -20,7 +20,7 @@
|
||||||
> const Ops = enum { dec, inc, pow };
|
const std = @import("std");
|
||||||
|
|
||||||
|
// Please complete the enum!
|
||||||
|
-const Ops = enum { ??? };
|
||||||
|
+const Ops = enum { dec, inc, pow };
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
const operations = [_]Ops{
|
||||||
|
|
|
@ -1,12 +1,26 @@
|
||||||
34c34
|
--- exercises/036_enums2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< blue = ???,
|
+++ answers/036_enums2.zig 2023-10-05 20:04:07.002765884 +0200
|
||||||
---
|
@@ -31,7 +31,7 @@
|
||||||
> blue = 0x0000ff,
|
const Color = enum(u32) {
|
||||||
56c56
|
red = 0xff0000,
|
||||||
< \\ <span style="color: #{}">Blue</span>
|
green = 0x00ff00,
|
||||||
---
|
- blue = ???,
|
||||||
> \\ <span style="color: #{x:0>6}">Blue</span>
|
+ blue = 0x0000ff,
|
||||||
62c62
|
};
|
||||||
< @intFromEnum(???), // Oops! We're missing something!
|
|
||||||
---
|
pub fn main() void {
|
||||||
> @intFromEnum(Color.blue), // Oops! We're missing something!
|
@@ -53,12 +53,12 @@
|
||||||
|
\\<p>
|
||||||
|
\\ <span style="color: #{x:0>6}">Red</span>
|
||||||
|
\\ <span style="color: #{x:0>6}">Green</span>
|
||||||
|
- \\ <span style="color: #{}">Blue</span>
|
||||||
|
+ \\ <span style="color: #{x:0>6}">Blue</span>
|
||||||
|
\\</p>
|
||||||
|
\\
|
||||||
|
, .{
|
||||||
|
@intFromEnum(Color.red),
|
||||||
|
@intFromEnum(Color.green),
|
||||||
|
- @intFromEnum(???), // Oops! We're missing something!
|
||||||
|
+ @intFromEnum(Color.blue), // Oops! We're missing something!
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,18 @@
|
||||||
38a39
|
--- exercises/037_structs.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> health: u8,
|
+++ answers/037_structs.zig 2023-10-05 20:04:07.009432674 +0200
|
||||||
46a48
|
@@ -36,6 +36,7 @@
|
||||||
> .health = 100,
|
role: Role,
|
||||||
|
gold: u32,
|
||||||
|
experience: u32,
|
||||||
|
+ health: u8,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
@@ -44,6 +45,7 @@
|
||||||
|
.role = Role.wizard,
|
||||||
|
.gold = 20,
|
||||||
|
.experience = 10,
|
||||||
|
+ .health = 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Glorp gains some gold.
|
||||||
|
|
|
@ -1,7 +1,15 @@
|
||||||
44a45,50
|
--- exercises/038_structs2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> chars[1] = Character{
|
+++ answers/038_structs2.zig 2023-10-05 20:04:07.012766070 +0200
|
||||||
> .role = Role.bard,
|
@@ -42,6 +42,12 @@
|
||||||
> .gold = 10,
|
//
|
||||||
> .health = 100,
|
// Feel free to run this program without adding Zump. What does
|
||||||
> .experience = 20,
|
// it do and why?
|
||||||
> };
|
+ chars[1] = Character{
|
||||||
|
+ .role = Role.bard,
|
||||||
|
+ .gold = 10,
|
||||||
|
+ .health = 100,
|
||||||
|
+ .experience = 20,
|
||||||
|
+ };
|
||||||
|
|
||||||
|
// Printing all RPG characters in a loop:
|
||||||
|
for (chars, 0..) |c, num| {
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
33c33
|
--- exercises/039_pointers.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< num2 = ???;
|
+++ answers/039_pointers.zig 2023-10-05 20:04:07.016099467 +0200
|
||||||
---
|
@@ -30,7 +30,7 @@
|
||||||
> num2 = num1_pointer.*;
|
|
||||||
|
// Please make num2 equal 5 using num1_pointer!
|
||||||
|
// (See the "cheatsheet" above for ideas.)
|
||||||
|
- num2 = ???;
|
||||||
|
+ num2 = num1_pointer.*;
|
||||||
|
|
||||||
|
std.debug.print("num1: {}, num2: {}\n", .{ num1, num2 });
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
26c26
|
--- exercises/040_pointers2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const b: *u8 = &a; // fix this!
|
+++ answers/040_pointers2.zig 2023-10-05 20:04:07.022766257 +0200
|
||||||
---
|
@@ -23,7 +23,7 @@
|
||||||
> const b: *const u8 = &a; // fix this!
|
|
||||||
|
pub fn main() void {
|
||||||
|
const a: u8 = 12;
|
||||||
|
- const b: *u8 = &a; // fix this!
|
||||||
|
+ const b: *const u8 = &a; // fix this!
|
||||||
|
|
||||||
|
std.debug.print("a: {}, b: {}\n", .{ a, b.* });
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
34c34
|
--- exercises/041_pointers3.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< ??? p: ??? = undefined;
|
+++ answers/041_pointers3.zig 2023-10-05 20:04:07.026099654 +0200
|
||||||
---
|
@@ -31,7 +31,7 @@
|
||||||
> var p: *u8 = undefined;
|
|
||||||
|
// Please define pointer "p" so that it can point to EITHER foo or
|
||||||
|
// bar AND change the value it points to!
|
||||||
|
- ??? p: ??? = undefined;
|
||||||
|
+ var p: *u8 = undefined;
|
||||||
|
|
||||||
|
p = &foo;
|
||||||
|
p.* += 1;
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
40c40
|
--- exercises/042_pointers4.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< ??? = 5; // fix me!
|
+++ answers/042_pointers4.zig 2023-10-05 20:04:07.032766444 +0200
|
||||||
---
|
@@ -37,5 +37,5 @@
|
||||||
> x.* = 5; // fix me!
|
// This function should take a reference to a u8 value and set it
|
||||||
|
// to 5.
|
||||||
|
fn makeFive(x: *u8) void {
|
||||||
|
- ??? = 5; // fix me!
|
||||||
|
+ x.* = 5; // fix me!
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
71c71
|
--- exercises/043_pointers5.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< printCharacter(???);
|
+++ answers/043_pointers5.zig 2023-10-05 20:04:07.036099841 +0200
|
||||||
---
|
@@ -68,7 +68,7 @@
|
||||||
> printCharacter(&glorp);
|
|
||||||
|
// FIX ME!
|
||||||
|
// Please pass Glorp to printCharacter():
|
||||||
|
- printCharacter(???);
|
||||||
|
+ printCharacter(&glorp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note how this function's "c" parameter is a pointer to a Character struct.
|
||||||
|
|
|
@ -1,4 +1,17 @@
|
||||||
21a22
|
--- exercises/044_quiz5.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> var elephantB = Elephant{ .letter = 'B' };
|
+++ answers/044_quiz5.zig 2023-10-05 20:04:07.039433235 +0200
|
||||||
27a29
|
@@ -19,12 +19,14 @@
|
||||||
> elephantB.tail = &elephantC;
|
pub fn main() void {
|
||||||
|
var elephantA = Elephant{ .letter = 'A' };
|
||||||
|
// (Please add Elephant B here!)
|
||||||
|
+ var elephantB = Elephant{ .letter = 'B' };
|
||||||
|
var elephantC = Elephant{ .letter = 'C' };
|
||||||
|
|
||||||
|
// Link the elephants so that each tail "points" to the next elephant.
|
||||||
|
// They make a circle: A->B->C->A...
|
||||||
|
elephantA.tail = &elephantB;
|
||||||
|
// (Please link Elephant B's tail to Elephant C here!)
|
||||||
|
+ elephantB.tail = &elephantC;
|
||||||
|
elephantC.tail = &elephantA;
|
||||||
|
|
||||||
|
visitElephants(&elephantA);
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
32c32
|
--- exercises/045_optionals.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const answer: u8 = result;
|
+++ answers/045_optionals.zig 2023-10-05 20:04:07.046100027 +0200
|
||||||
---
|
@@ -29,7 +29,7 @@
|
||||||
> const answer: u8 = result orelse 42;
|
|
||||||
|
// Please threaten the result so that answer is either the
|
||||||
|
// integer value from deepThought() OR the number 42:
|
||||||
|
- const answer: u8 = result;
|
||||||
|
+ const answer: u8 = result orelse 42;
|
||||||
|
|
||||||
|
std.debug.print("The Ultimate Answer: {}.\n", .{answer});
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,20 @@
|
||||||
24c24
|
--- exercises/046_optionals2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< tail: *Elephant = null, // Hmm... tail needs something...
|
+++ answers/046_optionals2.zig 2023-10-05 20:04:07.049433424 +0200
|
||||||
---
|
@@ -21,7 +21,7 @@
|
||||||
> tail: ?*Elephant = null, // <---- make this optional!
|
|
||||||
54c54
|
const Elephant = struct {
|
||||||
< if (e.tail == null) ???;
|
letter: u8,
|
||||||
---
|
- tail: *Elephant = null, // Hmm... tail needs something...
|
||||||
> if (e.tail == null) break;
|
+ tail: ?*Elephant = null, // <---- make this optional!
|
||||||
|
visited: bool = false,
|
||||||
|
};
|
||||||
|
|
||||||
|
@@ -51,7 +51,7 @@
|
||||||
|
// We should stop once we encounter a tail that
|
||||||
|
// does NOT point to another element. What can
|
||||||
|
// we put here to make that happen?
|
||||||
|
- if (e.tail == null) ???;
|
||||||
|
+ if (e.tail == null) break;
|
||||||
|
|
||||||
|
e = e.tail.?;
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
91c91
|
--- exercises/047_methods.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< ???.zap(???);
|
+++ answers/047_methods.zig 2023-10-05 20:04:07.056100214 +0200
|
||||||
---
|
@@ -88,7 +88,7 @@
|
||||||
> heat_ray.zap(alien);
|
for (&aliens) |*alien| {
|
||||||
|
|
||||||
|
// *** Zap the alien with the heat ray here! ***
|
||||||
|
- ???.zap(???);
|
||||||
|
+ heat_ray.zap(alien);
|
||||||
|
|
||||||
|
// If the alien's health is still above 0, it's still alive.
|
||||||
|
if (alien.health > 0) aliens_alive += 1;
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
57c57
|
--- exercises/048_methods2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< e = if (e.hasTail()) e.??? else break;
|
+++ answers/048_methods2.zig 2023-10-05 20:04:07.059433611 +0200
|
||||||
---
|
@@ -54,7 +54,7 @@
|
||||||
> e = if (e.hasTail()) e.getTail() else break;
|
|
||||||
|
// This gets the next elephant or stops:
|
||||||
|
// which method do we want here?
|
||||||
|
- e = if (e.hasTail()) e.??? else break;
|
||||||
|
+ e = if (e.hasTail()) e.getTail() else break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,18 @@
|
||||||
28a29,31
|
--- exercises/049_quiz6.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
> pub fn getTrunk(self: *Elephant) *Elephant {
|
+++ answers/049_quiz6.zig 2023-10-05 20:04:07.062767005 +0200
|
||||||
> return self.trunk.?;
|
@@ -26,9 +26,13 @@
|
||||||
> }
|
|
||||||
30,31c33,35
|
// Your Elephant trunk methods go here!
|
||||||
< ???
|
// ---------------------------------------------------
|
||||||
<
|
+ pub fn getTrunk(self: *Elephant) *Elephant {
|
||||||
---
|
+ return self.trunk.?;
|
||||||
> pub fn hasTrunk(self: *Elephant) bool {
|
+ }
|
||||||
> return (self.trunk != null);
|
|
||||||
> }
|
- ???
|
||||||
|
-
|
||||||
|
+ pub fn hasTrunk(self: *Elephant) bool {
|
||||||
|
+ return (self.trunk != null);
|
||||||
|
+ }
|
||||||
|
// ---------------------------------------------------
|
||||||
|
|
||||||
|
pub fn visit(self: *Elephant) void {
|
||||||
|
|
|
@ -1,14 +1,26 @@
|
||||||
68c68
|
--- exercises/050_no_value.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< var first_line1: *const [16]u8 = ???;
|
+++ answers/050_no_value.zig 2023-10-05 20:04:07.069433797 +0200
|
||||||
---
|
@@ -65,10 +65,10 @@
|
||||||
> var first_line1: *const [16]u8 = undefined;
|
const Err = error{Cthulhu};
|
||||||
71c71
|
|
||||||
< var first_line2: Err!*const [21]u8 = ???;
|
pub fn main() void {
|
||||||
---
|
- var first_line1: *const [16]u8 = ???;
|
||||||
> var first_line2: Err!*const [21]u8 = Err.Cthulhu;
|
+ var first_line1: *const [16]u8 = undefined;
|
||||||
80,81c80,81
|
first_line1 = "That is not dead";
|
||||||
< fn printSecondLine() ??? {
|
|
||||||
< var second_line2: ?*const [18]u8 = ???;
|
- var first_line2: Err!*const [21]u8 = ???;
|
||||||
---
|
+ var first_line2: Err!*const [21]u8 = Err.Cthulhu;
|
||||||
> fn printSecondLine() void {
|
first_line2 = "which can eternal lie";
|
||||||
> var second_line2: ?*const [18]u8 = null;
|
|
||||||
|
// Note we need the "{!s}" format for the error union string.
|
||||||
|
@@ -77,8 +77,8 @@
|
||||||
|
printSecondLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
-fn printSecondLine() ??? {
|
||||||
|
- var second_line2: ?*const [18]u8 = ???;
|
||||||
|
+fn printSecondLine() void {
|
||||||
|
+ var second_line2: ?*const [18]u8 = null;
|
||||||
|
second_line2 = "even death may die";
|
||||||
|
|
||||||
|
std.debug.print("And with strange aeons {s}.\n", .{second_line2.?});
|
||||||
|
|
|
@ -1,12 +1,27 @@
|
||||||
90c90
|
--- exercises/051_values.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const print = ???;
|
+++ answers/051_values.zig 2023-10-05 20:04:07.072767194 +0200
|
||||||
---
|
@@ -87,7 +87,7 @@
|
||||||
> const print = std.debug.print;
|
// Let's assign the std.debug.print function to a const named
|
||||||
155c155
|
// "print" so that we can use this new name later!
|
||||||
< levelUp(glorp, reward_xp);
|
|
||||||
---
|
- const print = ???;
|
||||||
> levelUp(&glorp, reward_xp);
|
+ const print = std.debug.print;
|
||||||
161c161
|
|
||||||
< fn levelUp(character_access: Character, xp: u32) void {
|
// Now let's look at assigning and pointing to values in Zig.
|
||||||
---
|
//
|
||||||
> fn levelUp(character_access: *Character, xp: u32) void {
|
@@ -152,13 +152,13 @@
|
||||||
|
print("XP before:{}, ", .{glorp.experience});
|
||||||
|
|
||||||
|
// Fix 1 of 2 goes here:
|
||||||
|
- levelUp(glorp, reward_xp);
|
||||||
|
+ levelUp(&glorp, reward_xp);
|
||||||
|
|
||||||
|
print("after:{}.\n", .{glorp.experience});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fix 2 of 2 goes here:
|
||||||
|
-fn levelUp(character_access: Character, xp: u32) void {
|
||||||
|
+fn levelUp(character_access: *Character, xp: u32) void {
|
||||||
|
character_access.experience += xp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,22 @@
|
||||||
35,36c35,36
|
--- exercises/052_slices.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const hand1: []u8 = cards[???];
|
+++ answers/052_slices.zig 2023-10-05 20:04:07.079433985 +0200
|
||||||
< const hand2: []u8 = cards[???];
|
@@ -32,8 +32,8 @@
|
||||||
---
|
var cards = [8]u8{ 'A', '4', 'K', '8', '5', '2', 'Q', 'J' };
|
||||||
> const hand1: []u8 = cards[0..4];
|
|
||||||
> const hand2: []u8 = cards[4..];
|
// Please put the first 4 cards in hand1 and the rest in hand2.
|
||||||
46c46
|
- const hand1: []u8 = cards[???];
|
||||||
< fn printHand(hand: ???) void {
|
- const hand2: []u8 = cards[???];
|
||||||
---
|
+ const hand1: []u8 = cards[0..4];
|
||||||
> fn printHand(hand: []u8) void {
|
+ const hand2: []u8 = cards[4..];
|
||||||
|
|
||||||
|
std.debug.print("Hand1: ", .{});
|
||||||
|
printHand(hand1);
|
||||||
|
@@ -43,7 +43,7 @@
|
||||||
|
}
|
||||||
|
|
||||||
|
// Please lend this function a hand. A u8 slice hand, that is.
|
||||||
|
-fn printHand(hand: ???) void {
|
||||||
|
+fn printHand(hand: []u8) void {
|
||||||
|
for (hand) |h| {
|
||||||
|
std.debug.print("{u} ", .{h});
|
||||||
|
}
|
||||||
|
|
|
@ -1,20 +1,29 @@
|
||||||
20,22c20,22
|
--- exercises/053_slices2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const base1: []u8 = scrambled[15..23];
|
+++ answers/053_slices2.zig 2023-10-05 20:04:07.082767381 +0200
|
||||||
< const base2: []u8 = scrambled[6..10];
|
@@ -17,19 +17,19 @@
|
||||||
< const base3: []u8 = scrambled[32..];
|
pub fn main() void {
|
||||||
---
|
const scrambled = "great base for all your justice are belong to us";
|
||||||
> const base1: []const u8 = scrambled[15..23];
|
|
||||||
> const base2: []const u8 = scrambled[6..10];
|
- const base1: []u8 = scrambled[15..23];
|
||||||
> const base3: []const u8 = scrambled[32..];
|
- const base2: []u8 = scrambled[6..10];
|
||||||
25,27c25,27
|
- const base3: []u8 = scrambled[32..];
|
||||||
< const justice1: []u8 = scrambled[11..14];
|
+ const base1: []const u8 = scrambled[15..23];
|
||||||
< const justice2: []u8 = scrambled[0..5];
|
+ const base2: []const u8 = scrambled[6..10];
|
||||||
< const justice3: []u8 = scrambled[24..31];
|
+ const base3: []const u8 = scrambled[32..];
|
||||||
---
|
printPhrase(base1, base2, base3);
|
||||||
> const justice1: []const u8 = scrambled[11..14];
|
|
||||||
> const justice2: []const u8 = scrambled[0..5];
|
- const justice1: []u8 = scrambled[11..14];
|
||||||
> const justice3: []const u8 = scrambled[24..31];
|
- const justice2: []u8 = scrambled[0..5];
|
||||||
33c33
|
- const justice3: []u8 = scrambled[24..31];
|
||||||
< fn printPhrase(part1: []u8, part2: []u8, part3: []u8) void {
|
+ const justice1: []const u8 = scrambled[11..14];
|
||||||
---
|
+ const justice2: []const u8 = scrambled[0..5];
|
||||||
> fn printPhrase(part1: []const u8, part2: []const u8, part3: []const u8) void {
|
+ const justice3: []const u8 = scrambled[24..31];
|
||||||
|
printPhrase(justice1, justice2, justice3);
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
-fn printPhrase(part1: []u8, part2: []u8, part3: []u8) void {
|
||||||
|
+fn printPhrase(part1: []const u8, part2: []const u8, part3: []const u8) void {
|
||||||
|
std.debug.print("'{s} {s} {s}.' ", .{ part1, part2, part3 });
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
35c35
|
--- exercises/054_manypointers.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const zen12_string: []const u8 = zen_manyptr;
|
+++ answers/054_manypointers.zig 2023-10-05 20:04:07.086100775 +0200
|
||||||
---
|
@@ -32,7 +32,7 @@
|
||||||
> const zen12_string: []const u8 = zen_manyptr[0..21];
|
// we can CONVERT IT TO A SLICE. (Hint: we do know the length!)
|
||||||
|
//
|
||||||
|
// Please fix this line so the print statement below can print it:
|
||||||
|
- const zen12_string: []const u8 = zen_manyptr;
|
||||||
|
+ const zen12_string: []const u8 = zen_manyptr[0..21];
|
||||||
|
|
||||||
|
// Here's the moment of truth!
|
||||||
|
std.debug.print("{s}\n", .{zen12_string});
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
62,63c62,63
|
--- exercises/055_unions.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< printInsect(ant, AntOrBee.c);
|
+++ answers/055_unions.zig 2023-10-05 20:04:07.092767568 +0200
|
||||||
< printInsect(bee, AntOrBee.c);
|
@@ -59,8 +59,8 @@
|
||||||
---
|
std.debug.print("Insect report! ", .{});
|
||||||
> printInsect(ant, AntOrBee.a);
|
|
||||||
> printInsect(bee, AntOrBee.b);
|
// Oops! We've made a mistake here.
|
||||||
|
- printInsect(ant, AntOrBee.c);
|
||||||
|
- printInsect(bee, AntOrBee.c);
|
||||||
|
+ printInsect(ant, AntOrBee.a);
|
||||||
|
+ printInsect(bee, AntOrBee.b);
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,20 @@
|
||||||
47,48c47,48
|
--- exercises/056_unions2.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< printInsect(???);
|
+++ answers/056_unions2.zig 2023-10-05 20:04:07.096100965 +0200
|
||||||
< printInsect(???);
|
@@ -44,14 +44,14 @@
|
||||||
---
|
std.debug.print("Insect report! ", .{});
|
||||||
> printInsect(ant);
|
|
||||||
> printInsect(bee);
|
// Could it really be as simple as just passing the union?
|
||||||
54c54
|
- printInsect(???);
|
||||||
< switch (???) {
|
- printInsect(???);
|
||||||
---
|
+ printInsect(ant);
|
||||||
> switch (insect) {
|
+ printInsect(bee);
|
||||||
|
|
||||||
|
std.debug.print("\n", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn printInsect(insect: Insect) void {
|
||||||
|
- switch (???) {
|
||||||
|
+ switch (insect) {
|
||||||
|
.still_alive => |a| std.debug.print("Ant alive is: {}. ", .{a}),
|
||||||
|
.flowers_visited => |f| std.debug.print("Bee visited {} flowers. ", .{f}),
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
18c18
|
--- exercises/057_unions3.zig 2023-10-03 22:15:22.122241138 +0200
|
||||||
< const Insect = union(InsectStat) {
|
+++ answers/057_unions3.zig 2023-10-05 20:04:07.099434359 +0200
|
||||||
---
|
@@ -15,7 +15,7 @@
|
||||||
> const Insect = union(enum) {
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
-const Insect = union(InsectStat) {
|
||||||
|
+const Insect = union(enum) {
|
||||||
|
flowers_visited: u16,
|
||||||
|
still_alive: bool,
|
||||||
|
};
|
||||||
|
|
|
@ -1,14 +1,31 @@
|
||||||
195,196c195,196
|
--- exercises/058_quiz7.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< .place => print("{s}", .{p.name}),
|
+++ answers/058_quiz7.zig 2023-10-05 20:04:07.106101152 +0200
|
||||||
< .path => print("--{}->", .{p.dist}),
|
@@ -192,8 +192,8 @@
|
||||||
---
|
// Oops! The hermit forgot how to capture the union values
|
||||||
> .place => |p| print("{s}", .{p.name}),
|
// in a switch statement. Please capture both values as
|
||||||
> .path => |p| print("--{}->", .{p.dist}),
|
// 'p' so the print statements work!
|
||||||
258c258
|
- .place => print("{s}", .{p.name}),
|
||||||
< if (place == entry.*.?.place) return entry;
|
- .path => print("--{}->", .{p.dist}),
|
||||||
---
|
+ .place => |p| print("{s}", .{p.name}),
|
||||||
> if (place == entry.*.?.place) return &entry.*.?;
|
+ .path => |p| print("--{}->", .{p.dist}),
|
||||||
312c312
|
}
|
||||||
< fn getTripTo(self: *HermitsNotebook, trip: []?TripItem, dest: *Place) void {
|
}
|
||||||
---
|
};
|
||||||
> fn getTripTo(self: *HermitsNotebook, trip: []?TripItem, dest: *Place) TripError!void {
|
@@ -255,7 +255,7 @@
|
||||||
|
// dereference and optional value "unwrapping" look
|
||||||
|
// together. Remember that you return the address with the
|
||||||
|
// "&" operator.
|
||||||
|
- if (place == entry.*.?.place) return entry;
|
||||||
|
+ if (place == entry.*.?.place) return &entry.*.?;
|
||||||
|
// Try to make your answer this long:__________;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
@@ -309,7 +309,7 @@
|
||||||
|
//
|
||||||
|
// Looks like the hermit forgot something in the return value of
|
||||||
|
// this function. What could that be?
|
||||||
|
- fn getTripTo(self: *HermitsNotebook, trip: []?TripItem, dest: *Place) void {
|
||||||
|
+ fn getTripTo(self: *HermitsNotebook, trip: []?TripItem, dest: *Place) TripError!void {
|
||||||
|
// We start at the destination entry.
|
||||||
|
const destination_entry = self.getEntry(dest);
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
23,25c23,25
|
--- exercises/059_integers.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< 0o131, // octal
|
+++ answers/059_integers.zig 2023-10-05 20:04:07.109434546 +0200
|
||||||
< 0b1101000, // binary
|
@@ -20,9 +20,9 @@
|
||||||
< 0x66, // hex
|
|
||||||
---
|
pub fn main() void {
|
||||||
> 0o132, // octal
|
const zig = [_]u8{
|
||||||
> 0b1101001, // binary
|
- 0o131, // octal
|
||||||
> 0x67, // hex
|
- 0b1101000, // binary
|
||||||
|
- 0x66, // hex
|
||||||
|
+ 0o132, // octal
|
||||||
|
+ 0b1101001, // binary
|
||||||
|
+ 0x67, // hex
|
||||||
|
};
|
||||||
|
|
||||||
|
print("{s} is cool.\n", .{zig});
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
46c46
|
--- exercises/060_floats.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const shuttle_weight: f16 = 907.18 * 2200;
|
+++ answers/060_floats.zig 2023-10-05 20:04:07.112767942 +0200
|
||||||
---
|
@@ -43,7 +43,7 @@
|
||||||
> const shuttle_weight: f32 = 907.18 * 2200.0;
|
//
|
||||||
|
// We'll convert this weight from tons to kilograms at a
|
||||||
|
// conversion of 907.18kg to the ton.
|
||||||
|
- const shuttle_weight: f16 = 907.18 * 2200;
|
||||||
|
+ const shuttle_weight: f32 = 907.18 * 2200.0;
|
||||||
|
|
||||||
|
// By default, float values are formatted in scientific
|
||||||
|
// notation. Try experimenting with '{d}' and '{d:.3}' to see
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
70c70
|
--- exercises/061_coercions.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const my_letter: ??? = &letter;
|
+++ answers/061_coercions.zig 2023-10-05 20:04:07.119434735 +0200
|
||||||
---
|
@@ -67,7 +67,7 @@
|
||||||
> const my_letter: ?*[1]u8 = &letter;
|
pub fn main() void {
|
||||||
|
var letter: u8 = 'A';
|
||||||
|
|
||||||
|
- const my_letter: ??? = &letter;
|
||||||
|
+ const my_letter: ?*[1]u8 = &letter;
|
||||||
|
// ^^^^^^^
|
||||||
|
// Your type here.
|
||||||
|
// Must coerce from &letter (which is a *u8).
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
50c50
|
--- exercises/062_loop_expressions.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< };
|
+++ answers/062_loop_expressions.zig 2023-10-05 20:04:07.122768129 +0200
|
||||||
---
|
@@ -47,7 +47,7 @@
|
||||||
> } else null;
|
// return it from the for loop.
|
||||||
|
const current_lang: ?[]const u8 = for (langs) |lang| {
|
||||||
|
if (lang.len == 3) break lang;
|
||||||
|
- };
|
||||||
|
+ } else null;
|
||||||
|
|
||||||
|
if (current_lang) |cl| {
|
||||||
|
print("Current language: {s}\n", .{cl});
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
131,132c131,132
|
--- exercises/063_labels.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< break;
|
+++ answers/063_labels.zig 2023-10-05 20:04:07.126101525 +0200
|
||||||
< };
|
@@ -128,8 +128,8 @@
|
||||||
---
|
// wanted for this Food.
|
||||||
> break food;
|
//
|
||||||
> } else menu[0];
|
// Please return this Food from the loop.
|
||||||
|
- break;
|
||||||
|
- };
|
||||||
|
+ break food;
|
||||||
|
+ } else menu[0];
|
||||||
|
// ^ Oops! We forgot to return Mac & Cheese as the default
|
||||||
|
// Food when the requested ingredients aren't found.
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,19 @@
|
||||||
66c66
|
--- exercises/064_builtins.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const expected_result: u8 = ???;
|
+++ answers/064_builtins.zig 2023-10-05 20:04:07.132768316 +0200
|
||||||
---
|
@@ -63,7 +63,7 @@
|
||||||
> const expected_result: u8 = 0b00010010;
|
//
|
||||||
81c81
|
// If there was no overflow at all while adding 5 to a, what value would
|
||||||
< const tupni: u8 = @bitReverse(input, tupni);
|
// 'my_result' hold? Write the answer in into 'expected_result'.
|
||||||
---
|
- const expected_result: u8 = ???;
|
||||||
> const tupni: u8 = @bitReverse(input);
|
+ const expected_result: u8 = 0b00010010;
|
||||||
|
print(". Without overflow: {b:0>8}. ", .{expected_result});
|
||||||
|
|
||||||
|
print("Furthermore, ", .{});
|
||||||
|
@@ -78,6 +78,6 @@
|
||||||
|
// Now it's your turn. See if you can fix this attempt to use
|
||||||
|
// this builtin to reverse the bits of a u8 integer.
|
||||||
|
const input: u8 = 0b11110000;
|
||||||
|
- const tupni: u8 = @bitReverse(input, tupni);
|
||||||
|
+ const tupni: u8 = @bitReverse(input);
|
||||||
|
print("{b:0>8} backwards is {b:0>8}.\n", .{ input, tupni });
|
||||||
|
}
|
||||||
|
|
|
@ -1,20 +1,39 @@
|
||||||
61c61
|
--- exercises/065_builtins2.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< narcissus.??? = ???;
|
+++ answers/065_builtins2.zig 2023-10-05 20:04:07.136101712 +0200
|
||||||
---
|
@@ -58,7 +58,7 @@
|
||||||
> narcissus.myself = &narcissus;
|
// Oops! We cannot leave the 'me' and 'myself' fields
|
||||||
73c73
|
// undefined. Please set them here:
|
||||||
< const Type2 = narcissus.fetchTheMostBeautifulType();
|
narcissus.me = &narcissus;
|
||||||
---
|
- narcissus.??? = ???;
|
||||||
> const Type2 = Narcissus.fetchTheMostBeautifulType();
|
+ narcissus.myself = &narcissus;
|
||||||
112c112
|
|
||||||
< if (fields[0].??? != void) {
|
// This determines a "peer type" from three separate
|
||||||
---
|
// references (they just happen to all be the same object).
|
||||||
> if (fields[0].type != void) {
|
@@ -70,7 +70,7 @@
|
||||||
116c116
|
//
|
||||||
< if (fields[1].??? != void) {
|
// The fix for this is very subtle, but it makes a big
|
||||||
---
|
// difference!
|
||||||
> if (fields[1].type != void) {
|
- const Type2 = narcissus.fetchTheMostBeautifulType();
|
||||||
120c120
|
+ const Type2 = Narcissus.fetchTheMostBeautifulType();
|
||||||
< if (fields[2].??? != void) {
|
|
||||||
---
|
// Now we print a pithy statement about Narcissus.
|
||||||
> if (fields[2].type != void) {
|
print("A {s} loves all {s}es. ", .{
|
||||||
|
@@ -109,15 +109,15 @@
|
||||||
|
// Please complete these 'if' statements so that the field
|
||||||
|
// name will not be printed if the field is of type 'void'
|
||||||
|
// (which is a zero-bit type that takes up no space at all!):
|
||||||
|
- if (fields[0].??? != void) {
|
||||||
|
+ if (fields[0].type != void) {
|
||||||
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[0].name});
|
||||||
|
}
|
||||||
|
|
||||||
|
- if (fields[1].??? != void) {
|
||||||
|
+ if (fields[1].type != void) {
|
||||||
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[1].name});
|
||||||
|
}
|
||||||
|
|
||||||
|
- if (fields[2].??? != void) {
|
||||||
|
+ if (fields[2].type != void) {
|
||||||
|
print(" {s}", .{@typeInfo(Narcissus).Struct.fields[2].name});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
65,66c65,66
|
--- exercises/066_comptime.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var var_int = 12345;
|
+++ answers/066_comptime.zig 2023-10-05 20:04:07.139435109 +0200
|
||||||
< var var_float = 987.654;
|
@@ -62,8 +62,8 @@
|
||||||
---
|
// types with specific sizes. The comptime numbers will be
|
||||||
> var var_int: u32 = 12345;
|
// coerced (if they'll fit!) into your chosen runtime types.
|
||||||
> var var_float: f32 = 987.654;
|
// For this it is necessary to specify a size, e.g. 32 bit.
|
||||||
|
- var var_int = 12345;
|
||||||
|
- var var_float = 987.654;
|
||||||
|
+ var var_int: u32 = 12345;
|
||||||
|
+ var var_float: f32 = 987.654;
|
||||||
|
|
||||||
|
// We can change what is stored at the areas set aside for
|
||||||
|
// "var_int" and "var_float" in the running compiled program.
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
38c38
|
--- exercises/067_comptime2.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var count = 0;
|
+++ answers/067_comptime2.zig 2023-10-05 20:04:07.146101899 +0200
|
||||||
---
|
@@ -35,7 +35,7 @@
|
||||||
> comptime var count = 0;
|
// In this contrived example, we've decided to allocate some
|
||||||
|
// arrays using a variable count! But something's missing...
|
||||||
|
//
|
||||||
|
- var count = 0;
|
||||||
|
+ comptime var count = 0;
|
||||||
|
|
||||||
|
count += 1;
|
||||||
|
var a1: [count]u8 = .{'A'} ** count;
|
||||||
|
|
|
@ -1,8 +1,20 @@
|
||||||
46c46
|
--- exercises/068_comptime3.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< if (my_scale == 0) @compileError("Scale 1:0 is not valid!");
|
+++ answers/068_comptime3.zig 2023-10-05 20:04:07.149435295 +0200
|
||||||
---
|
@@ -43,7 +43,7 @@
|
||||||
> if (my_scale == 0) my_scale = 1; //@compileError("Scale 1:0 is not valid!");
|
//
|
||||||
72c72
|
// Please change this so that it sets a 0 scale to 1
|
||||||
< var scale: u32 = undefined;
|
// instead.
|
||||||
---
|
- if (my_scale == 0) @compileError("Scale 1:0 is not valid!");
|
||||||
> comptime var scale: u32 = undefined;
|
+ if (my_scale == 0) my_scale = 1; //@compileError("Scale 1:0 is not valid!");
|
||||||
|
|
||||||
|
self.scale = my_scale;
|
||||||
|
self.hull_length /= my_scale;
|
||||||
|
@@ -69,7 +69,7 @@
|
||||||
|
// Hey, we can't just pass this runtime variable as an
|
||||||
|
// argument to the scaleMe() method. What would let us do
|
||||||
|
// that?
|
||||||
|
- var scale: u32 = undefined;
|
||||||
|
+ comptime var scale: u32 = undefined;
|
||||||
|
|
||||||
|
scale = 32; // 1:32 scale
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
45,46c45,46
|
--- exercises/069_comptime4.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< fn makeSequence(comptime T: type, ??? size: usize) [???]T {
|
+++ answers/069_comptime4.zig 2023-10-05 20:04:07.152768692 +0200
|
||||||
< var sequence: [???]T = undefined;
|
@@ -42,8 +42,8 @@
|
||||||
---
|
// 2) Sets the size of the array of type T (which is the
|
||||||
> fn makeSequence(comptime T: type, comptime size: usize) [size]T {
|
// sequence we're creating and returning).
|
||||||
> var sequence: [size]T = undefined;
|
//
|
||||||
|
-fn makeSequence(comptime T: type, ??? size: usize) [???]T {
|
||||||
|
- var sequence: [???]T = undefined;
|
||||||
|
+fn makeSequence(comptime T: type, comptime size: usize) [size]T {
|
||||||
|
+ var sequence: [size]T = undefined;
|
||||||
|
var i: usize = 0;
|
||||||
|
|
||||||
|
while (i < size) : (i += 1) {
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
126,127c126,127
|
--- exercises/070_comptime5.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const walks_like_duck = ???;
|
+++ answers/070_comptime5.zig 2023-10-05 20:04:07.159435482 +0200
|
||||||
< const quacks_like_duck = ???;
|
@@ -123,8 +123,8 @@
|
||||||
---
|
// Please make sure MyType has both waddle() and quack()
|
||||||
> const walks_like_duck = @hasDecl(MyType, "waddle");
|
// methods:
|
||||||
> const quacks_like_duck = @hasDecl(MyType, "quack");
|
const MyType = @TypeOf(possible_duck);
|
||||||
|
- const walks_like_duck = ???;
|
||||||
|
- const quacks_like_duck = ???;
|
||||||
|
+ const walks_like_duck = @hasDecl(MyType, "waddle");
|
||||||
|
+ const quacks_like_duck = @hasDecl(MyType, "quack");
|
||||||
|
|
||||||
|
const is_duck = walks_like_duck and quacks_like_duck;
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
43c43
|
--- exercises/071_comptime6.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ??? {
|
+++ answers/071_comptime6.zig 2023-10-05 20:04:07.162768879 +0200
|
||||||
---
|
@@ -40,7 +40,7 @@
|
||||||
> inline for (fields) |field| {
|
|
||||||
|
const fields = @typeInfo(Narcissus).Struct.fields;
|
||||||
|
|
||||||
|
- ??? {
|
||||||
|
+ inline for (fields) |field| {
|
||||||
|
if (field.type != void) {
|
||||||
|
print(" {s}", .{field.name});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
38c38
|
--- exercises/072_comptime7.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ??? (i < instructions.len) : (???) {
|
+++ answers/072_comptime7.zig 2023-10-05 20:04:07.169435669 +0200
|
||||||
---
|
@@ -35,7 +35,7 @@
|
||||||
> inline while (i < instructions.len) : (i += 3) {
|
// at compile time.
|
||||||
|
//
|
||||||
|
// Please fix this to loop once per "instruction":
|
||||||
|
- ??? (i < instructions.len) : (???) {
|
||||||
|
+ inline while (i < instructions.len) : (i += 3) {
|
||||||
|
|
||||||
|
// This gets the digit from the "instruction". Can you
|
||||||
|
// figure out why we subtract '0' from it?
|
||||||
|
|
|
@ -1,8 +1,17 @@
|
||||||
35c35
|
--- exercises/073_comptime8.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const my_llama = getLlama(5);
|
+++ answers/073_comptime8.zig 2023-10-05 20:04:07.172769065 +0200
|
||||||
---
|
@@ -32,12 +32,12 @@
|
||||||
> const my_llama = getLlama(4);
|
pub fn main() void {
|
||||||
40c40
|
// We meant to fetch the last llama. Please fix this simple
|
||||||
< fn getLlama(i: usize) u32 {
|
// mistake so the assertion no longer fails.
|
||||||
---
|
- const my_llama = getLlama(5);
|
||||||
> fn getLlama(comptime i: usize) u32 {
|
+ const my_llama = getLlama(4);
|
||||||
|
|
||||||
|
print("My llama value is {}.\n", .{my_llama});
|
||||||
|
}
|
||||||
|
|
||||||
|
-fn getLlama(i: usize) u32 {
|
||||||
|
+fn getLlama(comptime i: usize) u32 {
|
||||||
|
// We've put a guard assert() at the top of this function to
|
||||||
|
// prevent mistakes. The 'comptime' keyword here means that
|
||||||
|
// the mistake will be caught when we compile!
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
42c42
|
--- exercises/074_comptime9.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< fn makeLlamas(count: usize) [count]u8 {
|
+++ answers/074_comptime9.zig 2023-10-05 20:04:07.176102462 +0200
|
||||||
---
|
@@ -39,7 +39,7 @@
|
||||||
> fn makeLlamas(comptime count: usize) [count]u8 {
|
|
||||||
|
// And here's the function. Note that the return value type
|
||||||
|
// depends on one of the input arguments!
|
||||||
|
-fn makeLlamas(count: usize) [count]u8 {
|
||||||
|
+fn makeLlamas(comptime count: usize) [count]u8 {
|
||||||
|
var temp: [count]u8 = undefined;
|
||||||
|
var i = 0;
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
52c52,56
|
--- exercises/075_quiz8.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
<
|
+++ answers/075_quiz8.zig 2023-10-05 20:04:07.182769252 +0200
|
||||||
---
|
@@ -49,7 +49,11 @@
|
||||||
> return Path{
|
//
|
||||||
> .from = from,
|
// Please fill in the body of this function!
|
||||||
> .to = to,
|
fn makePath(from: *Place, to: *Place, dist: u8) Path {
|
||||||
> .dist = dist,
|
-
|
||||||
> };
|
+ return Path{
|
||||||
|
+ .from = from,
|
||||||
|
+ .to = to,
|
||||||
|
+ .dist = dist,
|
||||||
|
+ };
|
||||||
|
}
|
||||||
|
|
||||||
|
// Using our new function, these path definitions take up considerably less
|
||||||
|
|
|
@ -1,8 +1,20 @@
|
||||||
85c85
|
--- exercises/076_sentinels.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< for (???) |s| {
|
+++ answers/076_sentinels.zig 2023-10-05 20:04:07.186102649 +0200
|
||||||
---
|
@@ -82,7 +82,7 @@
|
||||||
> for (my_seq) |s| {
|
print("Array:", .{});
|
||||||
97c97
|
|
||||||
< while (??? != my_sentinel) {
|
// Loop through the items in my_seq.
|
||||||
---
|
- for (???) |s| {
|
||||||
> while (my_seq[i] != my_sentinel) {
|
+ for (my_seq) |s| {
|
||||||
|
print("{}", .{s});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
@@ -94,7 +94,7 @@
|
||||||
|
// Loop through the items in my_seq until we hit the
|
||||||
|
// sentinel value.
|
||||||
|
var i: usize = 0;
|
||||||
|
- while (??? != my_sentinel) {
|
||||||
|
+ while (my_seq[i] != my_sentinel) {
|
||||||
|
print("{}", .{my_seq[i]});
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
63c63
|
--- exercises/077_sentinels2.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const printable = ???;
|
+++ answers/077_sentinels2.zig 2023-10-05 20:04:07.189436043 +0200
|
||||||
---
|
@@ -60,7 +60,7 @@
|
||||||
> const printable = foo.data[0..foo.length];
|
// length... You've actually solved this problem before!
|
||||||
|
//
|
||||||
|
// Here's a big hint: do you remember how to take a slice?
|
||||||
|
- const printable = ???;
|
||||||
|
+ const printable = foo.data[0..foo.length];
|
||||||
|
|
||||||
|
print("{s}\n", .{printable});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
24c24
|
--- exercises/078_sentinels3.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const printable: [*:0]const u8 = ???;
|
+++ answers/078_sentinels3.zig 2023-10-05 20:04:07.196102836 +0200
|
||||||
---
|
@@ -21,7 +21,7 @@
|
||||||
> const printable: [*:0]const u8 = @ptrCast(data);
|
const data: [*]const u8 = "Weird Data!";
|
||||||
|
|
||||||
|
// Please cast 'data' to 'printable':
|
||||||
|
- const printable: [*:0]const u8 = ???;
|
||||||
|
+ const printable: [*:0]const u8 = @ptrCast(data);
|
||||||
|
|
||||||
|
print("{s}\n", .{printable});
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
23,24c23,24
|
--- exercises/079_quoted_identifiers.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const 55_cows: i32 = 55;
|
+++ answers/079_quoted_identifiers.zig 2023-10-05 20:04:07.199436232 +0200
|
||||||
< const isn't true: bool = false;
|
@@ -20,11 +20,11 @@
|
||||||
---
|
const print = @import("std").debug.print;
|
||||||
> const @"55_cows": i32 = 55;
|
|
||||||
> const @"isn't true": bool = false;
|
pub fn main() void {
|
||||||
27,28c27,28
|
- const 55_cows: i32 = 55;
|
||||||
< 55_cows,
|
- const isn't true: bool = false;
|
||||||
< isn't true,
|
+ const @"55_cows": i32 = 55;
|
||||||
---
|
+ const @"isn't true": bool = false;
|
||||||
> @"55_cows",
|
|
||||||
> @"isn't true",
|
print("Sweet freedom: {}, {}.\n", .{
|
||||||
|
- 55_cows,
|
||||||
|
- isn't true,
|
||||||
|
+ @"55_cows",
|
||||||
|
+ @"isn't true",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,18 @@
|
||||||
51c51
|
--- exercises/080_anonymous_structs.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var circle1 = ??? {
|
+++ answers/080_anonymous_structs.zig 2023-10-05 20:04:07.202769626 +0200
|
||||||
---
|
@@ -48,13 +48,13 @@
|
||||||
> var circle1 = Circle(i32){
|
// * circle1 should hold i32 integers
|
||||||
57c57
|
// * circle2 should hold f32 floats
|
||||||
< var circle2 = ??? {
|
//
|
||||||
---
|
- var circle1 = ??? {
|
||||||
> var circle2 = Circle(f32){
|
+ var circle1 = Circle(i32){
|
||||||
|
.center_x = 25,
|
||||||
|
.center_y = 70,
|
||||||
|
.radius = 15,
|
||||||
|
};
|
||||||
|
|
||||||
|
- var circle2 = ??? {
|
||||||
|
+ var circle2 = Circle(f32){
|
||||||
|
.center_x = 25.234,
|
||||||
|
.center_y = 70.999,
|
||||||
|
.radius = 15.714,
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
41c41
|
--- exercises/081_anonymous_structs2.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< fn printCircle(???) void {
|
+++ answers/081_anonymous_structs2.zig 2023-10-05 20:04:07.209436419 +0200
|
||||||
---
|
@@ -38,7 +38,7 @@
|
||||||
> fn printCircle(circle: anytype) void {
|
|
||||||
|
// Please complete this function which prints an anonymous struct
|
||||||
|
// representing a circle.
|
||||||
|
-fn printCircle(???) void {
|
||||||
|
+fn printCircle(circle: anytype) void {
|
||||||
|
print("x:{} y:{} radius:{}\n", .{
|
||||||
|
circle.center_x,
|
||||||
|
circle.center_y,
|
||||||
|
|
|
@ -1,16 +1,32 @@
|
||||||
85c85
|
--- exercises/082_anonymous_structs3.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const fields = ???;
|
+++ answers/082_anonymous_structs3.zig 2023-10-05 20:04:07.212769813 +0200
|
||||||
---
|
@@ -82,14 +82,14 @@
|
||||||
> const fields = @typeInfo(@TypeOf(tuple)).Struct.fields;
|
// @typeInfo(Circle).Struct.fields
|
||||||
92c92
|
//
|
||||||
< for (fields) |field| {
|
// This will be an array of StructFields.
|
||||||
---
|
- const fields = ???;
|
||||||
> inline for (fields) |field| {
|
+ const fields = @typeInfo(@TypeOf(tuple)).Struct.fields;
|
||||||
120,122c120,122
|
|
||||||
< field.???,
|
// 2. Loop through each field. This must be done at compile
|
||||||
< field.???,
|
// time.
|
||||||
< ???,
|
//
|
||||||
---
|
// Hint: remember 'inline' loops?
|
||||||
> field.name,
|
//
|
||||||
> field.type,
|
- for (fields) |field| {
|
||||||
> @field(tuple, field.name),
|
+ inline for (fields) |field| {
|
||||||
|
// 3. Print the field's name, type, and value.
|
||||||
|
//
|
||||||
|
// Each 'field' in this loop is one of these:
|
||||||
|
@@ -117,9 +117,9 @@
|
||||||
|
//
|
||||||
|
// The first field should print as: "0"(bool):true
|
||||||
|
print("\"{s}\"({any}):{any} ", .{
|
||||||
|
- field.???,
|
||||||
|
- field.???,
|
||||||
|
- ???,
|
||||||
|
+ field.name,
|
||||||
|
+ field.type,
|
||||||
|
+ @field(tuple, field.name),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,10 @@
|
||||||
23c23
|
--- exercises/083_anonymous_lists.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const hello = .{ 'h', 'e', 'l', 'l', 'o' };
|
+++ answers/083_anonymous_lists.zig 2023-10-05 20:04:07.216103210 +0200
|
||||||
---
|
@@ -20,6 +20,6 @@
|
||||||
> const hello: [5]u8 = .{ 'h', 'e', 'l', 'l', 'o' };
|
//
|
||||||
|
// = .{ 'h', 'e', 'l', 'l', 'o' };
|
||||||
|
//
|
||||||
|
- const hello = .{ 'h', 'e', 'l', 'l', 'o' };
|
||||||
|
+ const hello: [5]u8 = .{ 'h', 'e', 'l', 'l', 'o' };
|
||||||
|
print("I say {s}!\n", .{hello});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
51c51
|
--- exercises/084_async.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< foo();
|
+++ answers/084_async.zig 2023-10-05 20:04:07.219436606 +0200
|
||||||
---
|
@@ -48,7 +48,7 @@
|
||||||
> _ = async foo();
|
pub fn main() void {
|
||||||
|
// Additional Hint: you can assign things to '_' when you
|
||||||
|
// don't intend to do anything with them.
|
||||||
|
- foo();
|
||||||
|
+ _ = async foo();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() void {
|
||||||
|
|
|
@ -1,2 +1,10 @@
|
||||||
21a22
|
--- exercises/085_async2.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
> resume foo_frame;
|
+++ answers/085_async2.zig 2023-10-05 20:04:07.226103397 +0200
|
||||||
|
@@ -19,6 +19,7 @@
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
var foo_frame = async foo();
|
||||||
|
+ resume foo_frame;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() void {
|
||||||
|
|
|
@ -1,9 +1,16 @@
|
||||||
16c16,21
|
--- exercises/086_async3.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ???
|
+++ answers/086_async3.zig 2023-10-05 20:04:07.229436793 +0200
|
||||||
---
|
@@ -13,7 +13,12 @@
|
||||||
> // Silly solution. You can also use a loop.
|
const n = 5;
|
||||||
> resume foo_frame;
|
var foo_frame = async foo(n);
|
||||||
> resume foo_frame;
|
|
||||||
> resume foo_frame;
|
- ???
|
||||||
> resume foo_frame;
|
+ // Silly solution. You can also use a loop.
|
||||||
> resume foo_frame;
|
+ resume foo_frame;
|
||||||
|
+ resume foo_frame;
|
||||||
|
+ resume foo_frame;
|
||||||
|
+ resume foo_frame;
|
||||||
|
+ resume foo_frame;
|
||||||
|
|
||||||
|
print("\n", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,21 @@
|
||||||
19c19
|
--- exercises/087_async4.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ???
|
+++ answers/087_async4.zig 2023-10-05 20:04:07.236103584 +0200
|
||||||
---
|
@@ -16,7 +16,7 @@
|
||||||
> resume foo_frame;
|
|
||||||
27,28c27,28
|
while (global_counter <= 5) {
|
||||||
< ???
|
print("{} ", .{global_counter});
|
||||||
< ???
|
- ???
|
||||||
---
|
+ resume foo_frame;
|
||||||
> global_counter += 1;
|
}
|
||||||
> suspend {}
|
|
||||||
|
print("\n", .{});
|
||||||
|
@@ -24,7 +24,7 @@
|
||||||
|
|
||||||
|
fn foo() void {
|
||||||
|
while (true) {
|
||||||
|
- ???
|
||||||
|
- ???
|
||||||
|
+ global_counter += 1;
|
||||||
|
+ suspend {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
39c39
|
--- exercises/088_async5.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var value = ???
|
+++ answers/088_async5.zig 2023-10-05 20:04:07.239436980 +0200
|
||||||
---
|
@@ -36,7 +36,7 @@
|
||||||
> var value = await myframe;
|
pub fn main() void {
|
||||||
|
var myframe = async getPageTitle("http://example.com");
|
||||||
|
|
||||||
|
- var value = ???
|
||||||
|
+ var value = await myframe;
|
||||||
|
|
||||||
|
print("{s}\n", .{value});
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
44,45c44,45
|
--- exercises/089_async6.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var com_title = com_frame;
|
+++ answers/089_async6.zig 2023-10-05 20:04:07.242770376 +0200
|
||||||
< var org_title = org_frame;
|
@@ -41,8 +41,8 @@
|
||||||
---
|
var com_frame = async getPageTitle("http://example.com");
|
||||||
> var com_title = await com_frame;
|
var org_frame = async getPageTitle("http://example.org");
|
||||||
> var org_title = await org_frame;
|
|
||||||
|
- var com_title = com_frame;
|
||||||
|
- var org_title = org_frame;
|
||||||
|
+ var com_title = await com_frame;
|
||||||
|
+ var org_title = await org_frame;
|
||||||
|
|
||||||
|
print(".com: {s}, .org: {s}.\n", .{ com_title, org_title });
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
32c32
|
--- exercises/090_async7.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var my_beef = getBeef(0);
|
+++ answers/090_async7.zig 2023-10-05 20:04:07.249437167 +0200
|
||||||
---
|
@@ -29,7 +29,7 @@
|
||||||
> var my_beef = nosuspend getBeef(0);
|
// The main() function can not be async. But we know
|
||||||
|
// getBeef() will not suspend with this particular
|
||||||
|
// invocation. Please make this okay:
|
||||||
|
- var my_beef = getBeef(0);
|
||||||
|
+ var my_beef = nosuspend getBeef(0);
|
||||||
|
|
||||||
|
print("beef? {X}!\n", .{my_beef});
|
||||||
|
}
|
||||||
|
|
|
@ -1,16 +1,26 @@
|
||||||
20c20
|
--- exercises/091_async8.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< print("X", .{});
|
+++ answers/091_async8.zig 2023-10-05 20:04:07.252770563 +0200
|
||||||
---
|
@@ -17,7 +17,7 @@
|
||||||
> print("D", .{});
|
|
||||||
28c28
|
var frame = async suspendable();
|
||||||
< print("X", .{});
|
|
||||||
---
|
- print("X", .{});
|
||||||
> print("B", .{});
|
+ print("D", .{});
|
||||||
31c31
|
|
||||||
< print("X", .{});
|
resume frame;
|
||||||
---
|
|
||||||
> print("C", .{});
|
@@ -25,11 +25,11 @@
|
||||||
34c34
|
}
|
||||||
< print("X", .{});
|
|
||||||
---
|
fn suspendable() void {
|
||||||
> print("E", .{});
|
- print("X", .{});
|
||||||
|
+ print("B", .{});
|
||||||
|
|
||||||
|
suspend {
|
||||||
|
- print("X", .{});
|
||||||
|
+ print("C", .{});
|
||||||
|
}
|
||||||
|
|
||||||
|
- print("X", .{});
|
||||||
|
+ print("E", .{});
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
109c109
|
--- exercises/092_interfaces.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ???
|
+++ answers/092_interfaces.zig 2023-10-05 20:04:07.259437354 +0200
|
||||||
---
|
@@ -106,7 +106,7 @@
|
||||||
> insect.print();
|
for (my_insects) |insect| {
|
||||||
|
// Almost done! We want to print() each insect with a
|
||||||
|
// single method call here.
|
||||||
|
- ???
|
||||||
|
+ insect.print();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
57c57
|
--- exercises/093_hello_c.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< const c_res = write(2, "Hello C from Zig!", 17);
|
+++ answers/093_hello_c.zig 2023-10-05 20:04:07.262770750 +0200
|
||||||
---
|
@@ -54,7 +54,7 @@
|
||||||
> const c_res = c.write(2, "Hello C from Zig!", 17);
|
//
|
||||||
|
// In this exercise we use 'write' to output 17 chars,
|
||||||
|
// but something is still missing...
|
||||||
|
- const c_res = write(2, "Hello C from Zig!", 17);
|
||||||
|
+ const c_res = c.write(2, "Hello C from Zig!", 17);
|
||||||
|
|
||||||
|
// let's see what the result from C is:
|
||||||
|
std.debug.print(" - C result is {d} chars written.\n", .{c_res});
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
22c22
|
--- exercises/094_c_math.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ???
|
+++ answers/094_c_math.zig 2023-10-05 20:04:07.266104147 +0200
|
||||||
---
|
@@ -19,7 +19,7 @@
|
||||||
> @cInclude("math.h");
|
|
||||||
|
const c = @cImport({
|
||||||
|
// What do wee need here?
|
||||||
|
- ???
|
||||||
|
+ @cInclude("math.h");
|
||||||
|
});
|
||||||
|
|
||||||
|
pub fn main() !void {
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
57c57
|
--- exercises/095_for3.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< for (???) |n| {
|
+++ answers/095_for3.zig 2023-10-05 20:04:07.272770937 +0200
|
||||||
---
|
@@ -54,7 +54,7 @@
|
||||||
> for (1..21) |n| {
|
|
||||||
|
// I want to print every number between 1 and 20 that is NOT
|
||||||
|
// divisible by 3 or 5.
|
||||||
|
- for (???) |n| {
|
||||||
|
+ for (1..21) |n| {
|
||||||
|
|
||||||
|
// The '%' symbol is the "modulo" operator and it
|
||||||
|
// returns the remainder after division.
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
67c67
|
--- exercises/096_memory_allocation.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< var avg: []f64 = ???;
|
+++ answers/096_memory_allocation.zig 2023-10-05 20:04:07.276104333 +0200
|
||||||
---
|
@@ -64,7 +64,7 @@
|
||||||
> var avg: []f64 = try allocator.alloc(f64, arr.len);
|
const allocator = arena.allocator();
|
||||||
|
|
||||||
|
// allocate memory for this array
|
||||||
|
- var avg: []f64 = ???;
|
||||||
|
+ var avg: []f64 = try allocator.alloc(f64, arr.len);
|
||||||
|
|
||||||
|
runningAverage(arr, avg);
|
||||||
|
std.debug.print("Running Average: ", .{});
|
||||||
|
|
|
@ -1,4 +1,11 @@
|
||||||
83c83
|
--- exercises/097_bit_manipulation.zig 2023-10-03 22:15:22.125574535 +0200
|
||||||
< ???;
|
+++ answers/097_bit_manipulation.zig 2023-10-05 20:04:07.282771124 +0200
|
||||||
---
|
@@ -80,7 +80,7 @@
|
||||||
> x ^= y;
|
y ^= x;
|
||||||
|
|
||||||
|
// What must be written here?
|
||||||
|
- ???;
|
||||||
|
+ x ^= y;
|
||||||
|
|
||||||
|
print("x = {d}; y = {d}\n", .{ x, y });
|
||||||
|
}
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue