mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-22 06:43:09 -05:00
Zig: completed Reverse String
This commit is contained in:
parent
1e8c06ae40
commit
fe6da9630c
6 changed files with 145 additions and 0 deletions
19
zig/reverse-string/.exercism/config.json
Normal file
19
zig/reverse-string/.exercism/config.json
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"ee7"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"reverse_string.zig"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test_reverse_string.zig"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.zig"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Reverse a given string.",
|
||||||
|
"source": "Introductory challenge to reverse an input string",
|
||||||
|
"source_url": "https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb"
|
||||||
|
}
|
1
zig/reverse-string/.exercism/metadata.json
Normal file
1
zig/reverse-string/.exercism/metadata.json
Normal file
|
@ -0,0 +1 @@
|
||||||
|
{"track":"zig","exercise":"reverse-string","id":"506c2a4923c9458e9a24cda8f948de0b","url":"https://exercism.org/tracks/zig/exercises/reverse-string","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
53
zig/reverse-string/HELP.md
Normal file
53
zig/reverse-string/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 reverse_string.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
|
30
zig/reverse-string/README.md
Normal file
30
zig/reverse-string/README.md
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# Reverse String
|
||||||
|
|
||||||
|
Welcome to Reverse String on Exercism's Zig Track.
|
||||||
|
If you need help running the tests or submitting your code, check out `HELP.md`.
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Reversing strings (reading them from right to left, rather than from left to right) is a surprisingly common task in programming.
|
||||||
|
|
||||||
|
For example, in bioinformatics, reversing the sequence of DNA or RNA strings is often important for various analyses, such as finding complementary strands or identifying palindromic sequences that have biological significance.
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Your task is to reverse a given string.
|
||||||
|
|
||||||
|
Some examples:
|
||||||
|
|
||||||
|
- Turn `"stressed"` into `"desserts"`.
|
||||||
|
- Turn `"strops"` into `"sports"`.
|
||||||
|
- Turn `"racecar"` into `"racecar"`.
|
||||||
|
|
||||||
|
## Source
|
||||||
|
|
||||||
|
### Created by
|
||||||
|
|
||||||
|
- @ee7
|
||||||
|
|
||||||
|
### Based on
|
||||||
|
|
||||||
|
Introductory challenge to reverse an input string - https://medium.freecodecamp.org/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb
|
8
zig/reverse-string/reverse_string.zig
Normal file
8
zig/reverse-string/reverse_string.zig
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
/// Writes a reversed copy of `s` to `buffer`.
|
||||||
|
pub fn reverse(buffer: []u8, s: []const u8) []u8 {
|
||||||
|
@memcpy(buffer, s);
|
||||||
|
std.mem.reverse(u8, buffer);
|
||||||
|
return buffer;
|
||||||
|
}
|
34
zig/reverse-string/test_reverse_string.zig
Normal file
34
zig/reverse-string/test_reverse_string.zig
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
const std = @import("std");
|
||||||
|
const testing = std.testing;
|
||||||
|
|
||||||
|
const reverse_string = @import("reverse_string.zig");
|
||||||
|
|
||||||
|
fn testReverse(comptime s: []const u8, expected: []const u8) !void {
|
||||||
|
var buffer: [s.len]u8 = undefined;
|
||||||
|
const actual = reverse_string.reverse(&buffer, s);
|
||||||
|
try testing.expectEqualStrings(expected, actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
test "an empty string" {
|
||||||
|
try testReverse("", "");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a word" {
|
||||||
|
try testReverse("robot", "tobor");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a capitalized word" {
|
||||||
|
try testReverse("Ramen", "nemaR");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a sentence with punctuation" {
|
||||||
|
try testReverse("I'm hungry!", "!yrgnuh m'I");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "a palindrome" {
|
||||||
|
try testReverse("racecar", "racecar");
|
||||||
|
}
|
||||||
|
|
||||||
|
test "an even-sized word" {
|
||||||
|
try testReverse("drawer", "reward");
|
||||||
|
}
|
Loading…
Reference in a new issue