Merge pull request 'fixed removed array multiplication' (#423) from array_mult into main

Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/423
This commit is contained in:
Chris Boesch
2026-05-04 17:30:31 +02:00
14 changed files with 51 additions and 64 deletions

View File

@@ -777,7 +777,7 @@ const exercises = [_]Exercise{
}, },
.{ .{
.main_file = "006_strings.zig", .main_file = "006_strings.zig",
.output = "d=d ha ha ha Major Tom", .output = "d=d Major Tom",
.hint = "Each '???' needs something filled in.", .hint = "Each '???' needs something filled in.",
}, },
.{ .{

View File

@@ -1,5 +1,5 @@
// //
// Zig has some fun array operators. // Zig has one array operators.
// //
// You can use '++' to concatenate two arrays: // You can use '++' to concatenate two arrays:
// //
@@ -7,12 +7,8 @@
// const b = [_]u8{ 3,4 }; // const b = [_]u8{ 3,4 };
// const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5 // const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5
// //
// You can use '**' to repeat an array: // Note that '++'' only operate on arrays while your program is
// // _being compiled_. This special time is known in Zig
// const d = [_]u8{ 1,2,3 } ** 2; // equals 1 2 3 1 2 3
//
// Note that both '++' and '**' only operate on arrays while your
// program is _being compiled_. This special time is known in Zig
// parlance as "comptime" and we'll learn plenty more about that // parlance as "comptime" and we'll learn plenty more about that
// later. // later.
// //
@@ -30,7 +26,8 @@ pub fn main() void {
// (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_unit = [_]u8{ ??? };
const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit)));
// Okay, that's all of the problems. Let's see the results. // Okay, that's all of the problems. Let's see the results.
// //

View File

