forked from zig/exercises
Compare commits
14 Commits
dfdaf03d99
...
fc818d088d
Author | SHA1 | Date | |
---|---|---|---|
|
fc818d088d | ||
|
95083a8c34 | ||
|
2cb6aa0b88 | ||
|
5c099517a1 | ||
|
b80931c12d | ||
|
9d935ff81e | ||
|
a1dd3b038a | ||
|
332f56b6b4 | ||
|
d23622d0af | ||
|
a43eb1bf99 | ||
|
73dc633434 | ||
|
cb85607e64 | ||
|
4d844b4186 | ||
|
e38a912491 |
|
@ -16,6 +16,6 @@
|
|||
//
|
||||
const std = @import("std");
|
||||
|
||||
fn main() void {
|
||||
pub fn main() void {
|
||||
std.debug.print("Hello world!\n", .{});
|
||||
}
|
||||
|
|
|
@ -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", .{});
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
//
|
||||
|
|
|
@ -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 });
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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});
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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});
|
||||
}
|
||||
|
|
|
@ -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});
|
||||
|
||||
|
|
|
@ -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});
|
||||
}
|
||||
|
|
|
@ -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});
|
||||
}
|
||||
|
||||
|
|
|
@ -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 == 4) break;
|
||||
}
|
||||
|
||||
// Result: we want n=4
|
||||
|
|
Loading…
Reference in New Issue
Block a user