Skip to content

Commit 4f2ea13

Browse files
committed
Merge branch 'main' of https://github.com/rtoal/ple
2 parents 407eeec + 8be2948 commit 4f2ea13

File tree

3 files changed

+131
-53
lines changed

3 files changed

+131
-53
lines changed

zig/README.md

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
<img src="https://raw.githubusercontent.com/rtoal/ple/main/docs/resources/zig-logo-64.png">
22

3-
# Zig
3+
# Zig Explorations
44

5-
## Zig Examples:
5+
To build and run Zig programs on your local machine, download and install Zig from the [Zig Download](https://ziglang.org/download/) or use your favorite package manager, e.g. `brew install zig` on macOS.
66

7-
To get Zig:
8-
<details><summary><b>MacOS</b></summary>
9-
10-
<br />In your terminal (Mac Terminal): <br />
7+
Once installed, programs in this folder can be run from the command line like so:
118

12-
After installing homebrew:
9+
```
10+
zig run triple.zig
11+
```
1312

14-
```sh
15-
$ brew install zig
16-
```
17-
</details>
18-
<details><summary><b>Windows/Linux</b></summary>
19-
20-
Download zig from the website:
21-
- [Zig Download](https://ziglang.org/download/)
13+
```
14+
zig run permutations.zig -- I like carrots
15+
```
2216

23-
</details>
17+
```
18+
zig run top_ten_scorers.zig < ../test/wnba_input
19+
```
2420

25-
## About Zig:
21+
## About Zig
2622

27-
The Zig language was founded by Andrew Kelley in 2016. It was created to replace the <a href="https://github.com/rtoal/ple/tree/main/c">C</a> programming language and be more pragmatic. Zig offers many advancements such as features to improve safety, simple syntax, and a testing framework built into the language. The language also provides strong compile-time guarantees and aims to eliminate common programming errors. While it is a newer language, many developers approve of its capabilities.
23+
The Zig language was created by Andrew Kelley in 2016, as a pragmatic alternative to the venerable <a href="https://github.com/rtoal/ple/tree/main/c">C</a> programming language. Zig offers many safety features and a growing ecosystem for building and testing. It is known for having “no surprises”: there are no hidden function calls and no hidden memory allocations. Unlike C, Zig has no preprocessor and no macros. Zig does add option types, generics, and custom allocators.
2824

29-
## Zig Resources:
25+
## Zig Resources
3026

3127
Continue your exploration of Zig via:
3228

3329
- [Zig Home Page](https://ziglang.org)
34-
- [Zig Docs](https://ziglang.org/documentation/0.10.1/)
30+
- [Zig Docs](https://ziglang.org/documentation/master/)
3531
- [Intro to Zig](https://www.youtube.com/watch?v=YXrb-DqsBNU)
36-
- [ZigLearn](https://ziglearn.org/chapter-1/)
37-
- [Zig Wiki](https://en.wikipedia.org/wiki/Zig_(programming_language))
32+
- [ZigLearn](https://ziglearn.org/learn/)
33+
- [Wikipedia](<https://en.wikipedia.org/wiki/Zig_(programming_language)>)
34+
- The book [Introduction to Zig](https://github.com/pedropark99/zig-book)
3835

3936
## Zig Open Source Projects
4037

41-
Studying, and contributing to, open source projects is an excellent way to improve your proficiency in any language. You may enjoy:
38+
Studying, and contributing to, open source projects is an excellent way to improve your proficiency in any language. Of the many projects using Zig, you may enjoy:
4239

43-
- [Zig Open Source](https://github.com/kennethreitz/requests)
40+
- [Trending Zig Repositories on GitHub](https://github.com/trending/zig)
41+
- [tigerbeetle](https://github.com/tigerbeetle/tigerbeetle)
42+
- [asciigen](https://github.com/seatedro/asciigen)
43+
44+
You can contribute to Zig itself:
45+
46+
- [Language Repository at GitHub](https://github.com/ziglang/zig)

zig/permuations.zig

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
const std = @import("std");
22

3-
fn printPermutations(a: []u8, n: usize) void {
4-
if (n <= 0) {
5-
std.debug.print("{s}\n", .{a});
6-
} else {
7-
var i: usize = 0;
8-
while (i < n) : (i += 1) {
9-
printPermutations(a, n - 1);
10-
const j = if (n % 2 == 0) 0 else i;
11-
12-
const temp = a[j];
13-
a[j] = a[n];
14-
a[n] = temp;
3+
fn printArray(a: []const []const u8) !void {
4+
var writer = std.io.getStdOut().writer();
5+
for (a, 0..) |item, i| {
6+
try writer.writeAll(item);
7+
if (i < a.len - 1) {
8+
try writer.writeAll("\t");
159
}
16-
printPermutations(a, n - 1);
1710
}
11+
try writer.writeAll("\n");
1812
}
1913

20-
pub fn main() anyerror!void {
21-
var args = try std.process.argsAlloc(std.heap.page_allocator);
22-
defer std.process.argsFree(std.heap.page_allocator, args);
14+
fn printPermutations(a: [][]u8, n: usize) !void {
15+
if (n == 0) {
16+
try printArray(a);
17+
return;
18+
}
2319

24-
// Skip the first argument which is the program's own path
25-
const start_index: usize = 1;
26-
const end_index: usize = args.len;
20+
try printPermutations(a, n - 1);
2721

28-
if (end_index >= start_index) {
29-
var buffer: [100]u8 = undefined;
30-
var a: []u8 = buffer[0 .. end_index - start_index];
22+
var i: usize = 0;
23+
while (i < n) : (i += 1) {
24+
const j: usize = if (n % 2 == 0) 0 else i;
25+
std.mem.swap([]const u8, &a[j], &a[n]);
26+
try printPermutations(a, n - 1);
27+
}
28+
}
3129

32-
// Copy the arguments to the buffer
33-
for (start_index..end_index) |i| {
34-
a[i - start_index] = args[i][0];
35-
}
30+
pub fn main() !void {
31+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
32+
const allocator = gpa.allocator();
3633

37-
// Calculate and print the permutations
38-
printPermutations(a, a.len - 1);
39-
}
34+
var args = try std.process.argsAlloc(allocator);
35+
defer std.process.argsFree(allocator, args);
36+
const a = try allocator.dupe([]u8, args[1..]);
37+
defer allocator.free(a);
38+
39+
try printPermutations(a, a.len - 1);
4040
}

zig/top_ten_scorers.zig

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const std = @import("std");
2+
const Allocator = std.mem.Allocator;
3+
4+
const Player = struct {
5+
name: []const u8,
6+
team: []const u8,
7+
ppg: f64,
8+
};
9+
10+
pub fn main() !void {
11+
const allocator = std.heap.page_allocator;
12+
const reader = std.io.getStdIn().reader();
13+
14+
// Dynamically allocated array of players
15+
var players = std.ArrayList(Player).init(allocator);
16+
17+
// Read lines from stdin
18+
var line: []u8 = undefined;
19+
while (reader.readUntilDelimiterOrEof(&line, '\n') catch return) {
20+
// Split line by commas
21+
const fields = splitByComma(line);
22+
if (fields.len != 4) {
23+
continue; // Skip invalid lines
24+
}
25+
26+
// Parse games and points
27+
const games = std.fmt.parseInt(i32, fields[2], 10) catch continue;
28+
const points = std.fmt.parseInt(i32, fields[3], 10) catch continue;
29+
30+
if (games < 15) {
31+
continue; // Ignore players with fewer than 15 games
32+
}
33+
34+
// Create a player and add to list
35+
const ppg = f64(points) / f64(games);
36+
try players.append(Player{
37+
.name = fields[1],
38+
.team = fields[0],
39+
.ppg = ppg,
40+
});
41+
}
42+
43+
// Sort players by ppg in descending order
44+
std.sort.sort(players.items, cmpPPG);
45+
46+
// Print top 10 players
47+
const count = std.math.min(10, players.items.len);
48+
for (players.items[0..count]) |player| {
49+
try std.debug.print("{:22}{:4}{:8.2}\n", .{ player.name, player.team, player.ppg });
50+
}
51+
}
52+
53+
// Split the input line by commas
54+
fn splitByComma(line: []const u8) []const []const u8 {
55+
var parts = std.ArrayList([]const u8).init(std.heap.page_allocator);
56+
57+
// Use splitScalar to split by comma
58+
var it = std.mem.splitScalar(u8, line, ',');
59+
while (it.next()) |part| {
60+
try parts.append(part);
61+
}
62+
63+
return parts.items;
64+
}
65+
66+
// Compare function for sorting players by ppg
67+
fn cmpPPG(a: *Player, b: *Player) i32 {
68+
if (a.ppg > b.ppg) {
69+
return 1;
70+
} else if (a.ppg < b.ppg) {
71+
return -1;
72+
} else {
73+
return 0;
74+
}
75+
}

0 commit comments

Comments
 (0)