mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-11-14 07:10:48 -05:00
Zig: completed Pig Latin
This commit is contained in:
parent
e16700fbea
commit
d2de0534d9
6 changed files with 400 additions and 0 deletions
19
zig/pig-latin/.exercism/config.json
Normal file
19
zig/pig-latin/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"keiravillekode"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"pig_latin.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_pig_latin.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Implement a program that translates from English to Pig Latin.",
|
||||||
|
"source": "The Pig Latin exercise at Test First Teaching by Ultrasaurus",
|
||||||
|
"source_url": "https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/"
|
||||||
|
}
|
1
zig/pig-latin/.exercism/metadata.json
Normal file
1
zig/pig-latin/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"pig-latin","id":"d23f71cbaf584a0f98b05a1a255df945","url":"https://exercism.org/tracks/zig/exercises/pig-latin","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/pig-latin/HELP.md
Normal file
53
zig/pig-latin/HELP.md
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
# Help
|
||||||
|
|
||||||
|
## Running the tests
|
||||||
|
|
||||||
|
Write your code in `<exercise_name>.zig`.
|
||||||
|
|
||||||
|
To run the tests for an exercise, run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
zig test test_exercise_name.zig
|
||||||
|
```
|
||||||
|
|
||||||
|
in the exercise's root directory (replacing `exercise_name` with the name of the exercise).
|
||||||
|
|
||||||
|
## Submitting your solution
|
||||||
|
|
||||||
|
You can submit your solution using the `exercism submit pig_latin.zig` command.
|
||||||
|
This command will upload your solution to the Exercism website and print the solution page's URL.
|
||||||
|
|
||||||
|
It's possible to submit an incomplete solution which allows you to:
|
||||||
|
|
||||||
|
- See how others have completed the exercise
|
||||||
|
- Request help from a mentor
|
||||||
|
|
||||||
|
## Need to get help?
|
||||||
|
|
||||||
|
If you'd like help solving the exercise, check the following pages:
|
||||||
|
|
||||||
|
- The [Zig track's documentation](https://exercism.org/docs/tracks/zig)
|
||||||
|
- The [Zig track's programming category on the forum](https://forum.exercism.org/c/programming/zig)
|
||||||
|
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
|
||||||
|
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
|
||||||
|
|
||||||
|
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
|
||||||
|
|
||||||
|
- [The Zig Programming Language Documentation][documentation] is a great overview of all of the language features that Zig provides to those who use it.
|
||||||
|
- [Zig Guide][zig-guide] is an excellent primer that explains the language features that Zig has to offer.
|
||||||
|
- [Ziglings][ziglings] is highly recommended.
|
||||||
|
Learn Zig by fixing tiny broken programs.
|
||||||
|
- [The Zig Programming Language Discord][discord-zig] is the main [Discord][discord].
|
||||||
|
It provides a great way to get in touch with the Zig community at large, and get some quick, direct help for any Zig related problem.
|
||||||
|
- [#zig][irc] on irc.freenode.net is the main Zig IRC channel.
|
||||||
|
- [/r/Zig][reddit] is the main Zig subreddit.
|
||||||
|
- [Stack Overflow][stack-overflow] can be used to discover code snippets and solutions to problems that may have already asked and maybe solved by others.
|
||||||
|
|
||||||
|
[discord]: https://discordapp.com
|
||||||
|
[discord-zig]: https://discord.com/invite/gxsFFjE
|
||||||
|
[documentation]: https://ziglang.org/documentation/master
|
||||||
|
[irc]: https://webchat.freenode.net/?channels=%23zig
|
||||||
|
[reddit]: https://www.reddit.com/r/Zig
|
||||||
|
[stack-overflow]: https://stackoverflow.com/questions/tagged/zig
|
||||||
|
[zig-guide]: https://zig.guide/
|
||||||
|
[ziglings]: https://codeberg.org/ziglings/exercises
|
70
zig/pig-latin/README.md
Normal file
70
zig/pig-latin/README.md
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
# Pig Latin
|
||||||
|
|
||||||
|
Welcome to Pig Latin on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Your parents have challenged you and your sibling to a game of two-on-two basketball.
|
||||||
|
Confident they'll win, they let you score the first couple of points, but then start taking over the game.
|
||||||
|
Needing a little boost, you start speaking in [Pig Latin][pig-latin], which is a made-up children's language that's difficult for non-children to understand.
|
||||||
|
This will give you the edge to prevail over your parents!
|
||||||
|
|
||||||
|
[pig-latin]: https://en.wikipedia.org/wiki/Pig_latin
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Your task is to translate text from English to Pig Latin.
|
||||||
|
The translation is defined using four rules, which look at the pattern of vowels and consonants at the beginning of a word.
|
||||||
|
These rules look at each word's use of vowels and consonants:
|
||||||
|
|
||||||
|
- vowels: the letters `a`, `e`, `i`, `o`, and `u`
|
||||||
|
- consonants: the other 21 letters of the English alphabet
|
||||||
|
|
||||||
|
## Rule 1
|
||||||
|
|
||||||
|
If a word begins with a vowel, or starts with `"xr"` or `"yt"`, add an `"ay"` sound to the end of the word.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
- `"apple"` -> `"appleay"` (starts with vowel)
|
||||||
|
- `"xray"` -> `"xrayay"` (starts with `"xr"`)
|
||||||
|
- `"yttria"` -> `"yttriaay"` (starts with `"yt"`)
|
||||||
|
|
||||||
|
## Rule 2
|
||||||
|
|
||||||
|
If a word begins with one or more consonants, first move those consonants to the end of the word and then add an `"ay"` sound to the end of the word.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
- `"pig"` -> `"igp"` -> `"igpay"` (starts with single consonant)
|
||||||
|
- `"chair"` -> `"airch"` -> `"airchay"` (starts with multiple consonants)
|
||||||
|
- `"thrush"` -> `"ushthr"` -> `"ushthray"` (starts with multiple consonants)
|
||||||
|
|
||||||
|
## Rule 3
|
||||||
|
|
||||||
|
If a word starts with zero or more consonants followed by `"qu"`, first move those consonants (if any) and the `"qu"` part to the end of the word, and then add an `"ay"` sound to the end of the word.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
- `"quick"` -> `"ickqu"` -> `"ickquay"` (starts with `"qu"`, no preceding consonants)
|
||||||
|
- `"square"` -> `"aresqu"` -> `"aresquay"` (starts with one consonant followed by `"qu`")
|
||||||
|
|
||||||
|
## Rule 4
|
||||||
|
|
||||||
|
If a word starts with one or more consonants followed by `"y"`, first move the consonants preceding the `"y"`to the end of the word, and then add an `"ay"` sound to the end of the word.
|
||||||
|
|
||||||
|
Some examples:
|
||||||
|
|
||||||
|
- `"my"` -> `"ym"` -> `"ymay"` (starts with single consonant followed by `"y"`)
|
||||||
|
- `"rhythm"` -> `"ythmrh"` -> `"ythmrhay"` (starts with multiple consonants followed by `"y"`)
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @keiravillekode
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
The Pig Latin exercise at Test First Teaching by Ultrasaurus - https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/
|
99
zig/pig-latin/pig_latin.zig
Normal file
99
zig/pig-latin/pig_latin.zig
Normal file
|
@ -0,0 +1,99 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const mem = std.mem;
|
||||||
|
|
||||||
|
const rule1 = [_][]const u8{ "a", "e", "i", "o", "u", "xr", "yt", "ay" };
|
||||||
|
const vowels = "aeiou";
|
||||||
|
|
||||||
|
fn rule_one(res: *std.ArrayList(u8), phrase: []const u8) mem.Allocator.Error!bool {
|
||||||
|
for (rule1) |r| {
|
||||||
|
if (mem.eql(u8, r, &.{phrase[0]}) or mem.eql(u8, r, phrase[0..2])) {
|
||||||
|
try res.appendSlice("ay");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rule_two(res: *std.ArrayList(u8), phrase: []const u8) mem.Allocator.Error!bool {
|
||||||
|
for (0..phrase.len - 1) |i| {
|
||||||
|
if (!mem.containsAtLeast(u8, vowels, 1, &[_]u8{phrase[i]})) {
|
||||||
|
try res.appendSlice(&.{res.orderedRemove(mem.indexOf(u8, phrase, &[_]u8{phrase[0]}).?)});
|
||||||
|
} else {
|
||||||
|
try res.appendSlice("ay");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rule_three(res: *std.ArrayList(u8), phrase: []const u8) mem.Allocator.Error!bool {
|
||||||
|
var prev: u8 = 0;
|
||||||
|
|
||||||
|
for (0..phrase.len - 1) |i| {
|
||||||
|
if ((prev == 'q' and phrase[i] == 'u') or !mem.containsAtLeast(u8, vowels, 1, &[_]u8{phrase[i]})) {
|
||||||
|
try res.appendSlice(&.{res.orderedRemove(mem.indexOf(u8, phrase, &[_]u8{phrase[0]}).?)});
|
||||||
|
} else {
|
||||||
|
try res.appendSlice("ay");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
prev = phrase[i];
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Because Yttrium Oxide ("yttria") comes up all the time in children's games
|
||||||
|
fn rule_four(res: *std.ArrayList(u8), phrase: []const u8) mem.Allocator.Error!bool {
|
||||||
|
var i: usize = 0;
|
||||||
|
|
||||||
|
while (phrase[i] != 'y' and i < phrase.len) : (i += 1) {
|
||||||
|
try res.appendSlice(&.{res.orderedRemove(mem.indexOf(u8, phrase, &[_]u8{phrase[0]}).?)});
|
||||||
|
}
|
||||||
|
|
||||||
|
try res.appendSlice("ay");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn translate_single(ar: *std.ArrayList(u8), phrase: []const u8) mem.Allocator.Error!void {
|
||||||
|
try ar.appendSlice(phrase);
|
||||||
|
if (try rule_one(ar, phrase)) return;
|
||||||
|
|
||||||
|
ar.clearRetainingCapacity();
|
||||||
|
try ar.appendSlice(phrase);
|
||||||
|
if (try rule_three(ar, phrase)) return;
|
||||||
|
|
||||||
|
ar.clearRetainingCapacity();
|
||||||
|
try ar.appendSlice(phrase);
|
||||||
|
if (try rule_two(ar, phrase)) return;
|
||||||
|
|
||||||
|
ar.clearRetainingCapacity();
|
||||||
|
try ar.appendSlice(phrase);
|
||||||
|
_ = try rule_four(ar, phrase);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate(allocator: mem.Allocator, phrase: []const u8) mem.Allocator.Error![]u8 {
|
||||||
|
var it = mem.splitScalar(u8, phrase, ' ');
|
||||||
|
const first = it.first();
|
||||||
|
|
||||||
|
var res = std.ArrayList(u8).init(allocator);
|
||||||
|
try translate_single(&res, first);
|
||||||
|
|
||||||
|
if (it.peek() == null) {
|
||||||
|
return res.toOwnedSlice();
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf: [32]u8 = undefined;
|
||||||
|
var fba = std.heap.FixedBufferAllocator.init(&buf);
|
||||||
|
const fba_allocator = fba.allocator();
|
||||||
|
|
||||||
|
try res.appendSlice(" ");
|
||||||
|
|
||||||
|
while (it.next()) |word| {
|
||||||
|
var tmp = std.ArrayList(u8).init(fba_allocator);
|
||||||
|
try translate_single(&tmp, word);
|
||||||
|
try res.appendSlice(try tmp.toOwnedSlice());
|
||||||
|
try res.appendSlice(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = res.pop();
|
||||||
|
return res.toOwnedSlice();
|
||||||
|
}
|
158
zig/pig-latin/test_pig_latin.zig
Normal file
158
zig/pig-latin/test_pig_latin.zig
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const pig_latin = @import("pig_latin.zig");
|
||||||
|
|
||||||
|
test "ay is added to words that start with vowels-word beginning with a" {
|
||||||
|
const expected: []const u8 = "appleay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "apple");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ay is added to words that start with vowels-word beginning with e" {
|
||||||
|
const expected: []const u8 = "earay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "ear");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ay is added to words that start with vowels-word beginning with i" {
|
||||||
|
const expected: []const u8 = "iglooay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "igloo");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ay is added to words that start with vowels-word beginning with o" {
|
||||||
|
const expected: []const u8 = "objectay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "object");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ay is added to words that start with vowels-word beginning with u" {
|
||||||
|
const expected: []const u8 = "underay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "under");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "ay is added to words that start with vowels-word beginning with a vowel and followed by a qu" {
|
||||||
|
const expected: []const u8 = "equalay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "equal");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "first letter and ay are moved to the end of words that start with consonants-word beginning with p" {
|
||||||
|
const expected: []const u8 = "igpay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "pig");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "first letter and ay are moved to the end of words that start with consonants-word beginning with k" {
|
||||||
|
const expected: []const u8 = "oalakay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "koala");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "first letter and ay are moved to the end of words that start with consonants-word beginning with x" {
|
||||||
|
const expected: []const u8 = "enonxay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "xenon");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "first letter and ay are moved to the end of words that start with consonants-word beginning with q without a following u" {
|
||||||
|
const expected: []const u8 = "atqay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "qat");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single consonant-word beginning with ch" {
|
||||||
|
const expected: []const u8 = "airchay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "chair");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single consonant-word beginning with qu" {
|
||||||
|
const expected: []const u8 = "eenquay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "queen");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single consonant-word beginning with qu and a preceding consonant" {
|
||||||
|
const expected: []const u8 = "aresquay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "square");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single consonant-word beginning with th" {
|
||||||
|
const expected: []const u8 = "erapythay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "therapy");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single consonant-word beginning with thr" {
|
||||||
|
const expected: []const u8 = "ushthray";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "thrush");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single consonant-word beginning with sch" {
|
||||||
|
const expected: []const u8 = "oolschay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "school");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single vowel-word beginning with yt" {
|
||||||
|
const expected: []const u8 = "yttriaay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "yttria");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "some letter clusters are treated like a single vowel-word beginning with xr" {
|
||||||
|
const expected: []const u8 = "xrayay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "xray");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "position of y in a word determines if it is a consonant or a vowel-y is treated like a consonant at the beginning of a word" {
|
||||||
|
const expected: []const u8 = "ellowyay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "yellow");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "position of y in a word determines if it is a consonant or a vowel-y is treated like a vowel at the end of a consonant cluster" {
|
||||||
|
const expected: []const u8 = "ythmrhay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "rhythm");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "position of y in a word determines if it is a consonant or a vowel-y as second letter in two letter word" {
|
||||||
|
const expected: []const u8 = "ymay";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "my");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "phrases are translated-a whole phrase" {
|
||||||
|
const expected: []const u8 = "ickquay astfay unray";
|
||||||
|
const actual = try pig_latin.translate(testing.allocator, "quick fast run");
|
||||||
|
defer testing.allocator.free(actual);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
Loading…
Reference in a new issue