mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-06-08 07:50:00 +00:00
fixed removed array multiplication
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Zig has some fun array operators.
|
||||
// Zig has one array operators.
|
||||
//
|
||||
// You can use '++' to concatenate two arrays:
|
||||
//
|
||||
@@ -7,12 +7,8 @@
|
||||
// const b = [_]u8{ 3,4 };
|
||||
// const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5
|
||||
//
|
||||
// You can use '**' to repeat an array:
|
||||
//
|
||||
// const d = [_]u8{ 1,2,3 } ** 2; // equals 1 2 3 1 2 3
|
||||
//
|
||||
// Note that both '++' and '**' only operate on arrays while your
|
||||
// program is _being compiled_. This special time is known in Zig
|
||||
// Note that '++'' only operate on arrays while your program is
|
||||
// _being compiled_. This special time is known in Zig
|
||||
// parlance as "comptime" and we'll learn plenty more about that
|
||||
// later.
|
||||
//
|
||||
@@ -30,7 +26,8 @@ pub fn main() void {
|
||||
// (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_unit = [_]u8{ ??? };
|
||||
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.
|
||||
//
|
||||
|
||||
@@ -27,10 +27,6 @@ pub fn main() void {
|
||||
const d: u8 = ziggy[???];
|
||||
|
||||
// (Problem 2)
|
||||
// Use the array repeat '**' operator to make "ha ha ha ".
|
||||
const laugh = "ha " ???;
|
||||
|
||||
// (Problem 3)
|
||||
// Use the array concatenation '++' operator to make "Major Tom".
|
||||
// (You'll need to add a space as well!)
|
||||
const major = "Major";
|
||||
@@ -38,7 +34,7 @@ pub fn main() void {
|
||||
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 });
|
||||
std.debug.print("d={u} {s}\n", .{ d, major_tom });
|
||||
// Keen eyes will notice that we've put 'u' and 's' inside the '{}'
|
||||
// placeholders in the format string above. This tells the
|
||||
// print() function to format the values as a UTF-8 character and
|
||||
|
||||
@@ -224,11 +224,10 @@ const NotebookEntry = struct {
|
||||
// +---+----------------+----------------+----------+
|
||||
//
|
||||
const HermitsNotebook = struct {
|
||||
// Remember the array repetition operator `**`? It is no mere
|
||||
// novelty, it's also a great way to assign multiple items in an
|
||||
// array without having to list them one by one. Here we use it to
|
||||
// initialize an array with null values.
|
||||
entries: [place_count]?NotebookEntry = .{null} ** place_count,
|
||||
// Remember the array repetition function @splat()? It is a great way
|
||||
// to assign multiple items in an array without having to list them
|
||||
// one by one. Here we use it to initialize an array with null values.
|
||||
entries: [place_count]?NotebookEntry = @splat(null),
|
||||
|
||||
// The next entry keeps track of where we are in our "todo" list.
|
||||
next_entry: u8 = 0,
|
||||
@@ -409,7 +408,7 @@ pub fn main() void {
|
||||
// aside memory for the trip and have the hermit's notebook fill
|
||||
// in the trip from the destination back to the path. Note that
|
||||
// this is the first time we've actually used the destination!
|
||||
var trip = [_]?TripItem{null} ** (place_count * 2);
|
||||
var trip: [place_count * 2]?TripItem = @splat(null);
|
||||
|
||||
notebook.getTripTo(trip[0..], destination) catch |err| {
|
||||
print("Oh no! {}\n", .{err});
|
||||
|
||||
@@ -39,16 +39,16 @@ pub fn main() void {
|
||||
var count = 0;
|
||||
|
||||
count += 1;
|
||||
const a1: [count]u8 = .{'A'} ** count;
|
||||
const a1: [count]u8 = @splat('A');
|
||||
|
||||
count += 1;
|
||||
const a2: [count]u8 = .{'B'} ** count;
|
||||
const a2: [count]u8 = @splat('B');
|
||||
|
||||
count += 1;
|
||||
const a3: [count]u8 = .{'C'} ** count;
|
||||
const a3: [count]u8 = @splat('C');
|
||||
|
||||
count += 1;
|
||||
const a4: [count]u8 = .{'D'} ** count;
|
||||
const a4: [count]u8 = @splat('D');
|
||||
|
||||
print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 });
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ fn makeCreature(comptime count: usize, comptime fmt: []const u8) [count]Animal {
|
||||
|
||||
// We return an array of animals representing the creature. (This is why we
|
||||
// really needed the 'count' parameter. Arrays need a size.)
|
||||
var animals: [count]Animal = .{undefined} ** count;
|
||||
var animals: [count]Animal = undefined;
|
||||
var next_animal: usize = 0;
|
||||
|
||||
inline for (fmt) |char| {
|
||||
|
||||
@@ -48,9 +48,7 @@ const Path = struct {
|
||||
// instead.
|
||||
//
|
||||
// Please fill in the body of this function!
|
||||
fn makePath(from: *Place, to: *Place, dist: u8) Path {
|
||||
|
||||
}
|
||||
fn makePath(from: *Place, to: *Place, dist: u8) Path {}
|
||||
|
||||
// Using our new function, these path definitions take up considerably less
|
||||
// space in our program now!
|
||||
@@ -97,7 +95,7 @@ const NotebookEntry = struct {
|
||||
};
|
||||
|
||||
const HermitsNotebook = struct {
|
||||
entries: [place_count]?NotebookEntry = .{null} ** place_count,
|
||||
entries: [place_count]?NotebookEntry = @splat(null),
|
||||
next_entry: u8 = 0,
|
||||
end_of_entries: u8 = 0,
|
||||
|
||||
@@ -193,7 +191,7 @@ pub fn main() void {
|
||||
}
|
||||
}
|
||||
|
||||
var trip = [_]?TripItem{null} ** (place_count * 2);
|
||||
var trip: [place_count * 2]?TripItem = @splat(null);
|
||||
|
||||
notebook.getTripTo(trip[0..], destination) catch |err| {
|
||||
print("Oh no! {}\n", .{err});
|
||||
|
||||
Reference in New Issue
Block a user