-
-
Notifications
You must be signed in to change notification settings - Fork 148
Ordinal numbers exercise #351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9294271
Provide instruction, code and tests
aa93d6c
Rename test function
278d52b
Improve text, add wikipedia link, fix styling for ci
c334070
Improve tests, remove skeleton text
62c44c5
Update author, contributor and solution
97aa9c6
Add exception for empty function
ae24e4e
Updated correct method name, removed strict types
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
|
|
||
| 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...) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.