Skip to content

Commit 31416d2

Browse files
committed
Add Eliud's eggs exercise
1 parent d265195 commit 31416d2

File tree

8 files changed

+154
-0
lines changed

8 files changed

+154
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,14 @@
535535
"difficulty": 4,
536536
"status": "beta"
537537
},
538+
{
539+
"slug": "eliuds-eggs",
540+
"name": "Eliud's Eggs",
541+
"uuid": "683068c2-97df-430d-a713-b4fca940c12d",
542+
"practices": [],
543+
"prerequisites": [],
544+
"difficulty": 2
545+
},
538546
{
539547
"slug": "gigasecond",
540548
"name": "Gigasecond",
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+
"kahgoh"
4+
],
5+
"files": {
6+
"solution": [
7+
"eliuds-eggs.lisp"
8+
],
9+
"test": [
10+
"eliuds-eggs-test.lisp"
11+
],
12+
"example": [
13+
".meta/example.lisp"
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(defpackage :eliuds-eggs
2+
(:use :cl)
3+
(:export :egg-count))
4+
5+
(in-package :eliuds-eggs)
6+
7+
(defun do-egg-count (number &optional (acc 0))
8+
(if (= number 0)
9+
acc
10+
(multiple-value-bind (quot rem) (floor number 2)
11+
(do-egg-count quot (+ acc rem)))))
12+
13+
(defun egg-count (number)
14+
(do-egg-count number))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[559e789d-07d1-4422-9004-3b699f83bca3]
6+
description = "0 eggs"
7+
8+
[97223282-f71e-490c-92f0-b3ec9e275aba]
9+
description = "1 egg"
10+
11+
[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5]
12+
description = "4 eggs"
13+
14+
[0c18be92-a498-4ef2-bcbb-28ac4b06cb81]
15+
description = "13 eggs"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
;; Ensures that eliuds-eggs.lisp and the testing library are always loaded
2+
(eval-when (:compile-toplevel :load-toplevel :execute)
3+
(load "eliuds-eggs")
4+
(quicklisp-client:quickload :fiveam))
5+
6+
;; Defines the testing package with symbols from eliuds-eggs and FiveAM in scope
7+
;; The `run-tests` function is exported for use by both the user and test-runner
8+
(defpackage :eliuds-eggs-test
9+
(:use :cl :fiveam)
10+
(:export :run-tests))
11+
12+
;; Enter the testing package
13+
(in-package :eliuds-eggs-test)
14+
15+
;; Define and enter a new FiveAM test-suite
16+
(def-suite* eliuds-eggs-suite)
17+
18+
(test 0-eggs
19+
(let ((number 0))
20+
(is (= 0 (eliuds-eggs:egg-count number)))))
21+
22+
(test 1-egg
23+
(let ((number 16))
24+
(is (= 1 (eliuds-eggs:egg-count number)))))
25+
26+
(test 4-eggs
27+
(let ((number 89))
28+
(is (= 4 (eliuds-eggs:egg-count number)))))
29+
30+
(test 13-eggs
31+
(let ((number 2000000000))
32+
(is (= 13 (eliuds-eggs:egg-count number)))))
33+
34+
(defun run-tests (&optional (test-or-suite 'eliuds-eggs-suite))
35+
"Provides human readable results of test run. Default to entire suite."
36+
(run! test-or-suite))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(defpackage :eliuds-eggs
2+
(:use :cl)
3+
(:export :egg-count))
4+
5+
(in-package :eliuds-eggs)
6+
7+
(defun egg-count (number))

0 commit comments

Comments
 (0)