Finished 2nd quiz

This commit is contained in:
ganome 2025-01-25 14:23:24 -07:00
parent 3c0ecf6910
commit c245fd663a
Signed by untrusted user who does not match committer: Ganome
GPG Key ID: 944DE53336D81B83
5 changed files with 10 additions and 9 deletions

View File

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

View File

@ -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

View File

@ -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(":-| ", .{});

View File

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