Skip to content

Commit 7423a8f

Browse files
authored
[eliuds-eggs] Add new practice exercise (#2792)
1 parent 439243f commit 7423a8f

11 files changed

Lines changed: 253 additions & 0 deletions

File tree

config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,19 @@
11651165
"strings"
11661166
]
11671167
},
1168+
{
1169+
"slug": "eliuds-eggs",
1170+
"name": "Eliud's Eggs",
1171+
"uuid": "82fb24cc-ac33-4cce-a77a-b59ce3c77832",
1172+
"practices": [],
1173+
"prerequisites": [],
1174+
"difficulty": 3,
1175+
"topics": [
1176+
"bitwise_operations",
1177+
"integers",
1178+
"math"
1179+
]
1180+
},
11681181
{
11691182
"slug": "all-your-base",
11701183
"name": "All Your Base",
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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
![Seven individual nest boxes arranged in a row whose first, third, fourth and seventh nests each have a single egg.](https://assets.exercism.org/images/exercises/eliuds-eggs/example-1-coop.svg)
18+
19+
```text
20+
_ _ _ _ _ _ _
21+
|E| |E|E| | |E|
22+
```
23+
24+
### Resulting Binary
25+
26+
![1011001](https://assets.exercism.org/images/exercises/eliuds-eggs/example-1-binary.svg)
27+
28+
```text
29+
_ _ _ _ _ _ _
30+
|1|0|1|1|0|0|1|
31+
```
32+
33+
### Decimal number on the display
34+
35+
89
36+
37+
### Actual eggs in the coop
38+
39+
4
40+
41+
## Example 2
42+
43+
![Seven individual nest boxes arranged in a row where only the fourth nest has an egg.](https://assets.exercism.org/images/exercises/eliuds-eggs/example-2-coop.svg)
44+
45+
```text
46+
_ _ _ _ _ _ _
47+
| | | |E| | | |
48+
```
49+
50+
### Resulting Binary
51+
52+
![0001000](https://assets.exercism.org/images/exercises/eliuds-eggs/example-2-binary.svg)
53+
54+
```text
55+
_ _ _ _ _ _ _
56+
|0|0|0|1|0|0|0|
57+
```
58+
59+
### Decimal number on the display
60+
61+
8
62+
63+
### Actual eggs in the coop
64+
65+
1
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"authors": [
3+
"KasimKaizer"
4+
],
5+
"files": {
6+
"solution": [
7+
"eliuds_eggs.go"
8+
],
9+
"test": [
10+
"eliuds_eggs_test.go"
11+
],
12+
"example": [
13+
".meta/example.go"
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+
}
20+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Package eliudseggs contains an example solution for eliuds-eggs exercise.
2+
package eliudseggs
3+
4+
// EggCount determines the actual egg count from the displayed value.
5+
func EggCount(displayValue int) int {
6+
count := 0
7+
for displayValue > 0 {
8+
if (displayValue & 1) == 1 {
9+
count++
10+
}
11+
displayValue >>= 1
12+
}
13+
return count
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"text/template"
6+
7+
"../../../../gen"
8+
)
9+
10+
func main() {
11+
t, err := template.New("").Parse(tmpl)
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
var j = map[string]interface{}{
16+
"eggCount": &[]testCase{},
17+
}
18+
if err := gen.Gen("eliuds-eggs", j, t); err != nil {
19+
log.Fatal(err)
20+
}
21+
}
22+
23+
type testCase struct {
24+
Description string `json:"description"`
25+
Input struct {
26+
Number int `json:"number"`
27+
} `json:"input"`
28+
Expected int `json:"expected"`
29+
}
30+
31+
// Template to generate various test cases.
32+
var tmpl = `{{.Header}}
33+
type testCase struct {
34+
description string
35+
input int
36+
expected int
37+
}
38+
39+
var testCases = []testCase{
40+
{{range .J.eggCount}}{
41+
description: {{printf "%q" .Description}},
42+
input: {{.Input.Number}},
43+
expected: {{.Expected}},
44+
},{{end}}
45+
}
46+
`
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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package eliudseggs
2+
3+
// This is an auto-generated file. Do not change it manually. Run the generator to update the file.
4+
// See https://github.com/exercism/go#synchronizing-tests-and-instructions
5+
// Source: exercism/problem-specifications
6+
// Commit: 1a96480 Add `eliuds-eggs` exercise (#2382)
7+
8+
type testCase struct {
9+
description string
10+
input int
11+
expected int
12+
}
13+
14+
var testCases = []testCase{
15+
{
16+
description: "0 eggs",
17+
input: 0,
18+
expected: 0,
19+
},
20+
{
21+
description: "1 egg",
22+
input: 16,
23+
expected: 1,
24+
},
25+
{
26+
description: "4 eggs",
27+
input: 89,
28+
expected: 4,
29+
},
30+
{
31+
description: "13 eggs",
32+
input: 2000000000,
33+
expected: 13,
34+
},
35+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package eliudseggs
2+
3+
func EggCount(displayValue int) int {
4+
panic("Please implement the EggCount function")
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package eliudseggs
2+
3+
import "testing"
4+
5+
func TestEggCount(t *testing.T) {
6+
for _, tc := range testCases {
7+
t.Run(tc.description, func(t *testing.T) {
8+
got := EggCount(tc.input)
9+
if got != tc.expected {
10+
t.Fatalf("EggCount(%d) = %d, want: %d", tc.input, got, tc.expected)
11+
}
12+
})
13+
}
14+
}
15+
16+
func BenchmarkEggCount(b *testing.B) {
17+
for range b.N {
18+
for _, tc := range testCases {
19+
EggCount(tc.input)
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)