diff --git a/config.json b/config.json index 871f4c8429..37b140d2f6 100644 --- a/config.json +++ b/config.json @@ -466,7 +466,7 @@ "slug": "house", "name": "House", "uuid": "7c2e93ae-d265-4481-b583-a496608c6031", - "practices": ["loops"], + "practices": [], "prerequisites": [ "basics", "lists", @@ -680,7 +680,7 @@ "slug": "protein-translation", "name": "Protein Translation", "uuid": "c89243f3-703e-4fe0-8e43-f200eedf2825", - "practices": ["loops"], + "practices": [], "prerequisites": [ "basics", "conditionals", @@ -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", diff --git a/exercises/practice/pop-count/.docs/instructions.md b/exercises/practice/pop-count/.docs/instructions.md new file mode 100644 index 0000000000..b0c2df593c --- /dev/null +++ b/exercises/practice/pop-count/.docs/instructions.md @@ -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. diff --git a/exercises/practice/pop-count/.docs/introduction.md b/exercises/practice/pop-count/.docs/introduction.md new file mode 100644 index 0000000000..49eaffd8bc --- /dev/null +++ b/exercises/practice/pop-count/.docs/introduction.md @@ -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 +``` diff --git a/exercises/practice/pop-count/.meta/config.json b/exercises/practice/pop-count/.meta/config.json new file mode 100644 index 0000000000..baf822a16a --- /dev/null +++ b/exercises/practice/pop-count/.meta/config.json @@ -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" +} diff --git a/exercises/practice/pop-count/.meta/example.py b/exercises/practice/pop-count/.meta/example.py new file mode 100644 index 0000000000..4e3320aee9 --- /dev/null +++ b/exercises/practice/pop-count/.meta/example.py @@ -0,0 +1,6 @@ +def egg_count(display_value): + eggs = 0 + while display_value: + eggs += display_value % 2 + display_value //= 2 + return eggs diff --git a/exercises/practice/pop-count/.meta/template.j2 b/exercises/practice/pop-count/.meta/template.j2 new file mode 100644 index 0000000000..11c9e487d3 --- /dev/null +++ b/exercises/practice/pop-count/.meta/template.j2 @@ -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 %} \ No newline at end of file diff --git a/exercises/practice/pop-count/.meta/tests.toml b/exercises/practice/pop-count/.meta/tests.toml new file mode 100644 index 0000000000..e11683c2ef --- /dev/null +++ b/exercises/practice/pop-count/.meta/tests.toml @@ -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" diff --git a/exercises/practice/pop-count/pop_count.py b/exercises/practice/pop-count/pop_count.py new file mode 100644 index 0000000000..bc88553850 --- /dev/null +++ b/exercises/practice/pop-count/pop_count.py @@ -0,0 +1,2 @@ +def egg_count(display_value): + pass diff --git a/exercises/practice/pop-count/pop_count_test.py b/exercises/practice/pop-count/pop_count_test.py new file mode 100644 index 0000000000..c3ca192156 --- /dev/null +++ b/exercises/practice/pop-count/pop_count_test.py @@ -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)