Skip to content

Commit 1ed86e7

Browse files
Add Eliuds Eggs exercise (#639)
1 parent 2efa3a3 commit 1ed86e7

File tree

9 files changed

+238
-0
lines changed

9 files changed

+238
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,14 @@
12271227
"practices": [],
12281228
"prerequisites": [],
12291229
"difficulty": 5
1230+
},
1231+
{
1232+
"slug": "eliuds-eggs",
1233+
"name": "Eliud's Eggs",
1234+
"uuid": "9a8df843-8805-4287-b863-b8123defc266",
1235+
"practices": [],
1236+
"prerequisites": [],
1237+
"difficulty": 5
12301238
}
12311239
]
12321240
},
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Bitwise operators
2+
3+
In PHP there are [bitwise operators](https://www.php.net/manual/en/language.operators.bitwise.php).
4+
5+
For example, the "bitwise and" operator (`&`) can be used to check that a bit of a number is defined:
6+
```php
7+
$number = 89; // 0b01011001
8+
$mask16 = 16; // 0b00010000
9+
$mask32 = 32; // 0b00100000
10+
11+
$isMask16 = ($number & $mask16) > 0; // 0b00010000 > 0 => TRUE
12+
$isMask32 = ($number & $mask32) > 0; // 0b00000000 > 0 => FALSE
13+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Instructions
2+
3+
Your task is to count the number of 1 bits in the binary representation of a number.
4+
5+
## Restrictions
6+
7+
Keep your hands off that bit-count functionality provided by your standard library!
8+
Solve this one yourself using other basic tools instead.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Introduction
2+
3+
Your friend Eliud inherited a farm from her grandma Tigist.
4+
Her granny was an inventor and had a tendency to build things in an overly complicated manner.
5+
The chicken coop has a digital display showing an encoded number representing the positions of all eggs that could be picked up.
6+
7+
Eliud is asking you to write a program that shows the actual number of eggs in the coop.
8+
9+
The position information encoding is calculated as follows:
10+
11+
1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
12+
2. Convert the number from binary to decimal.
13+
3. Show the result on the display.
14+
15+
Example 1:
16+
17+
```text
18+
Chicken Coop:
19+
_ _ _ _ _ _ _
20+
|E| |E|E| | |E|
21+
22+
Resulting Binary:
23+
1 0 1 1 0 0 1
24+
25+
Decimal number on the display:
26+
89
27+
28+
Actual eggs in the coop:
29+
4
30+
```
31+
32+
Example 2:
33+
34+
```text
35+
Chicken Coop:
36+
_ _ _ _ _ _ _ _
37+
| | | |E| | | | |
38+
39+
Resulting Binary:
40+
0 0 0 1 0 0 0 0
41+
42+
Decimal number on the display:
43+
16
44+
45+
Actual eggs in the coop:
46+
1
47+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"homersimpsons"
4+
],
5+
"files": {
6+
"solution": [
7+
"EliudsEggs.php"
8+
],
9+
"test": [
10+
"EliudsEggsTest.php"
11+
],
12+
"example": [
13+
".meta/example.php"
14+
]
15+
},
16+
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
17+
"source": "Christian Willner, Eric Willigers",
18+
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
19+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class EliudsEggs
6+
{
7+
public function eggCount(int $displayValue): int
8+
{
9+
if ($displayValue === 0) {
10+
return 0;
11+
}
12+
13+
return ($displayValue & 1) + $this->eggCount($displayValue >> 1);
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[559e789d-07d1-4422-9004-3b699f83bca3]
13+
description = "0 eggs"
14+
15+
[97223282-f71e-490c-92f0-b3ec9e275aba]
16+
description = "1 egg"
17+
18+
[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5]
19+
description = "4 eggs"
20+
21+
[0c18be92-a498-4ef2-bcbb-28ac4b06cb81]
22+
description = "13 eggs"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class EliudsEggs
28+
{
29+
public function eggCount(int $displayValue): int
30+
{
31+
throw new \BadMethodCallException("Implement the eggCount function");
32+
}
33+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* By adding type hints and enabling strict type checking, code can become
5+
* easier to read, self-documenting and reduce the number of potential bugs.
6+
* By default, type declarations are non-strict, which means they will attempt
7+
* to change the original type to match the type specified by the
8+
* type-declaration.
9+
*
10+
* In other words, if you pass a string to a function requiring a float,
11+
* it will attempt to convert the string value to a float.
12+
*
13+
* To enable strict mode, a single declare directive must be placed at the top
14+
* of the file.
15+
* This means that the strictness of typing is configured on a per-file basis.
16+
* This directive not only affects the type declarations of parameters, but also
17+
* a function's return type.
18+
*
19+
* For more info review the Concept on strict type checking in the PHP track
20+
* <link>.
21+
*
22+
* To disable strict typing, comment out the directive below.
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
class EliudsEggsTest extends PHPUnit\Framework\TestCase
28+
{
29+
public static function setUpBeforeClass(): void
30+
{
31+
require_once 'EliudsEggs.php';
32+
}
33+
34+
/**
35+
* uuid: 559e789d-07d1-4422-9004-3b699f83bca3
36+
* @testdox 0 eggs
37+
*/
38+
public function test0Eggs()
39+
{
40+
$eliudsEggs = new EliudsEggs();
41+
$this->assertEquals(0, $eliudsEggs->eggCount(0));
42+
}
43+
44+
/**
45+
* uuid: 97223282-f71e-490c-92f0-b3ec9e275aba
46+
* @testdox 1 eggs
47+
*/
48+
public function test1Eggs()
49+
{
50+
$eliudsEggs = new EliudsEggs();
51+
$this->assertEquals(1, $eliudsEggs->eggCount(16));
52+
}
53+
54+
/**
55+
* uuid: 1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5
56+
* @testdox 4 eggs
57+
*/
58+
public function test4Eggs()
59+
{
60+
$eliudsEggs = new EliudsEggs();
61+
$this->assertEquals(4, $eliudsEggs->eggCount(89));
62+
}
63+
64+
/**
65+
* uuid: 0c18be92-a498-4ef2-bcbb-28ac4b06cb81
66+
* @testdox 13 eggs
67+
*/
68+
public function test13Eggs()
69+
{
70+
$eliudsEggs = new EliudsEggs();
71+
$this->assertEquals(13, $eliudsEggs->eggCount(2000000000));
72+
}
73+
}

0 commit comments

Comments
 (0)