From d43cb9726c86b6e0015d2695bd5fdb59d3282d1e Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Thu, 7 Oct 2021 12:49:29 -0300 Subject: [PATCH] Resistor color --- php/resistor-color/.exercism/config.json | 10 ++++ php/resistor-color/.exercism/metadata.json | 1 + php/resistor-color/HELP.md | 51 +++++++++++++++++ php/resistor-color/README.md | 35 ++++++++++++ php/resistor-color/ResistorColor.php | 35 ++++++++++++ php/resistor-color/ResistorColorTest.php | 64 ++++++++++++++++++++++ 6 files changed, 196 insertions(+) create mode 100644 php/resistor-color/.exercism/config.json create mode 100644 php/resistor-color/.exercism/metadata.json create mode 100644 php/resistor-color/HELP.md create mode 100644 php/resistor-color/README.md create mode 100644 php/resistor-color/ResistorColor.php create mode 100644 php/resistor-color/ResistorColorTest.php diff --git a/php/resistor-color/.exercism/config.json b/php/resistor-color/.exercism/config.json new file mode 100644 index 0000000..8d2c033 --- /dev/null +++ b/php/resistor-color/.exercism/config.json @@ -0,0 +1,10 @@ +{ + "blurb": "Convert a resistor band's color to its numeric representation", + "authors": ["camilopayan"], + "contributors": ["MichaelBunker"], + "files": { + "solution": ["ResistorColor.php"], + "test": ["ResistorColorTest.php"], + "example": [".meta/example.php"] + } +} diff --git a/php/resistor-color/.exercism/metadata.json b/php/resistor-color/.exercism/metadata.json new file mode 100644 index 0000000..11bf772 --- /dev/null +++ b/php/resistor-color/.exercism/metadata.json @@ -0,0 +1 @@ +{"track":"php","exercise":"resistor-color","id":"bc671895e6b54bc297b7e785fa301020","url":"https://exercism.org/tracks/php/exercises/resistor-color","handle":"JBiason","is_requester":true,"auto_approve":false} \ No newline at end of file diff --git a/php/resistor-color/HELP.md b/php/resistor-color/HELP.md new file mode 100644 index 0000000..a726606 --- /dev/null +++ b/php/resistor-color/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 ResistorColor.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/resistor-color/README.md b/php/resistor-color/README.md new file mode 100644 index 0000000..0529d6b --- /dev/null +++ b/php/resistor-color/README.md @@ -0,0 +1,35 @@ +# Resistor Color + +Welcome to Resistor Color on Exercism's PHP Track. +If you need help running the tests or submitting your code, check out `HELP.md`. + +## Instructions + +Resistors have color coded bands, where each color maps to a number. The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number. + +These colors are encoded as follows: + +- Black: 0 +- Brown: 1 +- Red: 2 +- Orange: 3 +- Yellow: 4 +- Green: 5 +- Blue: 6 +- Violet: 7 +- Grey: 8 +- White: 9 + +Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong. + +More information on the color encoding of resistors can be found in the [Electronic color code Wikipedia article](https://en.wikipedia.org/wiki/Electronic_color_code) + +## Source + +### Created by + +- @camilopayan + +### Contributed to by + +- @MichaelBunker \ No newline at end of file diff --git a/php/resistor-color/ResistorColor.php b/php/resistor-color/ResistorColor.php new file mode 100644 index 0000000..844839e --- /dev/null +++ b/php/resistor-color/ResistorColor.php @@ -0,0 +1,35 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +define("COLORS", + array("black", "brown", "red", "orange", "yellow", "green", "blue", + "violet", "grey", "white") +); + +function colorCode(string $color): int +{ + return array_search($color, COLORS); +} diff --git a/php/resistor-color/ResistorColorTest.php b/php/resistor-color/ResistorColorTest.php new file mode 100644 index 0000000..725d9d6 --- /dev/null +++ b/php/resistor-color/ResistorColorTest.php @@ -0,0 +1,64 @@ +. + * + * To disable strict typing, comment out the directive below. + */ + +declare(strict_types=1); + +class ResistorColorTest extends PHPUnit\Framework\TestCase +{ + public static function setUpBeforeClass(): void + { + require_once 'ResistorColor.php'; + } + + public function testBlackColorCode(): void + { + $this->assertEquals(colorCode("black"), 0); + } + + public function testOrangeColorCode(): void + { + $this->assertEquals(colorCode("orange"), 3); + } + + public function testWhiteColorCode(): void + { + $this->assertEquals(colorCode("white"), 9); + } + + public function testColors(): void + { + $this->assertEquals(COLORS, [ + "black", + "brown", + "red", + "orange", + "yellow", + "green", + "blue", + "violet", + "grey", + "white" + ]); + } +}