add ex52 slices
This commit is contained in:
parent
363459de84
commit
d1c6993898
3 changed files with 63 additions and 1 deletions
|
@ -268,7 +268,10 @@ const exercises = [_]Exercise{
|
||||||
.main_file = "51_values.zig",
|
.main_file = "51_values.zig",
|
||||||
.output = "1:false!. 2:true!. 3:true!. XP before:0, after:200.",
|
.output = "1:false!. 2:true!. 3:true!. XP before:0, after:200.",
|
||||||
},
|
},
|
||||||
// 52 slices!
|
.{
|
||||||
|
.main_file = "52_slices.zig",
|
||||||
|
.output = "Hand1: A 4 K 8 Hand2: 5 2 Q J",
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Check the zig version to make sure it can compile the examples properly.
|
/// Check the zig version to make sure it can compile the examples properly.
|
||||||
|
|
49
exercises/52_slices.zig
Normal file
49
exercises/52_slices.zig
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
//
|
||||||
|
// We've seen that passing arrays around can be awkward. Perhaps you
|
||||||
|
// remember a particularly horrendous function definition from quiz3?
|
||||||
|
// This function can only take arrays that are exactly 4 items long!
|
||||||
|
//
|
||||||
|
// fn printPowersOfTwo(numbers: [4]u16) void { ... }
|
||||||
|
//
|
||||||
|
// That's the trouble with arrays - their size is part of the data
|
||||||
|
// type and must be hard-coded into every usage of that type. This
|
||||||
|
// digits array is a [10]u8 forever and ever:
|
||||||
|
//
|
||||||
|
// var digits = [10]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||||
|
//
|
||||||
|
// Thankfully, Zig has slices, which let you dynamically point to a
|
||||||
|
// start item and provide a length. Here are slices of our digit
|
||||||
|
// array:
|
||||||
|
//
|
||||||
|
// const foo = digits[0..1]; // 0
|
||||||
|
// const bar = digits[3..9]; // 3 4 5 6 7 8
|
||||||
|
// const all = digits[0..]; // 0 1 2 3 4 5 6 7 8 9
|
||||||
|
//
|
||||||
|
// As you can see, a slice [x..y] defines a first item by index x and
|
||||||
|
// a length y (where y-1 is the index of the last item). Leaving y off
|
||||||
|
// gives you the rest of the items.
|
||||||
|
//
|
||||||
|
// Notice that the type of a slice on an array of u8 items is []u8.
|
||||||
|
//
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
pub fn main() void {
|
||||||
|
var cards = [8]u8{ 'A', '4', 'K', '8', '5', '2', 'Q', 'J' };
|
||||||
|
|
||||||
|
// Please put the first 4 cards in hand1 and the rest in hand2.
|
||||||
|
const hand1: []u8 = cards[???];
|
||||||
|
const hand2: []u8 = cards[???];
|
||||||
|
|
||||||
|
std.debug.print("Hand1: ", .{});
|
||||||
|
printHand(hand1);
|
||||||
|
|
||||||
|
std.debug.print("Hand2: ", .{});
|
||||||
|
printHand(hand2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Please lend this function a hand. A u8 slice hand, that is.
|
||||||
|
fn printHand(hand: ???) void {
|
||||||
|
for (hand) |h| {
|
||||||
|
std.debug.print("{u} ", .{h});
|
||||||
|
}
|
||||||
|
}
|
10
patches/patches/52_slices.patch
Normal file
10
patches/patches/52_slices.patch
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
34,35c34,35
|
||||||
|
< const hand1: []u8 = cards[???];
|
||||||
|
< const hand2: []u8 = cards[???];
|
||||||
|
---
|
||||||
|
> const hand1: []u8 = cards[0..4];
|
||||||
|
> const hand2: []u8 = cards[4..];
|
||||||
|
45c45
|
||||||
|
< fn printHand(hand: ???) void {
|
||||||
|
---
|
||||||
|
> fn printHand(hand: []u8) void {
|
Loading…
Reference in a new issue