From 3c0ecf69106f32ccfc6da0479b6cf073e237a6bc Mon Sep 17 00:00:00 2001 From: ganome Date: Sat, 25 Jan 2025 13:30:37 -0700 Subject: [PATCH] Up to first quiz complete --- 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 | 9 +++++---- exercises/009_if.zig | 2 +- exercises/010_if2.zig | 2 +- exercises/011_while.zig | 2 +- exercises/012_while2.zig | 2 +- 12 files changed, 25 insertions(+), 24 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..2bbf403 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: u19 = 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..e66bb34 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 @@ -30,13 +30,14 @@ pub fn main() void { // 'lang' array we just created by indexing the array // 'letters' with the variable 'x'. As you can see above, x=1 // to begin with. + //x = 1; 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..2cb3a64 100644 --- a/exercises/009_if.zig +++ b/exercises/009_if.zig @@ -21,7 +21,7 @@ const std = @import("std"); pub fn main() void { - const foo = 1; + const foo = true; // Please fix this condition: if (foo) { diff --git a/exercises/010_if2.zig b/exercises/010_if2.zig index f0ffb43..4744e9e 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..9f2ccb7 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}); }