Skip to content

Commit 6c59db2

Browse files
Add practice exercise pop-count (#2540)
1 parent 7f033c5 commit 6c59db2

File tree

10 files changed

+193
-0
lines changed

10 files changed

+193
-0
lines changed

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,18 @@
19711971
"numbers"
19721972
],
19731973
"difficulty": 5
1974+
},
1975+
{
1976+
"slug": "pop-count",
1977+
"name": "Pop Count",
1978+
"uuid": "2d5b6404-3315-48c1-892f-b594a960e7a1",
1979+
"practices": [
1980+
"numbers"
1981+
],
1982+
"prerequisites": [
1983+
"numbers"
1984+
],
1985+
"difficulty": 3
19741986
}
19751987
]
19761988
},
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"sanderploegsma"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/main/java/PopCount.java"
8+
],
9+
"test": [
10+
"src/test/java/PopCountTest.java"
11+
],
12+
"example": [
13+
".meta/src/reference/java/PopCount.java"
14+
],
15+
"invalidator": [
16+
"build.gradle"
17+
]
18+
},
19+
"blurb": "Count the 1 bits in a number",
20+
"source": "Christian Willner, Eric Willigers",
21+
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class PopCount {
2+
public int eggCount(int number) {
3+
var count = 0;
4+
var remaining = number;
5+
6+
while (remaining > 0) {
7+
if ((remaining & 1) > 0) {
8+
count++;
9+
}
10+
remaining = remaining >> 1;
11+
}
12+
13+
return count;
14+
}
15+
}
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
12+
testImplementation "org.assertj:assertj-core:3.15.0"
13+
}
14+
15+
test {
16+
useJUnitPlatform()
17+
18+
testLogging {
19+
exceptionFormat = "full"
20+
showStandardStreams = true
21+
events = ["passed", "failed", "skipped"]
22+
}
23+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class PopCount {
2+
public int eggCount(int number) {
3+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
4+
}
5+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import org.junit.jupiter.api.Disabled;
2+
import org.junit.jupiter.api.DisplayName;
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
7+
public class PopCountTest {
8+
@Test
9+
@DisplayName("0 eggs")
10+
public void test0Eggs() {
11+
assertThat(new PopCount().eggCount(0))
12+
.isEqualTo(0);
13+
}
14+
15+
@Disabled("Remove to run test")
16+
@Test
17+
@DisplayName("1 egg")
18+
public void test1Egg() {
19+
assertThat(new PopCount().eggCount(16))
20+
.isEqualTo(1);
21+
}
22+
23+
@Disabled("Remove to run test")
24+
@Test
25+
@DisplayName("4 eggs")
26+
public void test4Eggs() {
27+
assertThat(new PopCount().eggCount(89))
28+
.isEqualTo(4);
29+
}
30+
31+
@Disabled("Remove to run test")
32+
@Test
33+
@DisplayName("13 eggs")
34+
public void test13Eggs() {
35+
assertThat(new PopCount().eggCount(2000000000))
36+
.isEqualTo(13);
37+
}
38+
}

exercises/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ include 'practice:perfect-numbers'
9595
include 'practice:phone-number'
9696
include 'practice:pig-latin'
9797
include 'practice:poker'
98+
include 'practice:pop-count'
9899
include 'practice:pov'
99100
include 'practice:prime-factors'
100101
include 'practice:protein-translation'

0 commit comments

Comments
 (0)