mirror of
https://codeberg.org/ziglings/exercises.git
synced 2026-07-28 18:25:16 +00:00
Compare commits
15 Commits
151db7bcd4
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f39b462659 | |||
| a2eee9d52f | |||
| 248093a2de | |||
| 004ea3b3ae | |||
| c85daab1a1 | |||
| f6c3e4edc4 | |||
| a5ed7b636b | |||
| ba99eea43b | |||
| 708436c63d | |||
| e65b359055 | |||
| 719f7f0740 | |||
| 9a1ff49206 | |||
| 89fe65c158 | |||
| 6e34e835e0 | |||
| a03b676bed |
@@ -30,3 +30,11 @@ pub fn main(init: std.process.Init) !void {
|
|||||||
// We just learned of a single statement which can accomplish this.
|
// We just learned of a single statement which can accomplish this.
|
||||||
stdout.print("Hello world!\n", .{});
|
stdout.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Now you must be thinking "Why would I need to do this instead of good
|
||||||
|
// old 'std.debug.print()'?", but here's what you need to understand:
|
||||||
|
// 'std.debug.print()' prints its output to stderr, while the stdout_writer
|
||||||
|
// approach prints to stdout.
|
||||||
|
// A common practice is to pipe output of a command to other commands,
|
||||||
|
// and in order for the piping to work, those commands expect their input
|
||||||
|
// to come from stdout, not stderr.
|
||||||
|
|||||||
@@ -28,9 +28,9 @@
|
|||||||
// We will not go into the formula and its derivation in detail, but
|
// We will not go into the formula and its derivation in detail, but
|
||||||
// will deal with the series straight away:
|
// will deal with the series straight away:
|
||||||
//
|
//
|
||||||
// 4 4 4 4 4
|
// 4 4 4 4 4
|
||||||
// PI = --- - --- + --- - --- + --- ...
|
// PI = + --- - --- + --- - --- + --- ...
|
||||||
// 1 3 5 7 9
|
// 1 3 5 7 9
|
||||||
//
|
//
|
||||||
// As you can clearly see, the series starts with the whole number 4 and
|
// As you can clearly see, the series starts with the whole number 4 and
|
||||||
// approaches the circle number by subtracting and adding smaller and
|
// approaches the circle number by subtracting and adding smaller and
|
||||||
@@ -71,27 +71,28 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const count = 1_000_000_000;
|
const count = 500_000_000;
|
||||||
|
|
||||||
var pi_plus: f64 = 0;
|
var pi_plus: f64 = 0;
|
||||||
var pi_minus: f64 = 0;
|
var pi_minus: f64 = 0;
|
||||||
|
|
||||||
{
|
{
|
||||||
// First thread to calculate the plus numbers.
|
// First thread to calculate the plus numbers.
|
||||||
const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 5, count });
|
const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 1, count });
|
||||||
defer handle1.join();
|
defer handle1.join();
|
||||||
|
|
||||||
// Second thread to calculate the minus numbers.
|
// Second thread to calculate the minus numbers.
|
||||||
???
|
???
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here we add up the results.
|
// Here we add up the results.
|
||||||
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
|
std.debug.print("PI ≈ {d:.8} (error = {e:.1})\n", .{ pi_plus - pi_minus, std.math.pi - (pi_plus - pi_minus) });
|
||||||
}
|
}
|
||||||
|
|
||||||
fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
|
fn thread_pi(pi: *f64, begin: u64, count: u64) !void {
|
||||||
var n: u64 = begin;
|
for (0..count) |i| {
|
||||||
while (n < end) : (n += 4) {
|
const term = 4 / @as(f64, @floatFromInt(begin + 4 * i));
|
||||||
pi.* += 4 / @as(f64, @floatFromInt(n));
|
pi.* += term;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If you wish, you can increase the number of loop passes, which
|
// If you wish, you can increase the number of loop passes, which
|
||||||
@@ -103,8 +104,14 @@ fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
|
|||||||
// is created. Otherwise the debug functions slow down the speed
|
// is created. Otherwise the debug functions slow down the speed
|
||||||
// to such an extent that seconds become minutes during execution.
|
// to such an extent that seconds become minutes during execution.
|
||||||
//
|
//
|
||||||
|
// You can use the following command to build and run this
|
||||||
|
// exercise with the "ReleaseFast" flag:
|
||||||
|
//
|
||||||
|
// zig run exercises/108_threading2.zig -O ReleaseFast
|
||||||
|
//
|
||||||
// And you should remove the formatting restriction in "print",
|
// And you should remove the formatting restriction in "print",
|
||||||
// otherwise you will not be able to see the additional digits.
|
// otherwise you will not be able to see the additional digits.
|
||||||
//
|
//
|
||||||
// If count = 10_000_000_000_000 you should see the following:
|
// If count = 2_500_000_000_000, you should see the following:
|
||||||
// 3.141592653589
|
// 3.141592653589
|
||||||
|
// (after waiting on the order of 1000 seconds, or 17 minutes)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
--- exercises/026_hello2.zig 2026-01-09 22:51:45.803358789 +0100
|
--- exercises/026_hello2.zig 2026-07-01 16:27:18.674459266 +0200
|
||||||
+++ answers/026_hello2.zig 2026-01-09 22:50:46.016166527 +0100
|
+++ answers/026_hello2.zig 2026-07-01 16:27:08.579220702 +0200
|
||||||
@@ -28,5 +28,5 @@
|
@@ -28,7 +28,7 @@
|
||||||
// to be able to pass it up as a return value of main().
|
// to be able to pass it up as a return value of main().
|
||||||
//
|
//
|
||||||
// We just learned of a single statement which can accomplish this.
|
// We just learned of a single statement which can accomplish this.
|
||||||
- stdout.print("Hello world!\n", .{});
|
- stdout.print("Hello world!\n", .{});
|
||||||
+ try stdout.print("Hello world!\n", .{});
|
+ try stdout.print("Hello world!\n", .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Now you must be thinking "Why would I need to do this instead of good
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
--- exercises/036_enums2.zig 2023-10-03 22:15:22.122241138 +0200
|
--- exercises/036_enums2.zig 2026-07-21 20:48:50.596099259 +0200
|
||||||
+++ answers/036_enums2.zig 2023-10-05 20:04:07.002765884 +0200
|
+++ answers/036_enums2.zig 2026-07-21 20:50:14.726826859 +0200
|
||||||
@@ -31,7 +31,7 @@
|
@@ -31,7 +31,7 @@
|
||||||
const Color = enum(u32) {
|
const Color = enum(u32) {
|
||||||
red = 0xff0000,
|
red = 0xff0000,
|
||||||
@@ -18,9 +18,11 @@
|
|||||||
\\</p>
|
\\</p>
|
||||||
\\
|
\\
|
||||||
, .{
|
, .{
|
||||||
@intFromEnum(Color.red),
|
- @intFromEnum(Color.red),
|
||||||
@intFromEnum(Color.green),
|
- @intFromEnum(Color.green),
|
||||||
- @intFromEnum(???), // Oops! We're missing something!
|
- @intFromEnum(???), // Oops! We're missing something!
|
||||||
+ @intFromEnum(Color.blue), // Oops! We're missing something!
|
+ @backingInt(Color.red),
|
||||||
|
+ @backingInt(Color.green),
|
||||||
|
+ @backingInt(Color.blue), // Oops! We're missing something!
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
--- exercises/108_threading2.zig 2026-04-14 06:44:18.848246237 +0200
|
--- exercises/108_threading2.zig 2026-06-29 23:25:53
|
||||||
+++ answers/108_threading2.zig 2026-04-14 08:15:30.894485037 +0200
|
+++ answers/108_threading2.zig 2026-06-29 23:25:25
|
||||||
@@ -81,8 +81,8 @@
|
@@ -82,7 +82,8 @@
|
||||||
defer handle1.join();
|
defer handle1.join();
|
||||||
|
|
||||||
// Second thread to calculate the minus numbers.
|
// Second thread to calculate the minus numbers.
|
||||||
- ???
|
- ???
|
||||||
-
|
|
||||||
+ const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
|
+ const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
|
||||||
+ defer handle2.join();
|
+ defer handle2.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Here we add up the results.
|
// Here we add up the results.
|
||||||
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
|
|
||||||
|
|||||||
@@ -359,7 +359,6 @@ fn runExe(ctx: Context, ex: Exercise) !void {
|
|||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
resetLine();
|
|
||||||
print("Checking {s}...\n", .{ex.main_file});
|
print("Checking {s}...\n", .{ex.main_file});
|
||||||
|
|
||||||
return checkOutput(io, arena, ex, result);
|
return checkOutput(io, arena, ex, result);
|
||||||
@@ -392,7 +391,6 @@ fn runTest(ctx: Context, ex: Exercise) !void {
|
|||||||
return err;
|
return err;
|
||||||
};
|
};
|
||||||
|
|
||||||
resetLine();
|
|
||||||
print("Checking {s}...\n", .{ex.main_file});
|
print("Checking {s}...\n", .{ex.main_file});
|
||||||
|
|
||||||
return checkTest(ex, result);
|
return checkTest(ex, result);
|
||||||
@@ -486,10 +484,6 @@ fn help(ex: Exercise, mode: Mode) void {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resetLine() void {
|
|
||||||
if (stderr_term_mode == .escape_codes) print("{s}", .{"\x1b[2K\r"});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Removes trailing whitespace per line and any trailing LF at the end.
|
// Removes trailing whitespace per line and any trailing LF at the end.
|
||||||
fn trimLines(arena: std.mem.Allocator, buf: []const u8) ![]const u8 {
|
fn trimLines(arena: std.mem.Allocator, buf: []const u8) ![]const u8 {
|
||||||
var list = try std.ArrayList(u8).initCapacity(arena, buf.len);
|
var list = try std.ArrayList(u8).initCapacity(arena, buf.len);
|
||||||
@@ -1173,7 +1167,7 @@ const exercises = [_]Exercise{
|
|||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "108_threading2.zig",
|
.main_file = "108_threading2.zig",
|
||||||
.output = "PI ≈ 3.14159265",
|
.output = "PI ≈ 3.14159265 (error = 1.0e-9)",
|
||||||
},
|
},
|
||||||
.{
|
.{
|
||||||
.main_file = "109_files.zig",
|
.main_file = "109_files.zig",
|
||||||
|
|||||||
Reference in New Issue
Block a user