mirror of
https://git.aramjonghu.dev/AramJonghu/ziglings.git
synced 2026-09-04 17:23:44 +02:00
progress
This commit is contained in:
@@ -16,6 +16,6 @@
|
|||||||
//
|
//
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
fn main() void {
|
pub fn main() void {
|
||||||
std.debug.print("Hello world!\n", .{});
|
std.debug.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
// Please complete the import below:
|
// Please complete the import below:
|
||||||
//
|
//
|
||||||
|
|
||||||
??? = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
std.debug.print("Standard Library.\n", .{});
|
std.debug.print("Standard Library.\n", .{});
|
||||||
|
|||||||
@@ -34,12 +34,12 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const n: u8 = 50;
|
var n: u16 = 50;
|
||||||
n = n + 5;
|
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:
|
// There are no errors in the next line, just explanation:
|
||||||
// Perhaps you noticed before that the print function takes two
|
// Perhaps you noticed before that the print function takes two
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ pub fn main() void {
|
|||||||
// (Problem 1)
|
// (Problem 1)
|
||||||
// This "const" is going to cause a problem later - can you see what it is?
|
// This "const" is going to cause a problem later - can you see what it is?
|
||||||
// How do we fix it?
|
// 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.
|
// Individual values can be set with '[]' notation.
|
||||||
// Example: This line changes the first prime to 2 (which is correct):
|
// Example: This line changes the first prime to 2 (which is correct):
|
||||||
@@ -40,11 +40,11 @@ pub fn main() void {
|
|||||||
// (Problem 2)
|
// (Problem 2)
|
||||||
// Looks like we need to complete this expression. Use the example
|
// Looks like we need to complete this expression. Use the example
|
||||||
// above to set "fourth" to the fourth element of the some_primes array:
|
// above to set "fourth" to the fourth element of the some_primes array:
|
||||||
const fourth = some_primes[???];
|
const fourth = some_primes[3];
|
||||||
|
|
||||||
// (Problem 3)
|
// (Problem 3)
|
||||||
// Use the len property to get the length of the array:
|
// 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", .{
|
std.debug.print("First: {}, Fourth: {}, Length: {}\n", .{
|
||||||
first, fourth, length,
|
first, fourth, length,
|
||||||
|
|||||||
@@ -21,12 +21,12 @@ pub fn main() void {
|
|||||||
// (Problem 1)
|
// (Problem 1)
|
||||||
// Please set this array concatenating the two arrays above.
|
// Please set this array concatenating the two arrays above.
|
||||||
// It should result in: 1 3 3 7
|
// It should result in: 1 3 3 7
|
||||||
const leet = ???;
|
const leet = le ++ et;
|
||||||
|
|
||||||
// (Problem 2)
|
// (Problem 2)
|
||||||
// Please set this array using repetition.
|
// Please set this array using repetition.
|
||||||
// It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
|
// It should result in: 1 0 0 1 1 0 0 1 1 0 0 1
|
||||||
const bit_pattern_unit = [_]u8{ ??? };
|
const bit_pattern_unit = [_]u8{ 1, 0, 0, 1 };
|
||||||
const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit)));
|
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.
|
// Okay, that's all of the problems. Let's see the results.
|
||||||
|
|||||||
@@ -24,14 +24,14 @@ pub fn main() void {
|
|||||||
// (Problem 1)
|
// (Problem 1)
|
||||||
// Use array square bracket syntax to get the letter 'd' from
|
// Use array square bracket syntax to get the letter 'd' from
|
||||||
// the string "stardust" above.
|
// the string "stardust" above.
|
||||||
const d: u8 = ziggy[???];
|
const d: u8 = ziggy[4];
|
||||||
|
|
||||||
// (Problem 2)
|
// (Problem 2)
|
||||||
// Use the array concatenation '++' operator to make "Major Tom".
|
// Use the array concatenation '++' operator to make "Major Tom".
|
||||||
// (You'll need to add a space as well!)
|
// (You'll need to add a space as well!)
|
||||||
const major = "Major";
|
const major = "Major";
|
||||||
const tom = "Tom";
|
const tom = "Tom";
|
||||||
const major_tom = major ??? tom;
|
const major_tom = major ++ " " ++ tom;
|
||||||
|
|
||||||
// That's all the problems. Let's see our results:
|
// That's all the problems. Let's see our results:
|
||||||
std.debug.print("d={u} {s}\n", .{ d, major_tom });
|
std.debug.print("d={u} {s}\n", .{ d, major_tom });
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ const std = @import("std");
|
|||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const lyrics =
|
const lyrics =
|
||||||
Ziggy played guitar
|
\\Ziggy played guitar
|
||||||
Jamming good with Andrew Kelley
|
\\Jamming good with Andrew Kelley
|
||||||
And the Spiders from Mars
|
\\And the Spiders from Mars
|
||||||
;
|
;
|
||||||
|
|
||||||
std.debug.print("{s}\n", .{lyrics});
|
std.debug.print("{s}\n", .{lyrics});
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ pub fn main() void {
|
|||||||
// the idiomatic type to use for array indexing.
|
// the idiomatic type to use for array indexing.
|
||||||
//
|
//
|
||||||
// There IS a problem on this line, but 'usize' isn't it.
|
// 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
|
// Note: When you want to declare memory (an array in this
|
||||||
// case) without putting anything in it, you can set it to
|
// case) without putting anything in it, you can set it to
|
||||||
@@ -33,10 +33,10 @@ pub fn main() void {
|
|||||||
lang[0] = letters[x];
|
lang[0] = letters[x];
|
||||||
|
|
||||||
x = 3;
|
x = 3;
|
||||||
lang[???] = letters[x];
|
lang[1] = letters[x];
|
||||||
|
|
||||||
x = ???;
|
x = 5;
|
||||||
lang[2] = letters[???];
|
lang[2] = letters[x];
|
||||||
|
|
||||||
// We want to "Program in Zig!" of course:
|
// We want to "Program in Zig!" of course:
|
||||||
std.debug.print("Program in {s}!\n", .{lang});
|
std.debug.print("Program in {s}!\n", .{lang});
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ pub fn main() void {
|
|||||||
const foo = 42;
|
const foo = 42;
|
||||||
|
|
||||||
// Please fix this condition:
|
// Please fix this condition:
|
||||||
if (foo) {
|
if (foo == 42) {
|
||||||
// We want our program to print this message!
|
// We want our program to print this message!
|
||||||
std.debug.print("Foo is 42!\n", .{});
|
std.debug.print("Foo is 42!\n", .{});
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ pub fn main() void {
|
|||||||
|
|
||||||
// Please use an if...else expression to set "price".
|
// Please use an if...else expression to set "price".
|
||||||
// If discount is true, the price should be $17, otherwise $20:
|
// 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});
|
std.debug.print("With the discount, the price is ${}.\n", .{price});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ pub fn main() void {
|
|||||||
var n: u32 = 2;
|
var n: u32 = 2;
|
||||||
|
|
||||||
// Please use a condition that is true UNTIL "n" reaches 1024:
|
// Please use a condition that is true UNTIL "n" reaches 1024:
|
||||||
while (???) {
|
while (n != 1024) {
|
||||||
// Print the current number
|
// Print the current number
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ pub fn main() void {
|
|||||||
|
|
||||||
// Please set the continue expression so that we get the desired
|
// Please set the continue expression so that we get the desired
|
||||||
// results in the print statement below.
|
// results in the print statement below.
|
||||||
while (n < 1000) : ??? {
|
while (n < 1000) : (n *= 2) {
|
||||||
// Print the current number
|
// Print the current number
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ pub fn main() void {
|
|||||||
while (n <= 20) : (n += 1) {
|
while (n <= 20) : (n += 1) {
|
||||||
// The '%' symbol is the "modulo" operator and it
|
// The '%' symbol is the "modulo" operator and it
|
||||||
// returns the remainder after division.
|
// returns the remainder after division.
|
||||||
if (n % 3 == 0) ???;
|
if (n % 3 == 0) continue;
|
||||||
if (n % 5 == 0) ???;
|
if (n % 5 == 0) continue;
|
||||||
std.debug.print("{} ", .{n});
|
std.debug.print("{} ", .{n});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub fn main() void {
|
|||||||
// Oh dear! This while loop will go forever?!
|
// Oh dear! This while loop will go forever?!
|
||||||
// Please fix this so the print statement below gives the desired output.
|
// Please fix this so the print statement below gives the desired output.
|
||||||
while (true) : (n += 1) {
|
while (true) : (n += 1) {
|
||||||
if (???) ???;
|
if (n == 4) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Result: we want n=4
|
// Result: we want n=4
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ pub fn main() void {
|
|||||||
|
|
||||||
std.debug.print("A Dramatic Story: ", .{});
|
std.debug.print("A Dramatic Story: ", .{});
|
||||||
|
|
||||||
for (???) |???| {
|
for (story) |scene| {
|
||||||
if (scene == 'h') std.debug.print(":-) ", .{});
|
if (scene == 'h') std.debug.print(":-) ", .{});
|
||||||
if (scene == 's') std.debug.print(":-( ", .{});
|
if (scene == 's') std.debug.print(":-( ", .{});
|
||||||
if (scene == 'n') std.debug.print(":-| ", .{});
|
if (scene == 'n') std.debug.print(":-| ", .{});
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ pub fn main() void {
|
|||||||
// the value of the place as a power of two for each bit.
|
// the value of the place as a power of two for each bit.
|
||||||
//
|
//
|
||||||
// See if you can figure out the missing pieces:
|
// 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
|
// Note that we convert the usize i to a u32 with
|
||||||
// @intCast(), a builtin function just like @import().
|
// @intCast(), a builtin function just like @import().
|
||||||
// We'll learn about these properly in a later exercise.
|
// We'll learn about these properly in a later exercise.
|
||||||
|
|||||||
@@ -9,18 +9,18 @@
|
|||||||
// Let's go from 1 to 16. This has been started for you, but there
|
// Let's go from 1 to 16. This has been started for you, but there
|
||||||
// are some problems. :-(
|
// are some problems. :-(
|
||||||
//
|
//
|
||||||
const std = import standard library;
|
const std = @import("std");
|
||||||
|
|
||||||
function main() void {
|
pub fn main() void {
|
||||||
var i: u8 = 1;
|
var i: u8 = 1;
|
||||||
const stop_at: u8 = 16;
|
const stop_at: u8 = 16;
|
||||||
|
|
||||||
// What kind of loop is this? A 'for' or a 'while'?
|
// 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 % 3 == 0) std.debug.print("Fizz", .{});
|
||||||
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
if (i % 5 == 0) std.debug.print("Buzz", .{});
|
||||||
if (!(i % 3 == 0) and !(i % 5 == 0)) {
|
if (!(i % 3 == 0) and !(i % 5 == 0)) {
|
||||||
std.debug.print("{}", .{???});
|
std.debug.print("{}", .{i});
|
||||||
}
|
}
|
||||||
std.debug.print(", ", .{});
|
std.debug.print(", ", .{});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,6 @@ pub fn main() void {
|
|||||||
// We're just missing a couple things. One thing we're NOT missing is the
|
// 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?
|
// keyword "pub", which is not needed here. Can you guess why?
|
||||||
//
|
//
|
||||||
??? deepThought() ??? {
|
fn deepThought() u8 {
|
||||||
return 42; // Number courtesy Douglas Adams
|
return 42; // Number courtesy Douglas Adams
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ pub fn main() void {
|
|||||||
// You'll need to figure out the parameter name and type that we're
|
// You'll need to figure out the parameter name and type that we're
|
||||||
// expecting. The output type has already been specified for you.
|
// expecting. The output type has already been specified for you.
|
||||||
//
|
//
|
||||||
fn twoToThe(???) u32 {
|
fn twoToThe(my_number: u32) u32 {
|
||||||
return std.math.pow(u32, 2, my_number);
|
return std.math.pow(u32, 2, my_number);
|
||||||
// std.math.pow(type, a, b) takes a numeric type and two
|
// std.math.pow(type, a, b) takes a numeric type and two
|
||||||
// numbers of that type (or that can coerce to that type) and
|
// numbers of that type (or that can coerce to that type) and
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ pub fn main() void {
|
|||||||
//
|
//
|
||||||
// This function prints, but does not return anything.
|
// This function prints, but does not return anything.
|
||||||
//
|
//
|
||||||
fn printPowersOfTwo(numbers: [4]u16) ??? {
|
fn printPowersOfTwo(numbers: [4]u16) void {
|
||||||
loop (numbers) |n| {
|
for (numbers) |n| {
|
||||||
std.debug.print("{} ", .{twoToThe(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
|
// exercise. But don't be fooled! This one does the math without the aid
|
||||||
// of the standard library!
|
// of the standard library!
|
||||||
//
|
//
|
||||||
fn twoToThe(number: u16) ??? {
|
fn twoToThe(number: u16) u16 {
|
||||||
var n: u16 = 0;
|
var n: u16 = 0;
|
||||||
var total: u16 = 1;
|
var total: u16 = 1;
|
||||||
|
|
||||||
loop (n < number) : (n += 1) {
|
while (n < number) : (n += 1) {
|
||||||
total *= 2;
|
total *= 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ???;
|
return total;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@
|
|||||||
// "TooSmall". Please add it where needed!
|
// "TooSmall". Please add it where needed!
|
||||||
const MyNumberError = error{
|
const MyNumberError = error{
|
||||||
TooBig,
|
TooBig,
|
||||||
???,
|
TooSmall,
|
||||||
TooFour,
|
TooFour,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ pub fn main() void {
|
|||||||
if (number_error == MyNumberError.TooBig) {
|
if (number_error == MyNumberError.TooBig) {
|
||||||
std.debug.print(">4. ", .{});
|
std.debug.print(">4. ", .{});
|
||||||
}
|
}
|
||||||
if (???) {
|
if (number_error == MyNumberError.TooSmall) {
|
||||||
std.debug.print("<4. ", .{});
|
std.debug.print("<4. ", .{});
|
||||||
}
|
}
|
||||||
if (number_error == MyNumberError.TooFour) {
|
if (number_error == MyNumberError.TooFour) {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ const std = @import("std");
|
|||||||
const MyNumberError = error{TooSmall};
|
const MyNumberError = error{TooSmall};
|
||||||
|
|
||||||
pub fn main() void {
|
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
|
// Looks like my_number will need to either store a number OR
|
||||||
// an error. Can you set the type correctly above?
|
// an error. Can you set the type correctly above?
|
||||||
|
|||||||
@@ -12,14 +12,14 @@ const MyNumberError = error{TooSmall};
|
|||||||
|
|
||||||
pub fn main() void {
|
pub fn main() void {
|
||||||
const a: u32 = addTwenty(44) catch 22;
|
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 });
|
std.debug.print("a={}, b={}\n", .{ a, b });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Please provide the return type from this function.
|
// Please provide the return type from this function.
|
||||||
// Hint: it'll be an error union.
|
// Hint: it'll be an error union.
|
||||||
fn addTwenty(n: u32) ??? {
|
fn addTwenty(n: u32) !u32 {
|
||||||
if (n < 5) {
|
if (n < 5) {
|
||||||
return MyNumberError.TooSmall;
|
return MyNumberError.TooSmall;
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user