Skip to content

Commit 95f25cf

Browse files
vaengBethanyG
andauthored
feat: add pop-count exercise (#3518)
* feat: add pop-count practice exercise * config: del house, protein-trans from syllabus As pop-count was added to the loop concept, house and protein translation were removed to keep the tree lean. * Update config.json On second (and third) thought, let's put pop-count first -- ahead of saddle points and OCR. Feels like a better progression that way. --------- Co-authored-by: BethanyG <[email protected]>
1 parent ad890c1 commit 95f25cf

File tree

9 files changed

+160
-2
lines changed

9 files changed

+160
-2
lines changed

config.json

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@
466466
"slug": "house",
467467
"name": "House",
468468
"uuid": "7c2e93ae-d265-4481-b583-a496608c6031",
469-
"practices": ["loops"],
469+
"practices": [],
470470
"prerequisites": [
471471
"basics",
472472
"lists",
@@ -680,7 +680,7 @@
680680
"slug": "protein-translation",
681681
"name": "Protein Translation",
682682
"uuid": "c89243f3-703e-4fe0-8e43-f200eedf2825",
683-
"practices": ["loops"],
683+
"practices": [],
684684
"prerequisites": [
685685
"basics",
686686
"conditionals",
@@ -968,6 +968,21 @@
968968
"prerequisites": ["basics", "bools", "numbers", "classes"],
969969
"difficulty": 2
970970
},
971+
{
972+
"slug": "pop-count",
973+
"name": "Pop Count",
974+
"uuid": "356e2d29-7efc-4fa3-bec7-8b61c3e967da",
975+
"practices": ["loops"],
976+
"prerequisites": [
977+
"basics",
978+
"lists",
979+
"list-methods",
980+
"loops",
981+
"strings",
982+
"string-methods"
983+
],
984+
"difficulty": 3
985+
},
971986
{
972987
"slug": "saddle-points",
973988
"name": "Saddle Points",
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": ["vaeng"],
3+
"files": {
4+
"solution": [
5+
"pop_count.py"
6+
],
7+
"test": [
8+
"pop_count_test.py"
9+
],
10+
"example": [
11+
".meta/example.py"
12+
]
13+
},
14+
"blurb": "Count the 1 bits in a number",
15+
"source": "Christian Willner, Eric Willigers",
16+
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def egg_count(display_value):
2+
eggs = 0
3+
while display_value:
4+
eggs += display_value % 2
5+
display_value //= 2
6+
return eggs
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{%- import "generator_macros.j2" as macros with context -%}
2+
{{ macros.canonical_ref() }}
3+
4+
{{ macros.header() }}
5+
6+
class {{ exercise | camel_case }}Test(unittest.TestCase):
7+
{% for case in cases -%}
8+
def test_{{ case["description"] | to_snake }}(self):
9+
expected = {{ case["expected"] }}
10+
self.assertEqual(
11+
{{ case["property"] | to_snake }}({{ case["input"]["number"] }}), expected
12+
)
13+
14+
{% endfor %}
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def egg_count(display_value):
2+
pass
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# These tests are auto-generated with test data from:
2+
# https://github.com/exercism/problem-specifications/tree/main/exercises/pop-count/canonical-data.json
3+
# File last updated on 2023-10-18
4+
5+
import unittest
6+
7+
from pop_count import (
8+
egg_count,
9+
)
10+
11+
12+
class PopCountTest(unittest.TestCase):
13+
def test_0_eggs(self):
14+
expected = 0
15+
self.assertEqual(egg_count(0), expected)
16+
17+
def test_1_egg(self):
18+
expected = 1
19+
self.assertEqual(egg_count(16), expected)
20+
21+
def test_4_eggs(self):
22+
expected = 4
23+
self.assertEqual(egg_count(89), expected)
24+
25+
def test_13_eggs(self):
26+
expected = 13
27+
self.assertEqual(egg_count(2000000000), expected)

0 commit comments

Comments
 (0)