Frédéric G. MARAND 3 months ago
parent
commit
71b3a55948
6 changed files with 215 additions and 0 deletions
  1. 25 0
      leap/.exercism/config.json
  2. 1 0
      leap/.exercism/metadata.json
  3. 52 0
      leap/HELP.md
  4. 39 0
      leap/Leap.php
  5. 58 0
      leap/LeapTest.php
  6. 40 0
      leap/README.md

+ 25 - 0
leap/.exercism/config.json

@@ -0,0 +1,25 @@
+{
+  "authors": [],
+  "contributors": [
+    "arueckauer",
+    "dkinzer",
+    "kunicmarko20",
+    "kytrinyx",
+    "lafent",
+    "petemcfarlane"
+  ],
+  "files": {
+    "solution": [
+      "Leap.php"
+    ],
+    "test": [
+      "LeapTest.php"
+    ],
+    "example": [
+      ".meta/example.php"
+    ]
+  },
+  "blurb": "Determine whether a given year is a leap year.",
+  "source": "CodeRanch Cattle Drive, Assignment 3",
+  "source_url": "https://coderanch.com/t/718816/Leap"
+}

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

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

+ 52 - 0
leap/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 Leap.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.

+ 39 - 0
leap/Leap.php

@@ -0,0 +1,39 @@
+<?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 isLeap(int $year): bool
+{
+    if ($year % 400 == 0) {
+      return true;
+    }
+    if ($year % 100 == 0) {
+      return false;
+    }
+    if ($year % 4 != 0) {
+      return false;
+    }
+    return true;
+}

+ 58 - 0
leap/LeapTest.php

@@ -0,0 +1,58 @@
+<?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 LeapTest extends PHPUnit\Framework\TestCase
+{
+    public static function setUpBeforeClass(): void
+    {
+        require_once 'Leap.php';
+    }
+
+    public function testLeapYear(): void
+    {
+        $this->assertTrue(isLeap(1996));
+    }
+
+    public function testNonLeapYear(): void
+    {
+        $this->assertFalse(isLeap(1997));
+    }
+
+    public function testNonLeapEvenYear(): void
+    {
+        $this->assertFalse(isLeap(1998));
+    }
+
+    public function testCentury(): void
+    {
+        $this->assertFalse(isLeap(1900));
+    }
+
+    public function testFourthCentury(): void
+    {
+        $this->assertTrue(isLeap(2400));
+    }
+}

+ 40 - 0
leap/README.md

@@ -0,0 +1,40 @@
+# Leap
+
+Welcome to Leap on Exercism's PHP Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Introduction
+
+A leap year (in the Gregorian calendar) occurs:
+
+- In every year that is evenly divisible by 4.
+- Unless the year is evenly divisible by 100, in which case it's only a leap year if the year is also evenly divisible by 400.
+
+Some examples:
+
+- 1997 was not a leap year as it's not divisible by 4.
+- 1900 was not a leap year as it's not divisible by 400.
+- 2000 was a leap year!
+
+~~~~exercism/note
+For a delightful, four-minute explanation of the whole phenomenon of leap years, check out [this YouTube video](https://www.youtube.com/watch?v=xX96xng7sAE).
+~~~~
+
+## Instructions
+
+Your task is to determine whether a given year is a leap year.
+
+## Source
+
+### Contributed to by
+
+- @arueckauer
+- @dkinzer
+- @kunicmarko20
+- @kytrinyx
+- @lafent
+- @petemcfarlane
+
+### Based on
+
+CodeRanch Cattle Drive, Assignment 3 - https://coderanch.com/t/718816/Leap