Compare commits

..

No commits in common. "d12d5a677c14ac902994bf77f8a264b8b30f441c" and "5cdaee74205e97da127d749b07cbe6e64ba6a940" have entirely different histories.

39 changed files with 72 additions and 92 deletions

View File

@ -16,6 +16,6 @@
//
const std = @import("std");
pub fn main() void {
fn main() void {
std.debug.print("Hello world!\n", .{});
}

View File

@ -11,7 +11,7 @@
// Please complete the import below:
//
const std = @import("std");
??? = @import("std");
pub fn main() void {
std.debug.print("Standard Library.\n", .{});

View File

@ -34,12 +34,12 @@
const std = @import("std");
pub fn main() void {
var n: u8 = 50;
const n: u8 = 50;
n = n + 5;
const pi: u19 = 314159;
const pi: u8 = 314159;
const negative_eleven: i8 = -11;
const negative_eleven: u8 = -11;
// There are no errors in the next line, just explanation:
// Perhaps you noticed before that the print function takes two

View File

@ -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?
var some_primes = [_]u8{ 1, 3, 5, 7, 11, 13, 17, 19 };
const 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[3];
const fourth = some_primes[???];
// (Problem 3)
// Use the len property to get the length of the array:
const length = some_primes.len;
const length = some_primes.???;
std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
first, fourth, length,

View File

@ -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 = le ++ et;
const leet = ???;
// (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{ 1, 0, 0, 1 } ** 3;
const bit_pattern = [_]u8{ ??? } ** 3;
// Okay, that's all of the problems. Let's see the results.
//

View File

@ -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[4];
const d: u8 = ziggy[???];
// (Problem 2)
// Use the array repeat '**' operator to make "ha ha ha ".
const laugh = "ha " ** 3;
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";
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 });

View File

@ -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});

View File

@ -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.
var x: usize = 1;
const 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,14 +30,13 @@ 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[1] = letters[x];
lang[???] = letters[x];
x = 5;
lang[2] = letters[x];
x = ???;
lang[2] = letters[???];
// We want to "Program in Zig!" of course:
std.debug.print("Program in {s}!\n", .{lang});

View File

