From fe6da9630c6dc101a91a844b4d052b4eb7ce5471 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 20 Sep 2024 17:07:22 -0400 Subject: [PATCH] Zig: completed Reverse String --- zig/reverse-string/.exercism/config.json | 19 ++++++++ zig/reverse-string/.exercism/metadata.json | 1 + zig/reverse-string/HELP.md | 53 ++++++++++++++++++++++ zig/reverse-string/README.md | 30 ++++++++++++ zig/reverse-string/reverse_string.zig | 8 ++++ zig/reverse-string/test_reverse_string.zig | 34 ++++++++++++++ 6 files changed, 145 insertions(+) create mode 100644 zig/reverse-string/.exercism/config.json create mode 100644 zig/reverse-string/.exercism/metadata.json create mode 100644 zig/reverse-string/HELP.md create mode 100644 zig/reverse-string/README.md create mode 100644 zig/reverse-string/reverse_string.zig create mode 100644 zig/reverse-string/test_reverse_string.zig diff --git a/zig/reverse-string/.exercism/config.json b/zig/reverse-string/.exercism/config.json new file mode 100644 index 0000000..f385d00 --- /dev/null +++ b/zig/reverse-string/.exercism/config.json @@ -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" +} diff --git a/zig/reverse-string/.exercism/metadata.json b/zig/reverse-string/.exercism/metadata.json new file mode 100644 index 0000000..8a1c89c --- /dev/null +++ b/zig/reverse-string/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/zig/reverse-string/HELP.md b/zig/reverse-string/HELP.md new file mode 100644 index 0000000..2eabe3e --- /dev/null +++ b/zig/reverse-string/HELP.md @@ -0,0 +1,53 @@ +# Help + +## Running the tests + +Write your code in `.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 \ No newline at end of file diff --git a/zig/reverse-string/README.md b/zig/reverse-string/README.md new file mode 100644 index 0000000..c04f29c --- /dev/null +++ b/zig/reverse-string/README.md @@ -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 \ No newline at end of file diff --git a/zig/reverse-string/reverse_string.zig b/zig/reverse-string/reverse_string.zig new file mode 100644 index 0000000..1b983e4 --- /dev/null +++ b/zig/reverse-string/reverse_string.zig @@ -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; +} diff --git a/zig/reverse-string/test_reverse_string.zig b/zig/reverse-string/test_reverse_string.zig new file mode 100644 index 0000000..cf58d6d --- /dev/null +++ b/zig/reverse-string/test_reverse_string.zig @@ -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"); +}