Zig: completed Pangram, Scrabble Score, and Armstrong Numbers

This commit is contained in:
Andrew Scott 2024-08-04 12:29:44 -04:00
parent 3f28e82a4e
commit 8fbf44fecb
Signed by: a
GPG key ID: 7CD5A5977E4931C1
18 changed files with 563 additions and 0 deletions

View file

@ -0,0 +1,19 @@
{
"authors": [
"ee7"
],
"files": {
"solution": [
"armstrong_numbers.zig"
],
"test": [
"test_armstrong_numbers.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Determine if a number is an Armstrong number.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Narcissistic_number"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"armstrong-numbers","id":"1ca0d9a30cd74f64a84eb3638bbd3ef3","url":"https://exercism.org/tracks/zig/exercises/armstrong-numbers","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View 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 armstrong_numbers.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

View file

@ -0,0 +1,29 @@
# Armstrong Numbers
Welcome to Armstrong Numbers on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits.
For example:
- 9 is an Armstrong number, because `9 = 9^1 = 9`
- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1`
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153`
- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
Write some code to determine whether a number is an Armstrong number.
[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number
## Source
### Created by
- @ee7
### Based on
Wikipedia - https://en.wikipedia.org/wiki/Narcissistic_number

View file

@ -0,0 +1,17 @@
const std = @import("std");
pub fn isArmstrongNumber(num: u128) bool {
var digits: u8 = 0;
var digits_rem = num;
while (digits_rem != 0) : (digits_rem /= 10) {
digits += 1;
}
var sum: u128 = 0;
var num_rem = num;
while (num_rem != 0) : (num_rem /= 10) {
sum += std.math.pow(u128, num_rem % 10, digits);
}
return num == sum;
}

View file

@ -0,0 +1,56 @@
const std = @import("std");
const testing = std.testing;
const isArmstrongNumber = @import("armstrong_numbers.zig").isArmstrongNumber;
test "zero is an armstrong number" {
try testing.expect(isArmstrongNumber(0));
}
test "single-digit numbers are armstrong numbers" {
try testing.expect(isArmstrongNumber(5));
}
test "there are no two-digit armstrong numbers" {
try testing.expect(!isArmstrongNumber(10));
}
test "three-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(153));
}
test "three-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(100));
}
test "four-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(9_474));
}
test "four-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(9_475));
}
test "seven-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(9_926_315));
}
test "seven-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(9_926_314));
}
test "33-digit number that is an armstrong number" {
try testing.expect(isArmstrongNumber(186_709_961_001_538_790_100_634_132_976_990));
}
test "38-digit number that is not an armstrong number" {
try testing.expect(!isArmstrongNumber(99_999_999_999_999_999_999_999_999_999_999_999_999));
}
test "the largest and last armstrong number" {
try testing.expect(isArmstrongNumber(115_132_219_018_763_992_565_095_597_973_971_522_401));
}
test "the largest 128-bit unsigned integer value is not an armstrong number" {
try testing.expect(!isArmstrongNumber(340_282_366_920_938_463_463_374_607_431_768_211_455));
}

View file