@ -21,7 +21,7 @@
const std = @import("std");
pub fn main() void {
const foo = true;
const foo = 1;
// Please fix this condition:
if (foo) {

View File

@ -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(discount) 17 else 20;
const price: u8 = if ???;
std.debug.print("With the discount, the price is ${}.\n", .{price});
}

View File

@ -21,7 +21,7 @@ pub fn main() void {
var n: u32 = 2;
// Please use a condition that is true UNTIL "n" reaches 1024:
while (n < 1024 ) {
while (???) {
// Print the current number
std.debug.print("{} ", .{n});

View File

@ -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) : (n *= 2) {
while (n < 1000) : ??? {
// Print the current number
std.debug.print("{} ", .{n});
}

View File

@ -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) continue;
if (n % 5 == 0) continue;
if (n % 3 == 0) ???;
if (n % 5 == 0) ???;
std.debug.print("{} ", .{n});
}

View File

@ -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 (n == 4) break;
if (???) ???;
}
// Result: we want n=4

View File

@ -15,7 +15,7 @@ pub fn main() void {
std.debug.print("A Dramatic Story: ", .{});
for (story) |scene| {
for (???) |???| {
if (scene == 'h') std.debug.print(":-) ", .{});
if (scene == 's') std.debug.print(":-( ", .{});
if (scene == 'n') std.debug.print(":-| ", .{});

View File

@ -25,14 +25,13 @@ 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, 0..) |bit, i| {
for (bits, ???) |bit, ???| {
// 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.
const i_u32: u32 = @intCast(i);
const place_value = std.math.pow(u32, 2, i_u32);
value += place_value * bit;
// std.debug.print( "The current value is: {}.\n", .{place_value});
}
std.debug.print("The value of bits '1101': {}.\n", .{value});

View File

@ -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 ("std");
const std = import standard library;
pub fn main() void {
function main() void {
var i: u8 = 1;
const stop_at: u8 = 16;
// What kind of loop is this? A 'for' or a 'while'?
while (i <= stop_at) : (i += 1) {
??? (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("{}", .{i});
std.debug.print("{}", .{???});
}
std.debug.print(", ", .{});
}

View File

@ -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?
//
fn deepThought() u8 {
??? deepThought() ??? {
return 42; // Number courtesy Douglas Adams
}

View File

@ -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(my_number: u32) u32 {
fn twoToThe(???) 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

View File

@ -21,8 +21,8 @@ pub fn main() void {
//
// This function prints, but does not return anything.
//
fn printPowersOfTwo(numbers: [4]u16) void {
for (numbers) |n| {
fn printPowersOfTwo(numbers: [4]u16) ??? {
loop (numbers) |n| {
std.debug.print("{} ", .{twoToThe(n)});
}
}
@ -31,13 +31,13 @@ fn printPowersOfTwo(numbers: [4]u16) void {
// exercise. But don't be fooled! This one does the math without the aid
// of the standard library!
//
fn twoToThe(number: u16) u16 {
fn twoToThe(number: u16) ??? {
var n: u16 = 0;
var total: u16 = 1;
while (n < number) : (n += 1) {
loop (n < number) : (n += 1) {
total *= 2;
}
return total;
return ???;
}

View File

@ -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 (number_error == MyNumberError.TooSmall) {
if (???) {
std.debug.print("<4. ", .{});
}
if (number_error == MyNumberError.TooFour) {

View File

@ -19,7 +19,8 @@ const std = @import("std");
const MyNumberError = error{TooSmall};
pub fn main() void {
var my_number: MyNumberError!usize = 5;
var my_number: ??? = 5;
// Looks like my_number will need to either store a number OR
// an error. Can you set the type correctly above?
my_number = MyNumberError.TooSmall;

View File

@ -12,14 +12,14 @@ const MyNumberError = error{TooSmall};
pub fn main() void {
const a: u32 = addTwenty(44) catch 22;
const b: u32 = addTwenty(4) catch 22;
const b: u32 = addTwenty(4) ??? 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) MyNumberError!u32 {
fn addTwenty(n: u32) ??? {
if (n < 5) {
return MyNumberError.TooSmall;
} else {

View File

@ -53,19 +53,13 @@ fn fixTooBig(n: u32) MyNumberError!u32 {
}
fn fixTooSmall(n: u32) MyNumberError!u32 {
return detectProblems(n) catch |err| {
if (err == MyNumberError.TooSmall) {
return 10;
}
return err;
};
// Oh dear, this is missing a lot! But don't worry, it's nearly
// identical to fixTooBig() above.
//
// 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) ???;
}
fn detectProblems(n: u32) MyNumberError!u32 {

View File

@ -25,7 +25,9 @@ pub fn main() void {
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 = try detect(n);
//
const x = detect(n);
return x + 5;
}

View File

@ -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.
try stdout.print("Hello world!\n", .{});
stdout.print("Hello world!\n", .{});
}

View File

@ -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":
defer std.debug.print("Two\n", .{});
std.debug.print("Two\n", .{});
std.debug.print("One ", .{});
}

View File

@ -18,7 +18,7 @@ pub fn main() void {
fn printAnimal(animal: u8) void {
std.debug.print("(", .{});
defer std.debug.print(") ", .{}); // <---- how?!
std.debug.print(") ", .{}); // <---- how?!
if (animal == 'g') {
std.debug.print("Goat", .{});

View File

@ -32,7 +32,7 @@ fn makeNumber() MyErr!u32 {
// Please make the "failed" message print ONLY if the makeNumber()
// function exits with an error:
errdefer std.debug.print("failed!\n", .{});
std.debug.print("failed!\n", .{});
var num = try getNumber(); // <-- This could fail!

View File

@ -46,9 +46,6 @@ 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("?", .{});
},
}
}

View File

@ -31,9 +31,6 @@ 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 => {
return std.debug.print("!", .{});
},
};
std.debug.print("{c}", .{real_char});

View File

@ -35,9 +35,6 @@ pub fn main() void {
3 => {
current_value *= current_value;
},
else => {
unreachable;
},
}
std.debug.print("{} ", .{current_value});

View File

@ -39,7 +39,6 @@ pub fn main() void {
std.debug.print("={}. ", .{value});
} else |err| switch (err) {
MyNumberError.TooBig => std.debug.print(">4. ", .{}),
MyNumberError.TooSmall => std.debug.print("<4. ", .{}),
// Please add a match for TooSmall here and have it print: "<4. "
}
}

View File

@ -9,16 +9,10 @@ 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 = try getNumber();
// if (my_num) |value| {
// stdout("ZIG! {!}", .{value});
// } else |err| switch (err) {
// NumError.IllegalNumber => 42,
// }
const my_num: u32 = getNumber();
try stdout.print("my_num={}\n", .{my_num});
}

View File

@ -41,12 +41,12 @@ pub fn main() void {
for (hex_nums, ???) |hn, ???| {
if (hn != dn) {
print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
return;
}
}
print("Arrays match!\n", .{});
std.debug.print("Arrays match!\n", .{});
}
//
// You are perhaps wondering what happens if one of the two lists

View File

@ -40,7 +40,7 @@ pub fn main() !void {
// okay, seems like a threat of violence is not the answer in this case
// can you go here to find a way to read the content?
// https://ziglang.org/documentation/master/std/#std.fs.File
// hint: you might find two answers that are both valid in this case
// hint: you might find two answers that are both vaild in this case
const bytes_read = zig_read_the_file_or_i_will_fight_you(&content);
// Woah, too screamy. I know you're excited for zigling time but tone it down a bit.

View File

@ -15,11 +15,11 @@
// 1 => { op = 2; continue; },
// 2 => { op = 3; continue; },
// 3 => return,
// else => {},
// 4 => {},
// }
// break;
// }
// std.debug.print("This statement cannot be reached\n", .{});
// std.debug.print("This statement cannot be reached");
// }
//
// By combining all we've learned so far, we can now proceed with a labeled switch
@ -36,7 +36,7 @@
// 3 => return,
// else => {},
// }
// std.debug.print("This statement cannot be reached\n", .{});
// std.debug.print("This statement cannot be reached");
// }
//
// The flow of execution on this second case is:
@ -45,12 +45,13 @@
// to re-evaluate the labeled switch again, now providing the value '2';
// 3. In the case '2' we repeat the same pattern as case '1'
// but instead the value to be evaluated is now '3';
// 4. Finally we get to case '3', where we return from the function as a whole,
// so the debug statement is never executed.
// 5. In this example, since the input does not have clear, exhaustive patterns and
// can essentially be any 'u8' integer, we need to handle all cases not explicitly
// covered by using the 'else => {}' branch as the default case.
// 4. Finally we get to case '3', where we return from the function as a whole.
// 5. In this example as the input has no clear exhaustive patterns but a essentially
// any u8 integer, we need do need to handle any case that is not explicitly handled
// by using the `else => {}` branch as a default case.
//
// Since step 4 or a break stament do not exist in this switch, the debug statement is
// never executed
//
const std = @import("std");

View File

@ -7,5 +7,5 @@
- for (hex_nums, ???) |hn, ???| {
+ for (hex_nums, dec_nums) |hn, dn| {
if (hn != dn) {
print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
return;

View File

@ -42,7 +42,7 @@
- // can you go here to find a way to read the content?
+ // can you go here to find a way to read the content ?
// https://ziglang.org/documentation/master/std/#std.fs.File
// hint: you might find two answers that are both valid in this case
// hint: you might find two answers that are both vaild in this case
- const bytes_read = zig_read_the_file_or_i_will_fight_you(&content);
+ const bytes_read = try file.read(&content);