From 48eaec8d112c219563e66ce3656e091416954856 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 7 Oct 2021 12:38:08 -0300 Subject: [PATCH] Reverse String --- php/reverse-string/.exercism/config.json | 10 ++++ php/reverse-string/.exercism/metadata.json | 1 + php/reverse-string/HELP.md | 51 ++++++++++++++++++ php/reverse-string/README.md | 18 +++++++ php/reverse-string/ReverseString.php | 34 ++++++++++++ php/reverse-string/ReverseStringTest.php | 63 ++++++++++++++++++++++ 6 files changed, 177 insertions(+) create mode 100644 php/reverse-string/.exercism/config.json create mode 100644 php/reverse-string/.exercism/metadata.json create mode 100644 php/reverse-string/HELP.md create mode 100644 php/reverse-string/README.md create mode 100644 php/reverse-string/ReverseString.php create mode 100644 php/reverse-string/ReverseStringTest.php diff --git a/php/reverse-string/.exercism/config.json b/php/reverse-string/.exercism/config.json new file mode 100644 index 0000000..c88f1b2 --- /dev/null +++ b/php/reverse-string/.exercism/config.json @@ -0,0 +1,10 @@ +{ + "blurb": "Reverse a string", + "authors": ["MichaelBunker"], + "contributors": [], + "files": { + "solution": ["ReverseString.php"], + "test": ["ReverseStringTest.php"], + "example": [".meta/example.php"] + } +} diff --git a/php/reverse-string/.exercism/metadata.json b/php/reverse-string/.exercism/metadata.json new file mode 100644 index 0000000..4301ea2 --- /dev/null +++ b/php/reverse-string/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"php","exercise":"reverse-string","id":"af5aa937e83b4c10940c1025f6c9c4b7","url":"https://exercism.org/tracks/php/exercises/reverse-string","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/php/reverse-string/HELP.md b/php/reverse-string/HELP.md new file mode 100644 index 0000000..1e07c99 --- /dev/null +++ b/php/reverse-string/HELP.md @@ -0,0 +1,51 @@ +# Help + +## Running the tests + +## Running the tests + +1. Go to the root of your PHP exercise directory, which is `/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]: http://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) +- [Exercism's support channel on gitter](https://gitter.im/exercism/support) +- 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. \ No newline at end of file diff --git a/php/reverse-string/README.md b/php/reverse-string/README.md new file mode 100644 index 0000000..8ab68be --- /dev/null +++ b/php/reverse-string/README.md @@ -0,0 +1,18 @@ +# 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`. + +## Instructions + +Reverse a string + +For example: +input: "cool" +output: "looc" + +## Source + +### Created by + +- @MichaelBunker \ No newline at end of file diff --git a/php/reverse-string/ReverseString.php b/php/reverse-string/ReverseString.php new file mode 100644 index 0000000..2b8e5ce --- /dev/null +++ b/php/reverse-string/ReverseString.php @@ -0,0 +1,34 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +function reverseString(string $text): string +{ + $result = ''; + for ($i = strlen($text) - 1; $i >= 0; --$i) { + $result .= $text[$i]; + } + return $result; +} diff --git a/php/reverse-string/ReverseStringTest.php b/php/reverse-string/ReverseStringTest.php new file mode 100644 index 0000000..4d23e8d --- /dev/null +++ b/php/reverse-string/ReverseStringTest.php @@ -0,0 +1,63 @@ +. + * + * 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 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")); + } +}