@ -0,0 +1,19 @@
{
"authors": [
"massivelivefun"
],
"files": {
"solution": [
"pangram.zig"
],
"test": [
"test_pangram.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Determine if a sentence is a pangram.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Pangram"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"pangram","id":"8ca6b789549d4f8fb857e7b5d0ecb09f","url":"https://exercism.org/tracks/zig/exercises/pangram","handle":"Chomp1295","is_requester":true,"auto_approve":false}

53
zig/pangram/HELP.md Normal file
View 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 pangram.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

40
zig/pangram/README.md Normal file
View file

@ -0,0 +1,40 @@
# Pangram
Welcome to Pangram on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Introduction
You work for a company that sells fonts through their website.
They'd like to show a different sentence each time someone views a font on their website.
To give a comprehensive sense of the font, the random sentences should use **all** the letters in the English alphabet.
They're running a competition to get suggestions for sentences that they can use.
You're in charge of checking the submissions to see if they are valid.
~~~~exercism/note
Pangram comes from Greek, παν γράμμα, pan gramma, which means "every letter".
The best known English pangram is:
> The quick brown fox jumps over the lazy dog.
~~~~
## Instructions
Your task is to figure out if a sentence is a pangram.
A pangram is a sentence using every letter of the alphabet at least once.
It is case insensitive, so it doesn't matter if a letter is lower-case (e.g. `k`) or upper-case (e.g. `K`).
For this exercise, a sentence is a pangram if it contains each of the 26 letters in the English alphabet.
## Source
### Created by
- @massivelivefun
### Based on
Wikipedia - https://en.wikipedia.org/wiki/Pangram

18
zig/pangram/pangram.zig Normal file
View file

@ -0,0 +1,18 @@
const ascii = @import("std").ascii;
pub fn isPangram(str: []const u8) bool {
var alphabet = [_]u8{0} ** 26;
for (str) |c| {
if (!ascii.isAlphabetic(c)) {
continue;
}
alphabet[ascii.toLower(c) - 97] += 1;
}
for (alphabet) |count| {
if (count == 0) return false;
}
return true;
}

View file

@ -0,0 +1,48 @@
const std = @import("std");
const testing = std.testing;
const pangram = @import("pangram.zig");
test "empty sentence" {
try testing.expect(!pangram.isPangram(""));
}
test "perfect lower case" {
try testing.expect(pangram.isPangram("abcdefghijklmnopqrstuvwxyz"));
}
test "only lower case" {
try testing.expect(pangram.isPangram("the quick brown fox jumps over the lazy dog"));
}
test "missing the letter 'x'" {
try testing.expect(!pangram.isPangram("a quick movement of the enemy will jeopardize five gunboats"));
}
test "missing the letter 'h'" {
try testing.expect(!pangram.isPangram("five boxing wizards jump quickly at it"));
}
test "with underscores" {
try testing.expect(pangram.isPangram("the_quick_brown_fox_jumps_over_the_lazy_dog"));
}
test "with numbers" {
try testing.expect(pangram.isPangram("the 1 quick brown fox jumps over the 2 lazy dogs"));
}
test "missing letters replaced by numbers" {
try testing.expect(!pangram.isPangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog"));
}
test "mixed case and punctuation" {
try testing.expect(pangram.isPangram("\"Five quacking Zephyrs jolt my wax bed.\""));
}
test "a-m and A-M are 26 different characters but not a pangram" {
try testing.expect(!pangram.isPangram("abcdefghijklm ABCDEFGHIJKLM"));
}
test "non-alphanumeric printable ASCII" {
try testing.expect(!pangram.isPangram(" !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"));
}

View file

@ -0,0 +1,19 @@
{
"authors": [
"ee7"
],
"files": {
"solution": [
"scrabble_score.zig"
],
"test": [
"test_scrabble_score.zig"
],
"example": [
".meta/example.zig"
]
},
"blurb": "Given a word, compute the Scrabble score for that word.",
"source": "Inspired by the Extreme Startup game",
"source_url": "https://github.com/rchatley/extreme_startup"
}

View file

@ -0,0 +1 @@
{"track":"zig","exercise":"scrabble-score","id":"bb07d3f27fd543c2875b5f72e0b0316e","url":"https://exercism.org/tracks/zig/exercises/scrabble-score","handle":"Chomp1295","is_requester":true,"auto_approve":false}

View 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 scrabble_score.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

View file

@ -0,0 +1,48 @@
# Scrabble Score
Welcome to Scrabble Score on Exercism's Zig Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Introduction
[Scrabble][wikipedia] is a word game where players place letter tiles on a board to form words.
Each letter has a value.
A word's score is the sum of its letters' values.
[wikipedia]: https://en.wikipedia.org/wiki/Scrabble
## Instructions
Your task is to compute a word's Scrabble score by summing the values of its letters.
The letters are valued as follows:
| Letter | Value |
| ---------------------------- | ----- |
| A, E, I, O, U, L, N, R, S, T | 1 |
| D, G | 2 |
| B, C, M, P | 3 |
| F, H, V, W, Y | 4 |
| K | 5 |
| J, X | 8 |
| Q, Z | 10 |
For example, the word "cabbage" is worth 14 points:
- 3 points for C
- 1 point for A
- 3 points for B
- 3 points for B
- 1 point for A
- 2 points for G
- 1 point for E
## Source
### Created by
- @ee7
### Based on
Inspired by the Extreme Startup game - https://github.com/rchatley/extreme_startup

View file

@ -0,0 +1,18 @@
const std = @import("std");
pub fn score(s: []const u8) u32 {
var total: u32 = 0;
for (s) |c| {
total += switch (std.ascii.toLower(c)) {
'a', 'e', 'i', 'o', 'u', 'l', 'n', 'r', 's', 't' => 1,
'd', 'g' => 2,
'b', 'c', 'm', 'p' => 3,
'f', 'h', 'v', 'w', 'y' => 4,
'k' => 5,
'j', 'x' => 8,
'q', 'z' => 10,
else => unreachable,
};
}
return total;
}

View file

@ -0,0 +1,70 @@
const std = @import("std");
const testing = std.testing;
const score = @import("scrabble_score.zig").score;
test "lowercase letter" {
const expected: u32 = 1;
const actual = score("a");
try testing.expectEqual(expected, actual);
}
test "uppercase letter" {
const expected: u32 = 1;
const actual = score("A");
try testing.expectEqual(expected, actual);
}
test "valuable letter" {
const expected: u32 = 4;
const actual = score("f");
try testing.expectEqual(expected, actual);
}
test "short word" {
const expected: u32 = 2;
const actual = score("at");
try testing.expectEqual(expected, actual);
}
test "short, valuable word" {
const expected: u32 = 12;
const actual = score("zoo");
try testing.expectEqual(expected, actual);
}
test "medium word" {
const expected: u32 = 6;
const actual = score("street");
try testing.expectEqual(expected, actual);
}
test "medium, valuable word" {
const expected: u32 = 22;
const actual = score("quirky");
try testing.expectEqual(expected, actual);
}
test "long, mixed-case word" {
const expected: u32 = 41;
const actual = score("OxyphenButazone");
try testing.expectEqual(expected, actual);
}
test "english-like word" {
const expected: u32 = 8;
const actual = score("pinata");
try testing.expectEqual(expected, actual);
}
test "empty input" {
const expected: u32 = 0;
const actual = score("");
try testing.expectEqual(expected, actual);
}
test "entire alphabet available" {
const expected: u32 = 87;
const actual = score("abcdefghijklmnopqrstuvwxyz");
try testing.expectEqual(expected, actual);
}