Eliud's Eggs
From Exercism PythonMITOriginal pagePython1 class period1 exercise4 graded tests
Eliud's Eggs
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:
- Scan the potential egg-laying spots and mark down a
1for an existing egg or a0for an empty spot. - Convert the number from binary to decimal.
- Show the result on the display.
Example 1
_ _ _ _ _ _ _
|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
_ _ _ _ _ _ _
| | | |E| | | |
Resulting Binary
_ _ _ _ _ _ _
|0|0|0|1|0|0|0|
Decimal number on the display
8
Actual eggs in the coop
1
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.
Open the
01folder and editmain.py. Run Tests checks it.
For teachers
Eliud's Eggs
Students solve Exercism's “Eliud's Eggs” Python exercise in CS Room, with Exercism's own tests.
- Time: 1 class period
- Tests: 4, set up for you when you import the lesson
- Practices: loops