Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions exercises/practice/ordinal-number/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Instructions

Write a function to convert normal numbers to [ordinal numbers](https://en.wikipedia.org/wiki/Ordinal_numeral). For zero (0) just return "0" as string.
Comment thread
codedge marked this conversation as resolved.

Ordinal number are numbers that describe the position of something in a list.
It is this context that ordinal numbers will be used, using an English-spelled name of an ordinal number.

The ordinal numbers are (at least, one form of them):

```
1st 2nd 3rd 4th 5th... 9th 10th 11th 12th... 20th 21st 22nd 23rd 24th... etc.
```

So entering a normal number results in its ordinal equivalent:

```
1 -> 1st
2 -> 2nd
3 -> 3rd
(and so on...)
```
27 changes: 27 additions & 0 deletions exercises/practice/ordinal-number/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"authors": [
{
"github_username": "codedge",
"exercism_username": "codedge"
}
],
"contributors": [
{
"github_username": "neenjaw",
"exercism_username": "neenjaw"
}
],
"files": {
"solution": [
"OrdinalNumber.php"
],
"test": [
"OrdinalNumberTest.php"
],
"example": [
".meta/example.php"
]
},
"source": "",
"source_url": "https://en.wikipedia.org/wiki/Ordinal_numeral"
}
26 changes: 26 additions & 0 deletions exercises/practice/ordinal-number/.meta/example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

function toOrdinal(int $number): string
{
if (0 === $number) {
return '0';
}

$ending = 'th';

if (!in_array(($number % 100), [11, 12, 13])) {
switch ($number % 10) {
case 1:
$ending = 'st';
break;
case 2:
$ending = 'nd';
break;
case 3:
$ending = 'rd';
break;
}
}

return $number . $ending;
}
6 changes: 6 additions & 0 deletions exercises/practice/ordinal-number/OrdinalNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function toOrdinal(int $number): string
{
throw new \BadFunctionCallException("Implement the toOrdinal function");
}
59 changes: 59 additions & 0 deletions exercises/practice/ordinal-number/OrdinalNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

class OrdinalNumberTest extends PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass(): void
{
require_once 'OrdinalNumber.php';
}

public function testZero(): void
{
$this->assertEquals('0', toOrdinal(0));
}

public function testFirst(): void
{
$this->assertEquals('1st', toOrdinal(1));
}

public function testSecond(): void
{
$this->assertEquals('2nd', toOrdinal(2));
}

public function testThird(): void
{
$this->assertEquals('3rd', toOrdinal(3));
}

public function testFourth(): void
{
$this->assertEquals('4th', toOrdinal(4));
}

public function testTenth(): void
{
$this->assertEquals('10th', toOrdinal(10));
}

public function testEleventh(): void
{
$this->assertEquals('11th', toOrdinal(11));
}

public function testTwelfth(): void
{
$this->assertEquals('12th', toOrdinal(12));
}

public function testThirteenth(): void
{
$this->assertEquals('13th', toOrdinal(13));
}

public function testRandomNumber(): void
{
$this->assertEquals('62nd', toOrdinal(62));
}
}