Skip to content

Commit f30e807

Browse files
committed
Add Eliuds Eggs exercise
1 parent 3701616 commit f30e807

File tree

8 files changed

+241
-0
lines changed

8 files changed

+241
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,14 @@
12191219
"practices": [],
12201220
"prerequisites": [],
12211221
"difficulty": 3
1222+
},
1223+
{
1224+
"slug": "eliuds-eggs",
1225+
"name": "Eliud's Eggs",
1226+
"uuid": "9a8df843-8805-4287-b863-b8123defc266",
1227+
"practices": [],
1228+
"prerequisites": [],
1229+
"difficulty": 5
12221230
}
12231231
]
12241232
},
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [],
3+
"files": {
4+
"solution": [
5+
"EliudsEggs.php"
6+
],
7+
"test": [
8+
"EliudsEggsTest.php"
9+
],
10+
"example": [
11+
".meta/example.php"
12+
]
13+
},
14+
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
15+
"source": "Christian Willner, Eric Willigers",
16+
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
17+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
if ($displayValue === 0) {
32+
return 0;
33+
}
34+
35+
return ($displayValue & 1) + $this->eggCount($displayValue >> 1);
36+
}
37+
}
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: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
* @testdox 0 eggs
36+
*/
37+
public function test0Eggs()
38+
{
39+
$eliudsEggs = new EliudsEggs();
40+
$this->assertEquals(0, $eliudsEggs->eggCount(0));
41+
}
42+
43+
/**
44+
* @testdox 1 eggs
45+
*/
46+
public function test1Eggs()
47+
{
48+
$eliudsEggs = new EliudsEggs();
49+
$this->assertEquals(1, $eliudsEggs->eggCount(16));
50+
}
51+
52+
/**
53+
* @testdox 4 eggs
54+
*/
55+
public function test4Eggs()
56+
{
57+
$eliudsEggs = new EliudsEggs();
58+
$this->assertEquals(4, $eliudsEggs->eggCount(89));
59+
}
60+
61+
/**
62+
* @testdox 13 eggs
63+
*/
64+
public function test13Eggs()
65+
{
66+
$eliudsEggs = new EliudsEggs();
67+
$this->assertEquals(13, $eliudsEggs->eggCount(2000000000));
68+
}
69+
}

0 commit comments

Comments
 (0)