From e8270de928a430130000c95b7677c21f6a947ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20K=C3=B6hring?= Date: Mon, 27 May 2024 15:21:28 +0200 Subject: [PATCH] some exercises --- exercises/001_hello.zig | 2 +- exercises/002_std.zig | 2 +- exercises/003_assignment.zig | 6 +++--- exercises/004_arrays.zig | 6 +++--- exercises/005_arrays2.zig | 4 ++-- exercises/006_strings.zig | 6 +++--- exercises/007_strings2.zig | 6 +++--- exercises/008_quiz.zig | 8 ++++---- exercises/009_if.zig | 2 +- exercises/010_if2.zig | 2 +- exercises/011_while.zig | 2 +- exercises/012_while2.zig | 2 +- exercises/013_while3.zig | 4 ++-- exercises/014_while4.zig | 2 +- exercises/015_for.zig | 2 +- exercises/016_for2.zig | 2 +- exercises/017_quiz2.zig | 8 ++++---- exercises/018_functions.zig | 2 +- exercises/019_functions2.zig | 2 +- exercises/020_quiz3.zig | 10 +++++----- exercises/021_errors.zig | 4 ++-- exercises/022_errors2.zig | 2 +- exercises/023_errors3.zig | 4 ++-- exercises/024_errors4.zig | 7 ++++++- exercises/025_errors5.zig | 2 +- exercises/026_hello2.zig | 2 +- exercises/027_defer.zig | 2 +- exercises/028_defer2.zig | 2 +- exercises/029_errdefer.zig | 2 +- exercises/030_switch.zig | 1 + exercises/031_switch2.zig | 1 + exercises/032_unreachable.zig | 1 + exercises/033_iferror.zig | 2 +- exercises/034_quiz4.zig | 7 ++----- exercises/035_enums.zig | 2 +- exercises/036_enums2.zig | 6 +++--- 36 files changed, 66 insertions(+), 61 deletions(-) diff --git a/exercises/001_hello.zig b/exercises/001_hello.zig index 2d95a10..eab663a 100644 --- a/exercises/001_hello.zig +++ b/exercises/001_hello.zig @@ -16,6 +16,6 @@ // const std = @import("std"); -fn main() void { +pub fn main() void { std.debug.print("Hello world!\n", .{}); } diff --git a/exercises/002_std.zig b/exercises/002_std.zig index 8cc3792..5916c7c 100644 --- a/exercises/002_std.zig +++ b/exercises/002_std.zig @@ -11,7 +11,7 @@ // Please complete the import below: // -??? = @import("std"); +const std = @import("std"); pub fn main() void { std.debug.print("Standard Library.\n", .{}); diff --git a/exercises/003_assignment.zig b/exercises/003_assignment.zig index 10ba8cb..23ef638 100644 --- a/exercises/003_assignment.zig +++ b/exercises/003_assignment.zig @@ -34,12 +34,12 @@ const std = @import("std"); pub fn main() void { - const n: u8 = 50; + var n: u8 = 50; n = n + 5; - const pi: u8 = 314159; + const pi: u32 = 314159; - const negative_eleven: u8 = -11; + const negative_eleven: i8 = -11; // There are no errors in the next line, just explanation: // Perhaps you noticed before that the print function takes two diff --git a/exercises/004_arrays.zig b/exercises/004_arrays.zig index 88fcc78..b6756bb 100644 --- a/exercises/004_arrays.zig +++ b/exercises/004_arrays.zig @@ -27,7 +27,7 @@ pub fn main() void { // (Problem 1) // This "const" is going to cause a problem later - can you see what it is? // How do we fix it? - const some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 }; + var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 }; // Individual values can be set with '[]' notation. // Example: This line changes the first prime to 2 (which is correct): @@ -40,11 +40,11 @@ pub fn main() void { // (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, diff --git a/exercises/005_arrays2.zig b/exercises/005_arrays2.zig index 497d400..e5058a9 100644 --- a/exercises/005_arrays2.zig +++ b/exercises/005_arrays2.zig @@ -25,12 +25,12 @@ pub fn main() void { // (Problem 1) // Please set this array concatenating the two arrays above. // It should result in: 1 3 3 7 - const leet = ???; + 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. // diff --git a/exercises/006_strings.zig b/exercises/006_strings.zig index 5a7172c..3ad26ce 100644 --- a/exercises/006_strings.zig +++ b/exercises/006_strings.zig @@ -24,18 +24,18 @@ pub fn main() void { // (Problem 1) // Use array square bracket syntax to get the letter 'd' from // the string "stardust" above. - const d: u8 = ziggy[???]; + const d: u8 = ziggy[4]; // (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". // (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 }); diff --git a/exercises/007_strings2.zig b/exercises/007_strings2.zig index 6350be1..74cb752 100644 --- a/exercises/007_strings2.zig +++ b/exercises/007_strings2.zig @@ -15,9 +15,9 @@ const std = @import("std"); pub fn main() void { const lyrics = - Ziggy played guitar - 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}); diff --git a/exercises/008_quiz.zig b/exercises/008_quiz.zig index 5a81fb2..dff4da3 100644 --- a/exercises/008_quiz.zig +++ b/exercises/008_quiz.zig @@ -19,7 +19,7 @@ pub fn main() void { // the idiomatic type to use for array indexing. // // There IS a problem on this line, but 'usize' isn't it. - const x: usize = 1; + var x: usize = 1; // Note: When you want to declare memory (an array in this // case) without putting anything in it, you can set it to @@ -33,10 +33,10 @@ pub fn main() void { 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}); diff --git a/exercises/009_if.zig b/exercises/009_if.zig index 4536fc3..a053fa2 100644 --- a/exercises/009_if.zig +++ b/exercises/009_if.zig @@ -24,7 +24,7 @@ pub fn main() void { 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 { diff --git a/exercises/010_if2.zig b/exercises/010_if2.zig index f0ffb43..7bbc01c 100644 --- a/exercises/010_if2.zig +++ b/exercises/010_if2.zig @@ -10,7 +10,7 @@ pub fn main() void { // 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}); } diff --git a/exercises/011_while.zig b/exercises/011_while.zig index 674d904..1706e67 100644 --- a/exercises/011_while.zig +++ b/exercises/011_while.zig @@ -21,7 +21,7 @@ pub fn main() void { 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}); diff --git a/exercises/012_while2.zig b/exercises/012_while2.zig index c9905aa..6f8e168 100644 --- a/exercises/012_while2.zig +++ b/exercises/012_while2.zig @@ -25,7 +25,7 @@ pub fn main() void { // 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}); } diff --git a/exercises/013_while3.zig b/exercises/013_while3.zig index 4cccf62..8f9a924 100644 --- a/exercises/013_while3.zig +++ b/exercises/013_while3.zig @@ -24,8 +24,8 @@ pub fn main() void { while (n <= 20) : (n += 1) { // The '%' symbol is the "modulo" operator and it // 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}); } diff --git a/exercises/014_while4.zig b/exercises/014_while4.zig index 95aecb0..7b6c7fa 100644 --- a/exercises/014_while4.zig +++ b/exercises/014_while4.zig @@ -18,7 +18,7 @@ pub fn main() void { // 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 > 3) break; } // Result: we want n=4 diff --git a/exercises/015_for.zig b/exercises/015_for.zig index 0ee8e7d..e6b9729 100644 --- a/exercises/015_for.zig +++ b/exercises/015_for.zig @@ -15,7 +15,7 @@ pub fn main() void { 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(":-| ", .{}); diff --git a/exercises/016_for2.zig b/exercises/016_for2.zig index 6fb7844..eaab65f 100644 --- a/exercises/016_for2.zig +++ b/exercises/016_for2.zig @@ -25,7 +25,7 @@ pub fn main() void { // 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. diff --git a/exercises/017_quiz2.zig b/exercises/017_quiz2.zig index 6f32c2e..4ad3ca7 100644 --- a/exercises/017_quiz2.zig +++ b/exercises/017_quiz2.zig @@ -9,18 +9,18 @@ // Let's go from 1 to 16. This has been started for you, but there // are some problems. :-( // -const std = import standard library; +const std = @import("std"); -function main() void { +pub fn main() void { var i: u8 = 1; const stop_at: u8 = 16; // What kind of loop is this? A 'for' or a 'while'? - ??? (i <= stop_at) : (i += 1) { + while (i <= stop_at) : (i += 1) { if (i % 3 == 0) std.debug.print("Fizz", .{}); if (i % 5 == 0) std.debug.print("Buzz", .{}); if (!(i % 3 == 0) and !(i % 5 == 0)) { - std.debug.print("{}", .{???}); + std.debug.print("{}", .{i}); } std.debug.print(", ", .{}); } diff --git a/exercises/018_functions.zig b/exercises/018_functions.zig index 1f78438..c9cc6c5 100644 --- a/exercises/018_functions.zig +++ b/exercises/018_functions.zig @@ -25,6 +25,6 @@ pub fn main() void { // 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 } diff --git a/exercises/019_functions2.zig b/exercises/019_functions2.zig index a527ae2..e8a1ef0 100644 --- a/exercises/019_functions2.zig +++ b/exercises/019_functions2.zig @@ -22,7 +22,7 @@ pub fn main() void { // 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: u8) 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 diff --git a/exercises/020_quiz3.zig b/exercises/020_quiz3.zig index 571628e..d028d4d 100644 --- a/exercises/020_quiz3.zig +++ b/exercises/020_quiz3.zig @@ -21,8 +21,8 @@ pub fn main() void { // // This function prints, but does not return anything. // -fn printPowersOfTwo(numbers: [4]u16) ??? { - loop (numbers) |n| { +fn printPowersOfTwo(numbers: [4]u16) void { + for (numbers) |n| { std.debug.print("{} ", .{twoToThe(n)}); } } @@ -31,13 +31,13 @@ fn printPowersOfTwo(numbers: [4]u16) ??? { // exercise. But don't be fooled! This one does the math without the aid // of the standard library! // -fn twoToThe(number: u16) ??? { +fn twoToThe(number: u16) u32 { var n: u16 = 0; var total: u16 = 1; - loop (n < number) : (n += 1) { + while (n < number) : (n += 1) { total *= 2; } - return ???; + return total; } diff --git a/exercises/021_errors.zig b/exercises/021_errors.zig index 7afeace..a6670b1 100644 --- a/exercises/021_errors.zig +++ b/exercises/021_errors.zig @@ -9,7 +9,7 @@ // "TooSmall". Please add it where needed! const MyNumberError = error{ TooBig, - ???, + TooSmall, TooFour, }; @@ -26,7 +26,7 @@ pub fn main() void { if (number_error == MyNumberError.TooBig) { std.debug.print(">4. ", .{}); } - if (???) { + if (number_error == MyNumberError.TooSmall) { std.debug.print("<4. ", .{}); } if (number_error == MyNumberError.TooFour) { diff --git a/exercises/022_errors2.zig b/exercises/022_errors2.zig index 1d513b3..0d4bb73 100644 --- a/exercises/022_errors2.zig +++ b/exercises/022_errors2.zig @@ -19,7 +19,7 @@ const std = @import("std"); 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? diff --git a/exercises/023_errors3.zig b/exercises/023_errors3.zig index 195f21a..2fdc4a2 100644 --- a/exercises/023_errors3.zig +++ b/exercises/023_errors3.zig @@ -12,14 +12,14 @@ const MyNumberError = error{TooSmall}; pub fn main() void { const a: u32 = addTwenty(44) catch 22; - const b: u32 = addTwenty(4) ??? 22; + const b: u32 = addTwenty(4) catch 22; std.debug.print("a={}, b={}\n", .{ a, b }); } // 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 { diff --git a/exercises/024_errors4.zig b/exercises/024_errors4.zig index 02ec0f2..3768b60 100644 --- a/exercises/024_errors4.zig +++ b/exercises/024_errors4.zig @@ -59,7 +59,12 @@ fn fixTooSmall(n: u32) MyNumberError!u32 { // If we get a TooSmall error, we should return 10. // If we get any other error, we should return that error. // Otherwise, we return the u32 number. - return detectProblems(n) ???; + return detectProblems(n) catch |err| { + if (err == MyNumberError.TooSmall) { + return 10; + } + return err; + }; } fn detectProblems(n: u32) MyNumberError!u32 { diff --git a/exercises/025_errors5.zig b/exercises/025_errors5.zig index 94bf1c7..55e08a8 100644 --- a/exercises/025_errors5.zig +++ b/exercises/025_errors5.zig @@ -26,7 +26,7 @@ fn addFive(n: u32) MyNumberError!u32 { // This function needs to return any error which might come back from detect(). // Please use a "try" statement rather than a "catch". // - const x = detect(n); + const x = try detect(n); return x + 5; } diff --git a/exercises/026_hello2.zig b/exercises/026_hello2.zig index cd59b86..64c64c4 100644 --- a/exercises/026_hello2.zig +++ b/exercises/026_hello2.zig @@ -23,5 +23,5 @@ pub fn main() !void { // 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", .{}); } diff --git a/exercises/027_defer.zig b/exercises/027_defer.zig index b41e2af..68d0974 100644 --- a/exercises/027_defer.zig +++ b/exercises/027_defer.zig @@ -20,6 +20,6 @@ const std = @import("std"); 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 ", .{}); } diff --git a/exercises/028_defer2.zig b/exercises/028_defer2.zig index 35c1326..358fe28 100644 --- a/exercises/028_defer2.zig +++ b/exercises/028_defer2.zig @@ -18,7 +18,7 @@ pub fn main() void { fn printAnimal(animal: u8) void { std.debug.print("(", .{}); - std.debug.print(") ", .{}); // <---- how?! + defer std.debug.print(") ", .{}); // <---- how?! if (animal == 'g') { std.debug.print("Goat", .{}); diff --git a/exercises/029_errdefer.zig b/exercises/029_errdefer.zig index 39ab306..bda1ea2 100644 --- a/exercises/029_errdefer.zig +++ b/exercises/029_errdefer.zig @@ -32,7 +32,7 @@ fn makeNumber() MyErr!u32 { // 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! diff --git a/exercises/030_switch.zig b/exercises/030_switch.zig index cb983f5..04e6b6d 100644 --- a/exercises/030_switch.zig +++ b/exercises/030_switch.zig @@ -46,6 +46,7 @@ pub fn main() void { // 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("?", .{}), } } diff --git a/exercises/031_switch2.zig b/exercises/031_switch2.zig index cf5b5a5..444539c 100644 --- a/exercises/031_switch2.zig +++ b/exercises/031_switch2.zig @@ -31,6 +31,7 @@ pub fn main() void { 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}); diff --git a/exercises/032_unreachable.zig b/exercises/032_unreachable.zig index ffc35a4..7fe5fc1 100644 --- a/exercises/032_unreachable.zig +++ b/exercises/032_unreachable.zig @@ -35,6 +35,7 @@ pub fn main() void { 3 => { current_value *= current_value; }, + else => unreachable, } std.debug.print("{} ", .{current_value}); diff --git a/exercises/033_iferror.zig b/exercises/033_iferror.zig index 6ba0c61..a40a1a7 100644 --- a/exercises/033_iferror.zig +++ b/exercises/033_iferror.zig @@ -39,7 +39,7 @@ pub fn main() void { std.debug.print("={}. ", .{value}); } else |err| switch (err) { MyNumberError.TooBig => std.debug.print(">4. ", .{}), - // Please add a match for TooSmall here and have it print: "<4. " + MyNumberError.TooSmall => std.debug.print("<4. ", .{}), } } diff --git a/exercises/034_quiz4.zig b/exercises/034_quiz4.zig index 2d843f2..9f27411 100644 --- a/exercises/034_quiz4.zig +++ b/exercises/034_quiz4.zig @@ -6,14 +6,11 @@ // my_num=42 // const std = @import("std"); - const NumError = error{IllegalNumber}; -pub fn main() void { +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}); } diff --git a/exercises/035_enums.zig b/exercises/035_enums.zig index 1825f52..d99b589 100644 --- a/exercises/035_enums.zig +++ b/exercises/035_enums.zig @@ -20,7 +20,7 @@ const std = @import("std"); // Please complete the enum! -const Ops = enum { ??? }; +const Ops = enum { inc, dec, pow }; pub fn main() void { const operations = [_]Ops{ diff --git a/exercises/036_enums2.zig b/exercises/036_enums2.zig index dc2998c..5e0efdd 100644 --- a/exercises/036_enums2.zig +++ b/exercises/036_enums2.zig @@ -31,7 +31,7 @@ const std = @import("std"); const Color = enum(u32) { red = 0xff0000, green = 0x00ff00, - blue = ???, + blue = 0x0000ff, }; pub fn main() void { @@ -53,12 +53,12 @@ pub fn main() void { \\

\\ Red \\ Green - \\ Blue + \\ Blue \\

\\ , .{ @intFromEnum(Color.red), @intFromEnum(Color.green), - @intFromEnum(???), // Oops! We're missing something! + @intFromEnum(Color.blue), // Oops! We're missing something! }); }