Browse Source

02 Reverse string with multibyte support added.

Frédéric G. MARAND 3 months ago
parent
commit
4e4cceda39

+ 19 - 0
reverse-string/.exercism/config.json

@@ -0,0 +1,19 @@
+{
+  "authors": [
+    "MichaelBunker"
+  ],
+  "files": {
+    "solution": [
+      "ReverseString.php"
+    ],
+    "test": [
+      "ReverseStringTest.php"
+    ],
+    "example": [
+      ".meta/example.php"
+    ]
+  },
+  "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 - 0
reverse-string/.exercism/metadata.json

@@ -0,0 +1 @@
+{"track":"php","exercise":"reverse-string","id":"bc57101cde7046519b745297a69fe96b","url":"https://exercism.org/tracks/php/exercises/reverse-string","handle":"Fairgame","is_requester":true,"auto_approve":false}

+ 52 - 0
reverse-string/HELP.md

@@ -0,0 +1,52 @@
+# Help
+
+## Running the tests
+
+## Running the tests
+
+1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`.
+   To find the Exercism workspace run
+
+       ➜ exercism debug | grep Workspace
+
+1. Get [PHPUnit] if you don't have it already.
+
+       ➜ wget -O phpunit https://phar.phpunit.de/phpunit-9.phar
+       ➜ chmod +x phpunit
+       ➜ ./phpunit --version
+
+2. Execute the tests:
+
+       ➜ ./phpunit file_to_test.php
+
+   For example, to run the tests for the Hello World exercise, you would run:
+
+       ➜ ./phpunit HelloWorldTest.php
+
+[PHPUnit]: https://phpunit.de
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit ReverseString.php` 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 [PHP track's documentation](https://exercism.org/docs/tracks/php)
+- The [PHP track's programming category on the forum](https://forum.exercism.org/c/programming/php)
+- [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.
+
+To get help if you're having trouble, you can use one of the following resources:
+
+ - [/r/php](https://www.reddit.com/r/php) is the PHP subreddit.
+ - [StackOverflow](https://stackoverflow.com/questions/tagged/php) can be used to search for your problem and see if it has been answered already. You can also ask and answer questions.

+ 30 - 0
reverse-string/README.md

@@ -0,0 +1,30 @@
+# Reverse String
+
+Welcome to Reverse String on Exercism's PHP 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
+
+- @MichaelBunker
+
+### 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

+ 38 - 0
reverse-string/ReverseString.php

@@ -0,0 +1,38 @@
+<?php
+
+/*
+ * By adding type hints and enabling strict type checking, code can become
+ * easier to read, self-documenting and reduce the number of potential bugs.
+ * By default, type declarations are non-strict, which means they will attempt
+ * to change the original type to match the type specified by the
+ * type-declaration.
+ *
+ * In other words, if you pass a string to a function requiring a float,
+ * it will attempt to convert the string value to a float.
+ *
+ * To enable strict mode, a single declare directive must be placed at the top
+ * of the file.
+ * This means that the strictness of typing is configured on a per-file basis.
+ * This directive not only affects the type declarations of parameters, but also
+ * a function's return type.
+ *
+ * For more info review the Concept on strict type checking in the PHP track
+ * <link>.
+ *
+ * To disable strict typing, comment out the directive below.
+ */
+
+declare(strict_types=1);
+
+/**
+ * strrev() does not support multibyte inputs, so we need to use multibyte-aware functions.
+ */
+function reverseString(string $text): string
+{
+  $max = mb_strlen($text);
+  $res = "";
+  for ($i = 0; $i < $max; $i++) {
+    $res .= mb_substr($text, $max-$i-1, 1);
+  }
+  return $res;
+}

+ 68 - 0
reverse-string/ReverseStringTest.php

@@ -0,0 +1,68 @@
+<?php
+
+/*
+ * By adding type hints and enabling strict type checking, code can become
+ * easier to read, self-documenting and reduce the number of potential bugs.
+ * By default, type declarations are non-strict, which means they will attempt
+ * to change the original type to match the type specified by the
+ * type-declaration.
+ *
+ * In other words, if you pass a string to a function requiring a float,
+ * it will attempt to convert the string value to a float.
+ *
+ * To enable strict mode, a single declare directive must be placed at the top
+ * of the file.
+ * This means that the strictness of typing is configured on a per-file basis.
+ * This directive not only affects the type declarations of parameters, but also
+ * a function's return type.
+ *
+ * For more info review the Concept on strict type checking in the PHP track
+ * <link>.
+ *
+ * To disable strict typing, comment out the directive below.
+ */
+
+declare(strict_types=1);
+
+class ReverseStringTest extends PHPUnit\Framework\TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        require_once 'ReverseString.php';
+    }
+
+    public function testEmptyString(): void
+    {
+        $this->assertEquals("", reverseString(""));
+    }
+
+    public function testWord(): void
+    {
+        $this->assertEquals("tobor", reverseString("robot"));
+    }
+
+    public function testCapitalizedWord(): void
+    {
+        $this->assertEquals("nemaR", reverseString("Ramen"));
+    }
+
+    public function testMultibyte(): void
+    {
+        $this->assertEquals("hâché", reverseString("éhcâh"));
+    }
+
+    public function testSentenceWithPunctuation(): void
+    {
+        $this->assertEquals("!yrgnuh m'I", reverseString("I'm hungry!"));
+    }
+
+    public function testPalindrome(): void
+    {
+        $this->assertEquals("racecar", reverseString("racecar"));
+    }
+
+    public function testEvenSizedWord(): void
+    {
+        $this->assertEquals("reward", reverseString("drawer"));
+    }
+}