Browse Source

03 Raindrops.

Frédéric G. MARAND 2 months ago
parent
commit
8ac99831ee

+ 26 - 0
raindrops/.exercism/config.json

@@ -0,0 +1,26 @@
+{
+  "authors": [],
+  "contributors": [
+    "arueckauer",
+    "dkinzer",
+    "kenden",
+    "kunicmarko20",
+    "kytrinyx",
+    "lafent",
+    "petemcfarlane"
+  ],
+  "files": {
+    "solution": [
+      "Raindrops.php"
+    ],
+    "test": [
+      "RaindropsTest.php"
+    ],
+    "example": [
+      ".meta/example.php"
+    ]
+  },
+  "blurb": "Convert a number to a string, the content of which depends on the number's factors.",
+  "source": "A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division.",
+  "source_url": "https://en.wikipedia.org/wiki/Fizz_buzz"
+}

+ 1 - 0
raindrops/.exercism/metadata.json

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

+ 52 - 0
raindrops/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 Raindrops.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.

+ 49 - 0
raindrops/README.md

@@ -0,0 +1,49 @@
+# Raindrops
+
+Welcome to Raindrops on Exercism's PHP Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Introduction
+
+Raindrops is a slightly more complex version of the FizzBuzz challenge, a classic interview question.
+
+## Instructions
+
+Your task is to convert a number into its corresponding raindrop sounds.
+
+If a given number:
+
+- is divisible by 3, add "Pling" to the result.
+- is divisible by 5, add "Plang" to the result.
+- is divisible by 7, add "Plong" to the result.
+- **is not** divisible by 3, 5, or 7, the result should be the number as a string.
+
+## Examples
+
+- 28 is divisible by 7, but not 3 or 5, so the result would be `"Plong"`.
+- 30 is divisible by 3 and 5, but not 7, so the result would be `"PlingPlang"`.
+- 34 is not divisible by 3, 5, or 7, so the result would be `"34"`.
+
+~~~~exercism/note
+A common way to test if one number is evenly divisible by another is to compare the [remainder][remainder] or [modulus][modulo] to zero.
+Most languages provide operators or functions for one (or both) of these.
+
+[remainder]: https://exercism.org/docs/programming/operators/remainder
+[modulo]: https://en.wikipedia.org/wiki/Modulo_operation
+~~~~
+
+## Source
+
+### Contributed to by
+
+- @arueckauer
+- @dkinzer
+- @kenden
+- @kunicmarko20
+- @kytrinyx
+- @lafent
+- @petemcfarlane
+
+### Based on
+
+A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz

+ 42 - 0
raindrops/Raindrops.php

@@ -0,0 +1,42 @@
+<?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);
+
+function raindrops(int $number): string {
+  $res = "";
+  if (!($number % 3)) {
+    $res .= "Pling";
+  }
+  if (!($number % 5)) {
+    $res .= "Plang";
+  }
+  if (!($number % 7)) {
+    $res .= "Plong";
+  }
+  if (!$res) {
+    $res = "$number";
+  }
+  return $res;
+}

+ 113 - 0
raindrops/RaindropsTest.php

@@ -0,0 +1,113 @@
+<?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 RaindropsTest extends PHPUnit\Framework\TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        require_once 'Raindrops.php';
+    }
+
+    public function test1(): void
+    {
+        $this->assertSame("1", raindrops(1));
+    }
+
+    public function test3(): void
+    {
+        $this->assertSame("Pling", raindrops(3));
+    }
+
+    public function test5(): void
+    {
+        $this->assertSame("Plang", raindrops(5));
+    }
+
+    public function test7(): void
+    {
+        $this->assertSame("Plong", raindrops(7));
+    }
+
+    public function test6(): void
+    {
+        $this->assertSame("Pling", raindrops(6));
+    }
+
+    public function test9(): void
+    {
+        $this->assertSame("Pling", raindrops(9));
+    }
+
+    public function test10(): void
+    {
+        $this->assertSame("Plang", raindrops(10));
+    }
+
+    public function test14(): void
+    {
+        $this->assertSame("Plong", raindrops(14));
+    }
+
+    public function test15(): void
+    {
+        $this->assertSame("PlingPlang", raindrops(15));
+    }
+
+    public function test21(): void
+    {
+        $this->assertSame("PlingPlong", raindrops(21));
+    }
+
+    public function test25(): void
+    {
+        $this->assertSame("Plang", raindrops(25));
+    }
+
+    public function test35(): void
+    {
+        $this->assertSame("PlangPlong", raindrops(35));
+    }
+
+    public function test49(): void
+    {
+        $this->assertSame("Plong", raindrops(49));
+    }
+
+    public function test52(): void
+    {
+        $this->assertSame("52", raindrops(52));
+    }
+
+    public function test105(): void
+    {
+        $this->assertSame("PlingPlangPlong", raindrops(105));
+    }
+
+    public function test12121(): void
+    {
+        $this->assertSame("12121", raindrops(12121));
+    }
+}