-
-
Notifications
You must be signed in to change notification settings - Fork 145
Add Eliuds Eggs exercise #639
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
homersimpsons
merged 1 commit into
exercism:main
from
homersimpsons:exercise/eliuds-eggs
Feb 25, 2024
Merged
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions
13
exercises/practice/eliuds-eggs/.docs/instructions.append.md
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,13 @@ | ||
| ## Bitwise operators | ||
|
|
||
| In PHP there are [bitwise operators](https://www.php.net/manual/en/language.operators.bitwise.php). | ||
|
|
||
| For example, the "bitwise and" operator (`&`) can be used to check that a bit of a number is defined: | ||
| ```php | ||
| $number = 89; // 0b01011001 | ||
| $mask16 = 16; // 0b00010000 | ||
| $mask32 = 32; // 0b00100000 | ||
|
|
||
| $isMask16 = ($number & $mask16) > 0; // 0b00010000 > 0 => TRUE | ||
| $isMask32 = ($number & $mask32) > 0; // 0b00000000 > 0 => FALSE | ||
| ``` |
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,8 @@ | ||
| # Instructions | ||
|
|
||
| Your task is to count the number of 1 bits in the binary representation of a number. | ||
|
|
||
| ## Restrictions | ||
|
|
||
| Keep your hands off that bit-count functionality provided by your standard library! | ||
| Solve this one yourself using other basic tools instead. |
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,47 @@ | ||
| # Introduction | ||
|
|
||
| Your friend Eliud inherited a farm from her grandma Tigist. | ||
| Her granny was an inventor and had a tendency to build things in an overly complicated manner. | ||
| The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up. | ||
|
|
||
| Eliud is asking you to write a program that shows the actual number of eggs in the coop. | ||
|
|
||
| The position information encoding is calculated as follows: | ||
|
|
||
| 1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot. | ||
| 2. Convert the number from binary to decimal. | ||
| 3. Show the result on the display. | ||
|
|
||
| Example 1: | ||
|
|
||
| ```text | ||
| Chicken Coop: | ||
| _ _ _ _ _ _ _ | ||
| |E| |E|E| | |E| | ||
|
|
||
| Resulting Binary: | ||
| 1 0 1 1 0 0 1 | ||
|
|
||
| Decimal number on the display: | ||
| 89 | ||
|
|
||
| Actual eggs in the coop: | ||
| 4 | ||
| ``` | ||
|
|
||
| Example 2: | ||
|
|
||
| ```text | ||
| Chicken Coop: | ||
| _ _ _ _ _ _ _ _ | ||
| | | | |E| | | | | | ||
|
|
||
| Resulting Binary: | ||
| 0 0 0 1 0 0 0 0 | ||
|
|
||
| Decimal number on the display: | ||
| 16 | ||
|
|
||
| Actual eggs in the coop: | ||
| 1 | ||
| ``` |
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,19 @@ | ||
| { | ||
| "authors": [ | ||
| "homersimpsons" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "EliudsEggs.php" | ||
| ], | ||
| "test": [ | ||
| "EliudsEggsTest.php" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.php" | ||
| ] | ||
| }, | ||
| "blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.", | ||
| "source": "Christian Willner, Eric Willigers", | ||
| "source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5" | ||
| } |
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,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| class EliudsEggs | ||
| { | ||
| public function eggCount(int $displayValue): int | ||
| { | ||
| if ($displayValue === 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return ($displayValue & 1) + $this->eggCount($displayValue >> 1); | ||
| } | ||
| } |
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,22 @@ | ||
| # This is an auto-generated file. | ||
| # | ||
| # Regenerating this file via `configlet sync` will: | ||
| # - Recreate every `description` key/value pair | ||
| # - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
| # - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
| # - Preserve any other key/value pair | ||
| # | ||
| # As user-added comments (using the # character) will be removed when this file | ||
| # is regenerated, comments can be added via a `comment` key. | ||
|
|
||
| [559e789d-07d1-4422-9004-3b699f83bca3] | ||
| description = "0 eggs" | ||
|
|
||
| [97223282-f71e-490c-92f0-b3ec9e275aba] | ||
| description = "1 egg" | ||
|
|
||
| [1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5] | ||
| description = "4 eggs" | ||
|
|
||
| [0c18be92-a498-4ef2-bcbb-28ac4b06cb81] | ||
| description = "13 eggs" |
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,33 @@ | ||
| <?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 EliudsEggs | ||
| { | ||
| public function eggCount(int $displayValue): int | ||
| { | ||
| throw new \BadMethodCallException("Implement the eggCount 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,73 @@ | ||
| <?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 EliudsEggsTest extends PHPUnit\Framework\TestCase | ||
| { | ||
| public static function setUpBeforeClass(): void | ||
| { | ||
| require_once 'EliudsEggs.php'; | ||
| } | ||
|
|
||
| /** | ||
| * uuid: 559e789d-07d1-4422-9004-3b699f83bca3 | ||
| * @testdox 0 eggs | ||
| */ | ||
| public function test0Eggs() | ||
| { | ||
| $eliudsEggs = new EliudsEggs(); | ||
| $this->assertEquals(0, $eliudsEggs->eggCount(0)); | ||
| } | ||
|
|
||
| /** | ||
| * uuid: 97223282-f71e-490c-92f0-b3ec9e275aba | ||
| * @testdox 1 eggs | ||
| */ | ||
| public function test1Eggs() | ||
| { | ||
| $eliudsEggs = new EliudsEggs(); | ||
| $this->assertEquals(1, $eliudsEggs->eggCount(16)); | ||
| } | ||
|
|
||
| /** | ||
| * uuid: 1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5 | ||
| * @testdox 4 eggs | ||
| */ | ||
| public function test4Eggs() | ||
| { | ||
| $eliudsEggs = new EliudsEggs(); | ||
| $this->assertEquals(4, $eliudsEggs->eggCount(89)); | ||
| } | ||
|
|
||
| /** | ||
| * uuid: 0c18be92-a498-4ef2-bcbb-28ac4b06cb81 | ||
| * @testdox 13 eggs | ||
| */ | ||
| public function test13Eggs() | ||
| { | ||
| $eliudsEggs = new EliudsEggs(); | ||
| $this->assertEquals(13, $eliudsEggs->eggCount(2000000000)); | ||
| } | ||
| } | ||
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.