Skip to content

Commit

Permalink
feat: add pop-count exercise (#3518)
Browse files Browse the repository at this point in the history
* 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 <BethanyG@users.noreply.github.com>
  • Loading branch information
vaeng and BethanyG authored Oct 18, 2023
1 parent ad890c1 commit 95f25cf
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 2 deletions.
19 changes: 17 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@
"slug": "house",
"name": "House",
"uuid": "7c2e93ae-d265-4481-b583-a496608c6031",
"practices": ["loops"],
"practices": [],
"prerequisites": [
"basics",
"lists",
Expand Down Expand Up @@ -680,7 +680,7 @@
"slug": "protein-translation",
"name": "Protein Translation",
"uuid": "c89243f3-703e-4fe0-8e43-f200eedf2825",
"practices": ["loops"],
"practices": [],
"prerequisites": [
"basics",
"conditionals",
Expand Down Expand Up @@ -968,6 +968,21 @@
"prerequisites": ["basics", "bools", "numbers", "classes"],
"difficulty": 2
},
{
"slug": "pop-count",
"name": "Pop Count",
"uuid": "356e2d29-7efc-4fa3-bec7-8b61c3e967da",
"practices": ["loops"],
"prerequisites": [
"basics",
"lists",
"list-methods",
"loops",
"strings",
"string-methods"
],
"difficulty": 3
},
{
"slug": "saddle-points",
"name": "Saddle Points",
Expand Down
8 changes: 8 additions & 0 deletions exercises/practice/pop-count/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 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.
47 changes: 47 additions & 0 deletions exercises/practice/pop-count/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 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:

1. Scan the potential egg-laying spots and mark down a `1` for an existing egg or a `0` for an empty spot.
2. Convert the number from binary to decimal.
3. Show the result on the display.

Example 1:

```text
Chicken Coop:
_ _ _ _ _ _ _
|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:

```text
Chicken Coop:
_ _ _ _ _ _ _ _
| | | |E| | | | |
Resulting Binary:
0 0 0 1 0 0 0 0
Decimal number on the display:
16
Actual eggs in the coop:
1
```
17 changes: 17 additions & 0 deletions exercises/practice/pop-count/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["vaeng"],
"files": {
"solution": [
"pop_count.py"
],
"test": [
"pop_count_test.py"
],
"example": [
".meta/example.py"
]
},
"blurb": "Count the 1 bits in a number",
"source": "Christian Willner, Eric Willigers",
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
}
6 changes: 6 additions & 0 deletions exercises/practice/pop-count/.meta/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def egg_count(display_value):
eggs = 0
while display_value:
eggs += display_value % 2
display_value //= 2
return eggs
14 changes: 14 additions & 0 deletions exercises/practice/pop-count/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{%- import "generator_macros.j2" as macros with context -%}
{{ macros.canonical_ref() }}

{{ macros.header() }}

class {{ exercise | camel_case }}Test(unittest.TestCase):
{% for case in cases -%}
def test_{{ case["description"] | to_snake }}(self):
expected = {{ case["expected"] }}
self.assertEqual(
{{ case["property"] | to_snake }}({{ case["input"]["number"] }}), expected
)

{% endfor %}
22 changes: 22 additions & 0 deletions exercises/practice/pop-count/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[559e789d-07d1-4422-9004-3b699f83bca3]
description = "0 eggs"

[97223282-f71e-490c-92f0-b3ec9e275aba]
description = "1 egg"

[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5]
description = "4 eggs"

[0c18be92-a498-4ef2-bcbb-28ac4b06cb81]
description = "13 eggs"
2 changes: 2 additions & 0 deletions exercises/practice/pop-count/pop_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def egg_count(display_value):
pass
27 changes: 27 additions & 0 deletions exercises/practice/pop-count/pop_count_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# These tests are auto-generated with test data from:
# https://github.com/exercism/problem-specifications/tree/main/exercises/pop-count/canonical-data.json
# File last updated on 2023-10-18

import unittest

from pop_count import (
egg_count,
)


class PopCountTest(unittest.TestCase):
def test_0_eggs(self):
expected = 0
self.assertEqual(egg_count(0), expected)

def test_1_egg(self):
expected = 1
self.assertEqual(egg_count(16), expected)

def test_4_eggs(self):
expected = 4
self.assertEqual(egg_count(89), expected)

def test_13_eggs(self):
expected = 13
self.assertEqual(egg_count(2000000000), expected)

0 comments on commit 95f25cf

Please sign in to comment.