Skip to content

Commit

Permalink
Add pop-count exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored and lpil committed Nov 5, 2023
1 parent 21016e8 commit 72c0b63
Show file tree
Hide file tree
Showing 12 changed files with 213 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1852,6 +1852,19 @@
"case-expressions"
],
"difficulty": 5
},
{
"slug": "pop-count",
"name": "Eliud's Eggs",
"uuid": "2af8d672-a825-4699-a77e-771d5d48a5d1",
"practices": [
"ints"
],
"prerequisites": [
"basics",
"ints"
],
"difficulty": 2
}
]
},
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
```
4 changes: 4 additions & 0 deletions exercises/practice/pop-count/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*.beam
*.ez
build
erl_crash.dump
23 changes: 23 additions & 0 deletions exercises/practice/pop-count/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/pop_count.gleam"
],
"test": [
"test/pop_count_test.gleam"
],
"example": [
".meta/example.gleam"
],
"invalidator": [
"gleam.toml",
"manifest.toml"
]
},
"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"
}
19 changes: 19 additions & 0 deletions exercises/practice/pop-count/.meta/example.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import gleam/int
import gleam/result

pub fn egg_count(number: Int) -> Int {
recurse(number, 0)
}

fn recurse(number: Int, eggs: Int) -> Int {
case number {
0 -> eggs
_ -> {
let new_number = number / 2
case int.is_odd(number) {
True -> recurse(new_number, eggs + 1)
_ -> recurse(new_number, eggs)
}
}
}
}
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"
11 changes: 11 additions & 0 deletions exercises/practice/pop-count/gleam.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "pop_count"
version = "0.1.0"

[dependencies]
gleam_bitwise = "~> 1.2"
gleam_otp = "~> 0.7"
gleam_stdlib = "~> 0.30"
simplifile = "~> 0.1"

[dev-dependencies]
exercism_test_runner = "~> 1.4"
11 changes: 11 additions & 0 deletions exercises/practice/pop-count/gleam.toml-e
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name = "packages"
version = "0.1.0"

[dependencies]
gleam_bitwise = "~> 1.2"
gleam_otp = "~> 0.7"
gleam_stdlib = "~> 0.30"
simplifile = "~> 0.1"

[dev-dependencies]
exercism_test_runner = "~> 1.4"
25 changes: 25 additions & 0 deletions exercises/practice/pop-count/manifest.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file was generated by Gleam
# You typically do not need to edit this file

packages = [
{ name = "exercism_test_runner", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "glance", "gleam_json", "gleam_community_ansi", "gleam_stdlib", "simplifile", "gap"], otp_app = "exercism_test_runner", source = "hex", outer_checksum = "336FBF790841C2DC25EB77B35E76A09EFDB9771D7D813E0FDBC71A50CB79711D" },
{ name = "gap", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_community_ansi", "gleam_stdlib"], otp_app = "gap", source = "hex", outer_checksum = "AF290C27B3FAE5FE64E1B7E9C70A9E29AA0F42429C0592D375770C1C51B79D36" },
{ name = "glance", version = "0.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "B646A08970990D9D7A103443C5CD46F9D4297BF05F188767777FCC14ADF395EA" },
{ name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" },
{ name = "gleam_community_ansi", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise", "gleam_community_colour"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "6E4E0CF2B207C1A7FCD3C21AA43514D67BC7004F21F82045CDCCE6C727A14862" },
{ name = "gleam_community_colour", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "D27CE357ECB343929A8CEC3FBA0B499943A47F0EE1F589EE16AFC2DC21C61E5B" },
{ name = "gleam_erlang", version = "0.22.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "367D8B41A7A86809928ED1E7E55BFD0D46D7C4CF473440190F324AFA347109B4" },
{ name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" },
{ name = "gleam_otp", version = "0.7.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "ED7381E90636E18F5697FD7956EECCA635A3B65538DC2BE2D91A38E61DCE8903" },
{ name = "gleam_stdlib", version = "0.31.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "6D1BC5B4D4179B9FEE866B1E69FE180AC2CE485AD90047C0B32B2CA984052736" },
{ name = "glexer", version = "0.6.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "703D2347F5180B2BCEA4D258549B0D91DACD0905010892BAC46D04D913B84D1F" },
{ name = "simplifile", version = "0.1.14", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "10EA0207796F20488A3A166C50A189C9385333F3C9FAC187729DE7B9CE4ADDBC" },
{ name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" },
]

[requirements]
exercism_test_runner = { version = "~> 1.4" }
gleam_bitwise = { version = "~> 1.2" }
gleam_otp = { version = "~> 0.7" }
gleam_stdlib = { version = "~> 0.30" }
simplifile = { version = "~> 0.1" }
3 changes: 3 additions & 0 deletions exercises/practice/pop-count/src/pop_count.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn egg_count(number: Int) -> Int {
todo
}
27 changes: 27 additions & 0 deletions exercises/practice/pop-count/test/pop_count_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import exercism/test_runner
import exercism/should
import pop_count

pub fn main() {
test_runner.main()
}

pub fn zero_eggs_test() {
pop_count.egg_count(0)
|> should.equal(0)
}

pub fn one_egg_test() {
pop_count.egg_count(16)
|> should.equal(1)
}

pub fn four_eggs_test() {
pop_count.egg_count(89)
|> should.equal(4)
}

pub fn thirteen_eggs_test() {
pop_count.egg_count(2_000_000_000)
|> should.equal(13)
}

0 comments on commit 72c0b63

Please sign in to comment.