Skip to content

Commit 2e2924a

Browse files
committed
Added Ex. 100 fourth for (as foretold in ratfactor#261)
1 parent 4198d5b commit 2e2924a

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

build.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ const exercises = [_]Exercise{
519519
.main_file = "099_formatting.zig",
520520
.output = "\n X | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+\n 1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 \n\n 2 | 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 \n\n 3 | 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 \n\n 4 | 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 \n\n 5 | 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 \n\n 6 | 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 \n\n 7 | 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 \n\n 8 | 8 16 24 32 40 48 56 64 72 80 88 96 104 112 120 \n\n 9 | 9 18 27 36 45 54 63 72 81 90 99 108 117 126 135 \n\n10 | 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 \n\n11 | 11 22 33 44 55 66 77 88 99 110 121 132 143 154 165 \n\n12 | 12 24 36 48 60 72 84 96 108 120 132 144 156 168 180 \n\n13 | 13 26 39 52 65 78 91 104 117 130 143 156 169 182 195 \n\n14 | 14 28 42 56 70 84 98 112 126 140 154 168 182 196 210 \n\n15 | 15 30 45 60 75 90 105 120 135 150 165 180 195 210 225",
521521
},
522+
.{
523+
.main_file = "100_for4.zig",
524+
.output = "Arrays match!",
525+
},
522526
.{
523527
.main_file = "999_the_end.zig",
524528
.output = "\nThis is the end for now!\nWe hope you had fun and were able to learn a lot, so visit us again when the next exercises are available.",

exercises/100_for4.zig

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// We've seen that the 'for' loop can let us perform some action
3+
// for every item in an array or slice.
4+
//
5+
// More recently, we discovered that it supports ranges to
6+
// iterate over number sequences.
7+
//
8+
// This is part of a more general capability of the `for` loop:
9+
// looping over one or more "objects" where an object is an
10+
// array, slice, or range.
11+
//
12+
// In fact, we *did* use multiple objects way back in Exercise
13+
// 016 where we iterated over an array and also a numeric index.
14+
// It didn't always work exactly this way, so the exercise had to
15+
// be retroactively modified a little bit.
16+
//
17+
// for (bits, 0..) |bit, i| { ... }
18+
//
19+
// The general form of a 'for' loop with two lists is:
20+
//
21+
// for (list_a, list_b) |a, b| {
22+
// // Here we have the first item from list_a and list_b,
23+
// // then the second item from each, then the third and
24+
// // so forth...
25+
// }
26+
//
27+
// What's really beautiful about this is that we don't have to
28+
// keep track of an index or advancing a memory pointer for
29+
// *either* of these lists. That error-prone stuff is all taken
30+
// care of for us by the compiler.
31+
//
32+
// Below, we have a program that is supposed to compare two
33+
// arrays. Please make it work!
34+
//
35+
const std = @import("std");
36+
const print = std.debug.print;
37+
38+
pub fn main() void {
39+
const hex_nums = [_]u8{ 0xb, 0x2a, 0x77 };
40+
const dec_nums = [_]u8{ 11, 42, 119 };
41+
42+
for (hex_nums, ???) |hn, ???| {
43+
if (hn != dn) {
44+
std.debug.print("Uh oh! Found a mismatch: {d} vs {d}\n", .{ hn, dn });
45+
return;
46+
}
47+
}
48+
49+
std.debug.print("Arrays match!\n", .{});
50+
}
51+
//
52+
// You are perhaps wondering what happens if one of the two lists
53+
// is longer than the other? Try it!
54+
//
55+
// By the way, congratulations for making it to Exercise 100!
56+
//
57+
// +-------------+
58+
// | Celebration |
59+
// | Area * * * |
60+
// +-------------+
61+
//
62+
// Please keep your celebrating within the area provided.

patches/patches/100_for4.patch

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
42c42
2+
< for (hex_nums, ???) |hn, ???| {
3+
---
4+
> for (hex_nums, dec_nums) |hn, dn| {

0 commit comments

Comments
 (0)