@@ -27,10 +27,6 @@ pub fn main() void {
const d: u8 = ziggy[???]; const d: u8 = ziggy[???];
// (Problem 2) // (Problem 2)
// Use the array repeat '**' operator to make "ha ha ha ".
const laugh = "ha " ???;
// (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";
@@ -38,7 +34,7 @@ pub fn main() void {
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}\n", .{ d, major_tom });
// Keen eyes will notice that we've put 'u' and 's' inside the '{}' // Keen eyes will notice that we've put 'u' and 's' inside the '{}'
// placeholders in the format string above. This tells the // placeholders in the format string above. This tells the
// print() function to format the values as a UTF-8 character and // print() function to format the values as a UTF-8 character and

View File

@@ -224,11 +224,10 @@ const NotebookEntry = struct {
// +---+----------------+----------------+----------+ // +---+----------------+----------------+----------+
// //
const HermitsNotebook = struct { const HermitsNotebook = struct {
// Remember the array repetition operator `**`? It is no mere // Remember the array repetition function @splat()? It is a great way
// novelty, it's also a great way to assign multiple items in an // to assign multiple items in an array without having to list them
// array without having to list them one by one. Here we use it to // one by one. Here we use it to initialize an array with null values.
// initialize an array with null values. entries: [place_count]?NotebookEntry = @splat(null),
entries: [place_count]?NotebookEntry = .{null} ** place_count,
// The next entry keeps track of where we are in our "todo" list. // The next entry keeps track of where we are in our "todo" list.
next_entry: u8 = 0, next_entry: u8 = 0,
@@ -409,7 +408,7 @@ pub fn main() void {
// aside memory for the trip and have the hermit's notebook fill // aside memory for the trip and have the hermit's notebook fill
// in the trip from the destination back to the path. Note that // in the trip from the destination back to the path. Note that
// this is the first time we've actually used the destination! // this is the first time we've actually used the destination!
var trip = [_]?TripItem{null} ** (place_count * 2); var trip: [place_count * 2]?TripItem = @splat(null);
notebook.getTripTo(trip[0..], destination) catch |err| { notebook.getTripTo(trip[0..], destination) catch |err| {
print("Oh no! {}\n", .{err}); print("Oh no! {}\n", .{err});

View File

@@ -39,16 +39,16 @@ pub fn main() void {
var count = 0; var count = 0;
count += 1; count += 1;
const a1: [count]u8 = .{'A'} ** count; const a1: [count]u8 = @splat('A');
count += 1; count += 1;
const a2: [count]u8 = .{'B'} ** count; const a2: [count]u8 = @splat('B');
count += 1; count += 1;
const a3: [count]u8 = .{'C'} ** count; const a3: [count]u8 = @splat('C');
count += 1; count += 1;
const a4: [count]u8 = .{'D'} ** count; const a4: [count]u8 = @splat('D');
print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 }); print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 });

View File

@@ -32,7 +32,7 @@ fn makeCreature(comptime count: usize, comptime fmt: []const u8) [count]Animal {
// We return an array of animals representing the creature. (This is why we // We return an array of animals representing the creature. (This is why we
// really needed the 'count' parameter. Arrays need a size.) // really needed the 'count' parameter. Arrays need a size.)
var animals: [count]Animal = .{undefined} ** count; var animals: [count]Animal = undefined;
var next_animal: usize = 0; var next_animal: usize = 0;
inline for (fmt) |char| { inline for (fmt) |char| {

View File

@@ -48,9 +48,7 @@ const Path = struct {
// instead. // instead.
// //
// Please fill in the body of this function! // Please fill in the body of this function!
fn makePath(from: *Place, to: *Place, dist: u8) Path { fn makePath(from: *Place, to: *Place, dist: u8) Path {}
}
// Using our new function, these path definitions take up considerably less // Using our new function, these path definitions take up considerably less
// space in our program now! // space in our program now!
@@ -97,7 +95,7 @@ const NotebookEntry = struct {
}; };
const HermitsNotebook = struct { const HermitsNotebook = struct {
entries: [place_count]?NotebookEntry = .{null} ** place_count, entries: [place_count]?NotebookEntry = @splat(null),
next_entry: u8 = 0, next_entry: u8 = 0,
end_of_entries: u8 = 0, end_of_entries: u8 = 0,
@@ -193,7 +191,7 @@ pub fn main() void {
} }
} }
var trip = [_]?TripItem{null} ** (place_count * 2); var trip: [place_count * 2]?TripItem = @splat(null);
notebook.getTripTo(trip[0..], destination) catch |err| { notebook.getTripTo(trip[0..], destination) catch |err| {
print("Oh no! {}\n", .{err}); print("Oh no! {}\n", .{err});

View File

@@ -1,6 +1,6 @@
--- exercises/005_arrays2.zig 2023-10-03 22:15:22.122241138 +0200 --- exercises/005_arrays2.zig 2026-05-04 16:26:32.778330847 +0200
+++ answers/005_arrays2.zig 2023-10-05 20:04:06.862763262 +0200 +++ answers/005_arrays2.zig 2026-05-04 16:26:13.082917974 +0200
@@ -25,12 +25,12 @@ @@ -21,12 +21,12 @@
// (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
@@ -10,8 +10,8 @@
// (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_unit = [_]u8{ ??? };
+ const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3; + const bit_pattern_unit = [_]u8{ 1, 0, 0, 1 };
const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit)));
// Okay, that's all of the problems. Let's see the results. // Okay, that's all of the problems. Let's see the results.
//

View File

@@ -1,6 +1,6 @@
--- exercises/006_strings.zig 2023-10-03 22:15:22.122241138 +0200 --- exercises/006_strings.zig 2026-05-04 17:04:31.763821070 +0200
+++ answers/006_strings.zig 2023-10-05 20:04:06.869430053 +0200 +++ answers/006_strings.zig 2026-05-04 17:02:11.672866263 +0200
@@ -24,18 +24,18 @@ @@ -24,14 +24,14 @@
// (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.
@@ -8,11 +8,6 @@
+ const d: u8 = ziggy[4]; + const d: u8 = ziggy[4];
// (Problem 2) // (Problem 2)
// Use the array repeat '**' operator to make "ha ha ha ".
- const laugh = "ha " ???;
+ const laugh = "ha " ** 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";
@@ -21,4 +16,4 @@
+ 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}\n", .{ d, major_tom });

View File

@@ -1,5 +1,5 @@
--- exercises/058_quiz7.zig 2024-10-28 09:06:49.448505460 +0100 --- exercises/058_quiz7.zig 2026-05-04 16:34:31.692458399 +0200
+++ answers/058_quiz7.zig 2024-10-28 09:35:14.631932322 +0100 +++ answers/058_quiz7.zig 2026-05-04 16:34:29.239406323 +0200
@@ -192,8 +192,8 @@ @@ -192,8 +192,8 @@
// Oops! The hermit forgot how to capture the union values // Oops! The hermit forgot how to capture the union values
// in a switch statement. Please capture each value as // in a switch statement. Please capture each value as
@@ -11,7 +11,7 @@
} }
} }
}; };
@@ -255,7 +255,7 @@ @@ -254,7 +254,7 @@
// dereference and optional value "unwrapping" look // dereference and optional value "unwrapping" look
// together. Remember that you return the address with the // together. Remember that you return the address with the
// "&" operator. // "&" operator.
@@ -20,7 +20,7 @@
// Try to make your answer this long:__________; // Try to make your answer this long:__________;
} }
return null; return null;
@@ -309,7 +309,7 @@ @@ -308,7 +308,7 @@
// //
// Looks like the hermit forgot something in the return value of // Looks like the hermit forgot something in the return value of
// this function. What could that be? // this function. What could that be?

