summaryrefslogtreecommitdiff
path: root/exercises
diff options
context:
space:
mode:
authorChris Boesch <chrboesch@noreply.codeberg.org>2026-05-04 17:30:31 +0200
committerChris Boesch <chrboesch@noreply.codeberg.org>2026-05-04 17:30:31 +0200
commitdde51b3126b0f9cfbbf5d1ec49fd19ad37dd5a5f (patch)
treea333fe8d515ae8fad19a94e37bdc4649f6dd662c /exercises
parent1ba1e301a8a977f652157ca60a0522c95bc736aa (diff)
parent8af3372cf204861f8541f91e47543f1b33337c5b (diff)
Merge pull request 'fixed removed array multiplication' (#423) from array_mult into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/423
Diffstat (limited to 'exercises')
-rw-r--r--exercises/005_arrays2.zig13
-rw-r--r--exercises/006_strings.zig6
-rw-r--r--exercises/058_quiz7.zig11
-rw-r--r--exercises/067_comptime2.zig8
-rw-r--r--exercises/074_comptime9.zig2
-rw-r--r--exercises/075_quiz8.zig8
6 files changed, 19 insertions, 29 deletions
diff --git a/exercises/005_arrays2.zig b/exercises/005_arrays2.zig
index 497d400..fdbb505 100644
--- a/exercises/005_arrays2.zig
+++ b/exercises/005_arrays2.zig
@@ -1,5 +1,5 @@
//
-// Zig has some fun array operators.
+// Zig has one array operators.
//
// You can use '++' to concatenate two arrays:
//
@@ -7,12 +7,8 @@
// const b = [_]u8{ 3,4 };
// const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5
//
-// You can use '**' to repeat an array:
-//
-// 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
+// Note that '++'' 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
// later.
//
@@ -30,7 +26,8 @@ pub fn main() void {
// (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_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.
//
diff --git a/exercises/006_strings.zig b/exercises/006_strings.zig
index 5a7172c..08977de 100644
--- a/exercises/006_strings.zig
+++ b/exercises/006_strings.zig
@@ -27,10 +27,6 @@ pub fn main() void {
const d: u8 = ziggy[???];
// (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".
// (You'll need to add a space as well!)
const major = "Major";
@@ -38,7 +34,7 @@ pub fn main() void {
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 });
+ std.debug.print("d={u} {s}\n", .{ d, major_tom });
// Keen eyes will notice that we've put 'u' and 's' inside the '{}'
// placeholders in the format string above. This tells the
// print() function to format the values as a UTF-8 character and
diff --git a/exercises/058_quiz7.zig b/exercises/058_quiz7.zig
index fda83fc..d3b677b 100644
--- a/exercises/058_quiz7.zig
+++ b/exercises/058_quiz7.zig
@@ -224,11 +224,10 @@ const NotebookEntry = struct {
// +---+----------------+----------------+----------+
//
const HermitsNotebook = struct {
- // Remember the array repetition operator `**`? It is no mere
- // novelty, it's also a great way to assign multiple items in an
- // array without having to list them one by one. Here we use it to
- // initialize an array with null values.
- entries: [place_count]?NotebookEntry = .{null} ** place_count,
+ // Remember the array repetition function @splat()? It is a great way
+ // to assign multiple items in an array without having to list them
+ // one by one. Here we use it to initialize an array with null values.
+ entries: [place_count]?NotebookEntry = @splat(null),
// The next entry keeps track of where we are in our "todo" list.
next_entry: u8 = 0,
@@ -409,7 +408,7 @@ pub fn main() void {
// aside memory for the trip and have the hermit's notebook fill
// in the trip from the destination back to the path. Note that
// 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| {
print("Oh no! {}\n", .{err});
diff --git a/exercises/067_comptime2.zig b/exercises/067_comptime2.zig
index bdbc3da..e212263 100644
--- a/exercises/067_comptime2.zig
+++ b/exercises/067_comptime2.zig
@@ -39,16 +39,16 @@ pub fn main() void {
var count = 0;
count += 1;
- const a1: [count]u8 = .{'A'} ** count;
+ const a1: [count]u8 = @splat('A');
count += 1;
- const a2: [count]u8 = .{'B'} ** count;
+ const a2: [count]u8 = @splat('B');
count += 1;
- const a3: [count]u8 = .{'C'} ** count;
+ const a3: [count]u8 = @splat('C');
count += 1;
- const a4: [count]u8 = .{'D'} ** count;
+ const a4: [count]u8 = @splat('D');
print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 });
diff --git a/exercises/074_comptime9.zig b/exercises/074_comptime9.zig
index 1fc8e31..dfdc588 100644
--- a/exercises/074_comptime9.zig
+++ b/exercises/074_comptime9.zig
@@ -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
// 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;
inline for (fmt) |char| {
diff --git a/exercises/075_quiz8.zig b/exercises/075_quiz8.zig
index 63d208b..08270ee 100644
--- a/exercises/075_quiz8.zig
+++ b/exercises/075_quiz8.zig
@@ -48,9 +48,7 @@ const Path = struct {
// instead.
//
// 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
// space in our program now!
@@ -97,7 +95,7 @@ const NotebookEntry = struct {
};
const HermitsNotebook = struct {
- entries: [place_count]?NotebookEntry = .{null} ** place_count,
+ entries: [place_count]?NotebookEntry = @splat(null),
next_entry: 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| {
print("Oh no! {}\n", .{err});