Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pop-count practice exercise #725

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,21 @@
"difficulty": 4,
"topics": [
]
},
{
"slug": "pop-count",
"name": "Pop Count",
"uuid": "cfe01bad-0972-466c-b52a-9b8cf563b2f4",
"practices": [
"loops"
],
"prerequisites": [
"if-statements",
"comparisons"
],
"difficulty": 3,
"topics": [
]
}
],
"foregone": [
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
```
22 changes: 22 additions & 0 deletions exercises/practice/pop-count/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"vaeng"
],
"contributors": [],
"files": {
"solution": [
"pop_count.cpp",
"pop_count.h"
],
"test": [
"pop_count_test.cpp"
],
"example": [
".meta/example.cpp",
".meta/example.h"
]
},
"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"
}
12 changes: 12 additions & 0 deletions exercises/practice/pop-count/.meta/example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "pop_count.h"

namespace chicken_coop {
int positions_to_quantity(int display_value) {
int counter{};
while (display_value != 0) {
if (display_value % 2) ++counter;
display_value /= 2;
}
return counter;
}
} // namespace chicken_coop
5 changes: 5 additions & 0 deletions exercises/practice/pop-count/.meta/example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace chicken_coop {
int positions_to_quantity(int display_value);
} // namespace chicken_coop
11 changes: 11 additions & 0 deletions exercises/practice/pop-count/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[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"
64 changes: 64 additions & 0 deletions exercises/practice/pop-count/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Get the exercise name from the current directory
get_filename_component(exercise ${CMAKE_CURRENT_SOURCE_DIR} NAME)

# Basic CMake project
cmake_minimum_required(VERSION 3.5.1)

# Name the project after the exercise
project(${exercise} CXX)

# Get a source filename from the exercise name by replacing -'s with _'s
string(REPLACE "-" "_" file ${exercise})

# Implementation could be only a header
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${file}.cpp)
set(exercise_cpp ${file}.cpp)
else()
set(exercise_cpp "")
endif()

# Use the common Catch library?
if(EXERCISM_COMMON_CATCH)
# For Exercism track development only
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h $<TARGET_OBJECTS:catchlib>)
elseif(EXERCISM_TEST_SUITE)
# The Exercism test suite is being run, the Docker image already
# includes a pre-built version of Catch.
find_package(Catch2 REQUIRED)
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h)
target_link_libraries(${exercise} PRIVATE Catch2::Catch2WithMain)
# When Catch is installed system wide we need to include a different
# header, we need this define to use the correct one.
target_compile_definitions(${exercise} PRIVATE EXERCISM_TEST_SUITE)
else()
# Build executable from sources and headers
add_executable(${exercise} ${file}_test.cpp ${exercise_cpp} ${file}.h test/tests-main.cpp)
endif()

set_target_properties(${exercise} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS OFF
)

set(CMAKE_BUILD_TYPE Debug)

if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(GNU|Clang)")
set_target_properties(${exercise} PROPERTIES
COMPILE_FLAGS "-Wall -Wextra -Wpedantic -Werror"
)
endif()

# Configure to run all the tests?
if(${EXERCISM_RUN_ALL_TESTS})
target_compile_definitions(${exercise} PRIVATE EXERCISM_RUN_ALL_TESTS)
endif()

# Tell MSVC not to warn us about unchecked iterators in debug builds
if(${MSVC})
set_target_properties(${exercise} PROPERTIES
COMPILE_DEFINITIONS_DEBUG _SCL_SECURE_NO_WARNINGS)
endif()

# Run the tests on every build
add_custom_target(test_${exercise} ALL DEPENDS ${exercise} COMMAND ${exercise})
7 changes: 7 additions & 0 deletions exercises/practice/pop-count/pop_count.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "pop_count.h"

namespace chicken_coop {



} // namespace chicken_coop
7 changes: 7 additions & 0 deletions exercises/practice/pop-count/pop_count.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

namespace chicken_coop {



} // namespace chicken_coop
30 changes: 30 additions & 0 deletions exercises/practice/pop-count/pop_count_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "pop_count.h"
#ifdef EXERCISM_TEST_SUITE
#include <catch2/catch.hpp>
#else
#include "test/catch.hpp"
#endif

TEST_CASE("0 eggs")
{
REQUIRE(chicken_coop::positions_to_quantity(0) == 0);
}

#if defined(EXERCISM_RUN_ALL_TESTS)

TEST_CASE("1 egg")
{
REQUIRE(chicken_coop::positions_to_quantity(16) == 1);
}

TEST_CASE("4 eggs")
{
REQUIRE(chicken_coop::positions_to_quantity(89) == 4);
}

TEST_CASE("13 eggs")
{
REQUIRE(chicken_coop::positions_to_quantity(2000000000) == 13);
}

#endif
Loading