my ziglings
0

Configure Feed

Select the types of activity you want to include in your feed.

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

Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/423

Chris Boesch (May 4, 2026, 5:30 PM +0200) dde51b31 1ba1e301

+51 -64
+1 -1
build.zig
··· 777 777 }, 778 778 .{ 779 779 .main_file = "006_strings.zig", 780 - .output = "d=d ha ha ha Major Tom", 780 + .output = "d=d Major Tom", 781 781 .hint = "Each '???' needs something filled in.", 782 782 }, 783 783 .{
+5 -8
exercises/005_arrays2.zig
··· 1 1 // 2 - // Zig has some fun array operators. 2 + // Zig has one array operators. 3 3 // 4 4 // You can use '++' to concatenate two arrays: 5 5 // ··· 7 7 // const b = [_]u8{ 3,4 }; 8 8 // const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5 9 9 // 10 - // You can use '**' to repeat an array: 11 - // 12 - // const d = [_]u8{ 1,2,3 } ** 2; // equals 1 2 3 1 2 3 13 - // 14 - // Note that both '++' and '**' only operate on arrays while your 15 - // program is _being compiled_. This special time is known in Zig 10 + // Note that '++'' only operate on arrays while your program is 11 + // _being compiled_. This special time is known in Zig 16 12 // parlance as "comptime" and we'll learn plenty more about that 17 13 // later. 18 14 // ··· 30 26 // (Problem 2) 31 27 // Please set this array using repetition. 32 28 // It should result in: 1 0 0 1 1 0 0 1 1 0 0 1 33 - const bit_pattern = [_]u8{ ??? } ** 3; 29 + const bit_pattern_unit = [_]u8{ ??? }; 30 + const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit))); 34 31 35 32 // Okay, that's all of the problems. Let's see the results. 36 33 //
+1 -5
exercises/006_strings.zig
··· 27 27 const d: u8 = ziggy[???]; 28 28 29 29 // (Problem 2) 30 - // Use the array repeat '**' operator to make "ha ha ha ". 31 - const laugh = "ha " ???; 32 - 33 - // (Problem 3) 34 30 // Use the array concatenation '++' operator to make "Major Tom". 35 31 // (You'll need to add a space as well!) 36 32 const major = "Major"; ··· 38 34 const major_tom = major ??? tom; 39 35 40 36 // That's all the problems. Let's see our results: 41 - std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom }); 37 + std.debug.print("d={u} {s}\n", .{ d, major_tom }); 42 38 // Keen eyes will notice that we've put 'u' and 's' inside the '{}' 43 39 // placeholders in the format string above. This tells the 44 40 // print() function to format the values as a UTF-8 character and
+5 -6
exercises/058_quiz7.zig
··· 224 224 // +---+----------------+----------------+----------+ 225 225 // 226 226 const HermitsNotebook = struct { 227 - // Remember the array repetition operator `**`? It is no mere 228 - // novelty, it's also a great way to assign multiple items in an 229 - // array without having to list them one by one. Here we use it to 230 - // initialize an array with null values. 231 - entries: [place_count]?NotebookEntry = .{null} ** place_count, 227 + // Remember the array repetition function @splat()? It is a great way 228 + // to assign multiple items in an array without having to list them 229 + // one by one. Here we use it to initialize an array with null values. 230 + entries: [place_count]?NotebookEntry = @splat(null), 232 231 233 232 // The next entry keeps track of where we are in our "todo" list. 234 233 next_entry: u8 = 0, ··· 409 408 // aside memory for the trip and have the hermit's notebook fill 410 409 // in the trip from the destination back to the path. Note that 411 410 // this is the first time we've actually used the destination! 412 - var trip = [_]?TripItem{null} ** (place_count * 2); 411 + var trip: [place_count * 2]?TripItem = @splat(null); 413 412 414 413 notebook.getTripTo(trip[0..], destination) catch |err| { 415 414 print("Oh no! {}\n", .{err});
+4 -4
exercises/067_comptime2.zig
··· 39 39 var count = 0; 40 40 41 41 count += 1; 42 - const a1: [count]u8 = .{'A'} ** count; 42 + const a1: [count]u8 = @splat('A'); 43 43 44 44 count += 1; 45 - const a2: [count]u8 = .{'B'} ** count; 45 + const a2: [count]u8 = @splat('B'); 46 46 47 47 count += 1; 48 - const a3: [count]u8 = .{'C'} ** count; 48 + const a3: [count]u8 = @splat('C'); 49 49 50 50 count += 1; 51 - const a4: [count]u8 = .{'D'} ** count; 51 + const a4: [count]u8 = @splat('D'); 52 52 53 53 print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 }); 54 54
+1 -1
exercises/074_comptime9.zig
··· 32 32 33 33 // We return an array of animals representing the creature. (This is why we 34 34 // really needed the 'count' parameter. Arrays need a size.) 35 - var animals: [count]Animal = .{undefined} ** count; 35 + var animals: [count]Animal = undefined; 36 36 var next_animal: usize = 0; 37 37 38 38 inline for (fmt) |char| {
+3 -5
exercises/075_quiz8.zig
··· 48 48 // instead. 49 49 // 50 50 // Please fill in the body of this function! 51 - fn makePath(from: *Place, to: *Place, dist: u8) Path { 52 - 53 - } 51 + fn makePath(from: *Place, to: *Place, dist: u8) Path {} 54 52 55 53 // Using our new function, these path definitions take up considerably less 56 54 // space in our program now! ··· 97 95 }; 98 96 99 97 const HermitsNotebook = struct { 100 - entries: [place_count]?NotebookEntry = .{null} ** place_count, 98 + entries: [place_count]?NotebookEntry = @splat(null), 101 99 next_entry: u8 = 0, 102 100 end_of_entries: u8 = 0, 103 101 ··· 193 191 } 194 192 } 195 193 196 - var trip = [_]?TripItem{null} ** (place_count * 2); 194 + var trip: [place_count * 2]?TripItem = @splat(null); 197 195 198 196 notebook.getTripTo(trip[0..], destination) catch |err| { 199 197 print("Oh no! {}\n", .{err});
+6 -6
patches/patches/005_arrays2.patch
··· 1 - --- exercises/005_arrays2.zig 2023-10-03 22:15:22.122241138 +0200 2 - +++ answers/005_arrays2.zig 2023-10-05 20:04:06.862763262 +0200 3 - @@ -25,12 +25,12 @@ 1 + --- exercises/005_arrays2.zig 2026-05-04 16:26:32.778330847 +0200 2 + +++ answers/005_arrays2.zig 2026-05-04 16:26:13.082917974 +0200 3 + @@ -21,12 +21,12 @@ 4 4 // (Problem 1) 5 5 // Please set this array concatenating the two arrays above. 6 6 // It should result in: 1 3 3 7 ··· 10 10 // (Problem 2) 11 11 // Please set this array using repetition. 12 12 // It should result in: 1 0 0 1 1 0 0 1 1 0 0 1 13 - - const bit_pattern = [_]u8{ ??? } ** 3; 14 - + const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3; 13 + - const bit_pattern_unit = [_]u8{ ??? }; 14 + + const bit_pattern_unit = [_]u8{ 1, 0, 0, 1 }; 15 + const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit))); 15 16 16 17 // Okay, that's all of the problems. Let's see the results. 17 - //
+4 -9
patches/patches/006_strings.patch
··· 1 - --- exercises/006_strings.zig 2023-10-03 22:15:22.122241138 +0200 2 - +++ answers/006_strings.zig 2023-10-05 20:04:06.869430053 +0200 3 - @@ -24,18 +24,18 @@ 1 + --- exercises/006_strings.zig 2026-05-04 17:04:31.763821070 +0200 2 + +++ answers/006_strings.zig 2026-05-04 17:02:11.672866263 +0200 3 + @@ -24,14 +24,14 @@ 4 4 // (Problem 1) 5 5 // Use array square bracket syntax to get the letter 'd' from 6 6 // the string "stardust" above. ··· 8 8 + const d: u8 = ziggy[4]; 9 9 10 10 // (Problem 2) 11 - // Use the array repeat '**' operator to make "ha ha ha ". 12 - - const laugh = "ha " ???; 13 - + const laugh = "ha " ** 3; 14 - 15 - // (Problem 3) 16 11 // Use the array concatenation '++' operator to make "Major Tom". 17 12 // (You'll need to add a space as well!) 18 13 const major = "Major"; ··· 21 16 + const major_tom = major ++ " " ++ tom; 22 17 23 18 // That's all the problems. Let's see our results: 24 - std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom }); 19 + std.debug.print("d={u} {s}\n", .{ d, major_tom });
+4 -4
patches/patches/058_quiz7.patch
··· 1 - --- exercises/058_quiz7.zig 2024-10-28 09:06:49.448505460 +0100 2 - +++ answers/058_quiz7.zig 2024-10-28 09:35:14.631932322 +0100 1 + --- exercises/058_quiz7.zig 2026-05-04 16:34:31.692458399 +0200 2 + +++ answers/058_quiz7.zig 2026-05-04 16:34:29.239406323 +0200 3 3 @@ -192,8 +192,8 @@ 4 4 // Oops! The hermit forgot how to capture the union values 5 5 // in a switch statement. Please capture each value as ··· 11 11 } 12 12 } 13 13 }; 14 - @@ -255,7 +255,7 @@ 14 + @@ -254,7 +254,7 @@ 15 15 // dereference and optional value "unwrapping" look 16 16 // together. Remember that you return the address with the 17 17 // "&" operator. ··· 20 20 // Try to make your answer this long:__________; 21 21 } 22 22 return null; 23 - @@ -309,7 +309,7 @@ 23 + @@ -308,7 +308,7 @@ 24 24 // 25 25 // Looks like the hermit forgot something in the return value of 26 26 // this function. What could that be?
+3 -3
patches/patches/067_comptime2.patch
··· 1 - --- exercises/067_comptime2.zig 2025-12-07 22:51:24.396031248 +0100 2 - +++ answers/067_comptime2.zig 2025-12-07 22:50:18.721616293 +0100 1 + --- exercises/067_comptime2.zig 2026-05-04 15:38:52.565144012 +0200 2 + +++ answers/067_comptime2.zig 2026-05-04 15:37:20.257213463 +0200 3 3 @@ -36,7 +36,7 @@ 4 4 // In this contrived example, we've decided to allocate some 5 5 // arrays using a variable count! But something's missing... ··· 8 8 + comptime var count = 0; 9 9 10 10 count += 1; 11 - const a1: [count]u8 = .{'A'} ** count; 11 + const a1: [count]u8 = @splat('A');
+3 -3
patches/patches/074_comptime9.patch
··· 1 - --- exercises/074_comptime9.zig 2026-04-30 19:28:08.990995153 +0200 2 - +++ answers/074_comptime9.zig 2026-04-30 19:27:09.642728692 +0200 1 + --- exercises/074_comptime9.zig 2026-05-04 17:11:05.144118157 +0200 2 + +++ answers/074_comptime9.zig 2026-05-04 17:10:36.778519877 +0200 3 3 @@ -28,12 +28,12 @@ 4 4 start, // Ready to start a new animal. 5 5 l, // This means we've seen an "l", so if we see an "m", we know it's a Llama. ··· 9 9 10 10 // We return an array of animals representing the creature. (This is why we 11 11 // really needed the 'count' parameter. Arrays need a size.) 12 - var animals: [count]Animal = .{undefined} ** count; 12 + var animals: [count]Animal = undefined; 13 13 - var next_animal: usize = 0; 14 14 + comptime var next_animal: usize = 0; 15 15
+8 -6
patches/patches/075_quiz8.patch
··· 1 - --- exercises/075_quiz8.zig 2023-11-21 14:48:15.440702720 +0100 2 - +++ answers/075_quiz8.zig 2023-11-21 14:50:23.453311616 +0100 3 - @@ -49,7 +49,11 @@ 1 + --- exercises/075_quiz8.zig 2026-05-04 15:51:48.254371574 +0200 2 + +++ answers/075_quiz8.zig 2026-05-04 15:49:28.426445382 +0200 3 + @@ -48,7 +48,13 @@ 4 + // instead. 4 5 // 5 6 // Please fill in the body of this function! 6 - fn makePath(from: *Place, to: *Place, dist: u8) Path { 7 - - 7 + -fn makePath(from: *Place, to: *Place, dist: u8) Path {} 8 + +fn makePath(from: *Place, to: *Place, dist: u8) Path { 8 9 + return Path{ 9 10 + .from = from, 10 11 + .to = to, 11 12 + .dist = dist, 12 13 + }; 13 - } 14 + +} 14 15 15 16 // Using our new function, these path definitions take up considerably less 17 + // space in our program now!
+3 -3
patches/patches/110_files2.patch
··· 1 - --- exercises/110_files2.zig 2026-03-20 19:23:48.874150121 +0100 2 - +++ answers/110_files2.zig 2026-04-02 10:51:15.815831734 +0200 1 + --- exercises/110_files2.zig 2026-05-04 17:08:38.913033915 +0200 2 + +++ answers/110_files2.zig 2026-05-04 17:08:37.112995948 +0200 3 3 @@ -39,7 +39,7 @@ 4 4 // initialize an array of u8 with all letter 'A' 5 5 // we need to pick the size of the array, 64 seems like a good number 6 6 // fix the initialization below 7 7 - var content = ['A']*64; 8 - + var content = [_]u8{'A'} ** 64; 8 + + var content: [64]u8 = @splat('A'); 9 9 // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` 10 10 std.debug.print("{s}\n", .{content}); 11 11