View File

@@ -1,5 +1,5 @@
--- exercises/067_comptime2.zig 2025-12-07 22:51:24.396031248 +0100 --- exercises/067_comptime2.zig 2026-05-04 15:38:52.565144012 +0200
+++ answers/067_comptime2.zig 2025-12-07 22:50:18.721616293 +0100 +++ answers/067_comptime2.zig 2026-05-04 15:37:20.257213463 +0200
@@ -36,7 +36,7 @@ @@ -36,7 +36,7 @@
// In this contrived example, we've decided to allocate some // In this contrived example, we've decided to allocate some
// arrays using a variable count! But something's missing... // arrays using a variable count! But something's missing...
@@ -8,4 +8,4 @@
+ comptime var count = 0; + comptime var count = 0;
count += 1; count += 1;
const a1: [count]u8 = .{'A'} ** count; const a1: [count]u8 = @splat('A');

View File

@@ -1,5 +1,5 @@
--- exercises/074_comptime9.zig 2026-04-30 19:28:08.990995153 +0200 --- exercises/074_comptime9.zig 2026-05-04 17:11:05.144118157 +0200
+++ answers/074_comptime9.zig 2026-04-30 19:27:09.642728692 +0200 +++ answers/074_comptime9.zig 2026-05-04 17:10:36.778519877 +0200
@@ -28,12 +28,12 @@ @@ -28,12 +28,12 @@
start, // Ready to start a new animal. start, // Ready to start a new animal.
l, // This means we've seen an "l", so if we see an "m", we know it's a Llama. l, // This means we've seen an "l", so if we see an "m", we know it's a Llama.
@@ -9,7 +9,7 @@
// We return an array of animals representing the creature. (This is why we // We return an array of animals representing the creature. (This is why we
// really needed the 'count' parameter. Arrays need a size.) // really needed the 'count' parameter. Arrays need a size.)
var animals: [count]Animal = .{undefined} ** count; var animals: [count]Animal = undefined;
- var next_animal: usize = 0; - var next_animal: usize = 0;
+ comptime var next_animal: usize = 0; + comptime var next_animal: usize = 0;

View File

@@ -1,15 +1,17 @@
--- exercises/075_quiz8.zig 2023-11-21 14:48:15.440702720 +0100 --- exercises/075_quiz8.zig 2026-05-04 15:51:48.254371574 +0200
+++ answers/075_quiz8.zig 2023-11-21 14:50:23.453311616 +0100 +++ answers/075_quiz8.zig 2026-05-04 15:49:28.426445382 +0200
@@ -49,7 +49,11 @@ @@ -48,7 +48,13 @@
// instead.
// //
// Please fill in the body of this function! // Please fill in the body of this function!
fn makePath(from: *Place, to: *Place, dist: u8) Path { -fn makePath(from: *Place, to: *Place, dist: u8) Path {}
- +fn makePath(from: *Place, to: *Place, dist: u8) Path {
+ return Path{ + return Path{
+ .from = from, + .from = from,
+ .to = to, + .to = to,
+ .dist = dist, + .dist = dist,
+ }; + };
} +}
// Using our new function, these path definitions take up considerably less // Using our new function, these path definitions take up considerably less
// space in our program now!

View File

@@ -1,11 +1,11 @@
--- exercises/110_files2.zig 2026-03-20 19:23:48.874150121 +0100 --- exercises/110_files2.zig 2026-05-04 17:08:38.913033915 +0200
+++ answers/110_files2.zig 2026-04-02 10:51:15.815831734 +0200 +++ answers/110_files2.zig 2026-05-04 17:08:37.112995948 +0200
@@ -39,7 +39,7 @@ @@ -39,7 +39,7 @@
// initialize an array of u8 with all letter 'A' // initialize an array of u8 with all letter 'A'
// we need to pick the size of the array, 64 seems like a good number // we need to pick the size of the array, 64 seems like a good number
// fix the initialization below // fix the initialization below
- var content = ['A']*64; - var content = ['A']*64;
+ var content = [_]u8{'A'} ** 64; + var content: [64]u8 = @splat('A');
// this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA`
std.debug.print("{s}\n", .{content}); std.debug.print("{s}\n", .{content});