mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-21 22:33:11 -05:00
Compare commits
2 commits
852758eb9c
...
d2de0534d9
Author | SHA1 | Date | |
---|---|---|---|
d2de0534d9 | |||
e16700fbea |
12 changed files with 673 additions and 0 deletions
19
zig/luhn/.exercism/config.json
Normal file
19
zig/luhn/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"ee7"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"luhn.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_luhn.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Given a number determine whether or not it is valid per the Luhn formula.",
|
||||||
|
"source": "The Luhn Algorithm on Wikipedia",
|
||||||
|
"source_url": "https://en.wikipedia.org/wiki/Luhn_algorithm"
|
||||||
|
}
|
1
zig/luhn/.exercism/metadata.json
Normal file
1
zig/luhn/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"luhn","id":"5c3683c495e143e98cf457b99607c9fb","url":"https://exercism.org/tracks/zig/exercises/luhn","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/luhn/HELP.md
Normal file
53
zig/luhn/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 luhn.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
|
79
zig/luhn/README.md
Normal file
79
zig/luhn/README.md
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
# Luhn
|
||||||
|
|
||||||
|
Welcome to Luhn on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Given a number determine whether or not it is valid per the Luhn formula.
|
||||||
|
|
||||||
|
The [Luhn algorithm][luhn] is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers and Canadian Social Insurance Numbers.
|
||||||
|
|
||||||
|
The task is to check if a given string is valid.
|
||||||
|
|
||||||
|
## Validating a Number
|
||||||
|
|
||||||
|
Strings of length 1 or less are not valid.
|
||||||
|
Spaces are allowed in the input, but they should be stripped before checking.
|
||||||
|
All other non-digit characters are disallowed.
|
||||||
|
|
||||||
|
### Example 1: valid credit card number
|
||||||
|
|
||||||
|
```text
|
||||||
|
4539 3195 0343 6467
|
||||||
|
```
|
||||||
|
|
||||||
|
The first step of the Luhn algorithm is to double every second digit, starting from the right.
|
||||||
|
We will be doubling
|
||||||
|
|
||||||
|
```text
|
||||||
|
4_3_ 3_9_ 0_4_ 6_6_
|
||||||
|
```
|
||||||
|
|
||||||
|
If doubling the number results in a number greater than 9 then subtract 9 from the product.
|
||||||
|
The results of our doubling:
|
||||||
|
|
||||||
|
```text
|
||||||
|
8569 6195 0383 3437
|
||||||
|
```
|
||||||
|
|
||||||
|
Then sum all of the digits:
|
||||||
|
|
||||||
|
```text
|
||||||
|
8+5+6+9+6+1+9+5+0+3+8+3+3+4+3+7 = 80
|
||||||
|
```
|
||||||
|
|
||||||
|
If the sum is evenly divisible by 10, then the number is valid.
|
||||||
|
This number is valid!
|
||||||
|
|
||||||
|
### Example 2: invalid credit card number
|
||||||
|
|
||||||
|
```text
|
||||||
|
8273 1232 7352 0569
|
||||||
|
```
|
||||||
|
|
||||||
|
Double the second digits, starting from the right
|
||||||
|
|
||||||
|
```text
|
||||||
|
7253 2262 5312 0539
|
||||||
|
```
|
||||||
|
|
||||||
|
Sum the digits
|
||||||
|
|
||||||
|
```text
|
||||||
|
7+2+5+3+2+2+6+2+5+3+1+2+0+5+3+9 = 57
|
||||||
|
```
|
||||||
|
|
||||||
|
57 is not evenly divisible by 10, so this number is not valid.
|
||||||
|
|
||||||
|
[luhn]: https://en.wikipedia.org/wiki/Luhn_algorithm
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @ee7
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
The Luhn Algorithm on Wikipedia - https://en.wikipedia.org/wiki/Luhn_algorithm
|
29
zig/luhn/luhn.zig
Normal file
29
zig/luhn/luhn.zig
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const ascii = std.ascii;
|
||||||
|
|
||||||
|
pub fn isValid(s: []const u8) bool {
|
||||||
|
var idx: usize = s.len;
|
||||||
|
var luhn_idx: usize = 1;
|
||||||
|
var total: u64 = 0;
|
||||||
|
|
||||||
|
while (idx > 0) {
|
||||||
|
idx -= 1;
|
||||||
|
if (s[idx] == 32) continue;
|
||||||
|
if (!ascii.isDigit(s[idx])) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var curr: u8 = s[idx] - 48;
|
||||||
|
if (luhn_idx % 2 == 0) {
|
||||||
|
curr *= 2;
|
||||||
|
if (curr > 9) {
|
||||||
|
curr -= 9;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
total += curr;
|
||||||
|
luhn_idx += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (luhn_idx <= 2) return false;
|
||||||
|
return total % 10 == 0;
|
||||||
|
}
|
92
zig/luhn/test_luhn.zig
Normal file
92
zig/luhn/test_luhn.zig
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const isValid = @import("luhn.zig").isValid;
|
||||||
|
|
||||||
|
test "single digit strings cannot be valid" {
|
||||||
|
try testing.expect(!isValid("1"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a single zero is invalid" {
|
||||||
|
try testing.expect(!isValid("0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a simple valid SIN that remains valid if reversed" {
|
||||||
|
try testing.expect(isValid("059"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a simple valid SIN that becomes invalid if reversed" {
|
||||||
|
try testing.expect(isValid("59"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a valid Canadian SIN" {
|
||||||
|
try testing.expect(isValid("055 444 285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid Canadian SIN" {
|
||||||
|
try testing.expect(!isValid("055 444 286"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid credit card" {
|
||||||
|
try testing.expect(!isValid("8273 1232 7352 0569"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid long number with an even remainder" {
|
||||||
|
try testing.expect(!isValid("1 2345 6789 1234 5678 9012"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "invalid long number with a remainder divisible by 5" {
|
||||||
|
try testing.expect(!isValid("1 2345 6789 1234 5678 9013"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid number with an even number of digits" {
|
||||||
|
try testing.expect(isValid("095 245 88"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid number with an odd number of spaces" {
|
||||||
|
try testing.expect(isValid("234 567 891 234"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid strings with a non-digit added at the end become invalid" {
|
||||||
|
try testing.expect(!isValid("059a"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid strings with punctuation included become invalid" {
|
||||||
|
try testing.expect(!isValid("055-444-285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid strings with symbols included become invalid" {
|
||||||
|
try testing.expect(!isValid("055# 444$ 285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "single zero with space is invalid" {
|
||||||
|
try testing.expect(!isValid(" 0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "more than a single zero is valid" {
|
||||||
|
try testing.expect(isValid("0000 0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "input digit 9 is correctly converted to output digit 9" {
|
||||||
|
try testing.expect(isValid("091"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "very long input is valid" {
|
||||||
|
try testing.expect(isValid("9999999999 9999999999 9999999999 9999999999"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "valid luhn with an odd number of digits and non zero first digit" {
|
||||||
|
try testing.expect(isValid("109"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "using ascii value for non-doubled non-digit isn't allowed" {
|
||||||
|
try testing.expect(!isValid("055b 444 285"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "using ascii value for doubled non-digit isn't allowed" {
|
||||||
|
try testing.expect(!isValid(":9"));
|
||||||
|
}
|
||||||
|
|
||||||
|
test "non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed" {
|
||||||
|
try testing.expect(!isValid("59%59"));
|
||||||
|
}
|
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