From 72c0b63639651e7512ce9bfd1bf2d5e9591dd3ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Wed, 1 Nov 2023 21:12:43 -0700 Subject: [PATCH] Add pop-count exercise --- config.json | 13 +++++ .../practice/pop-count/.docs/instructions.md | 8 ++++ .../practice/pop-count/.docs/introduction.md | 47 +++++++++++++++++++ exercises/practice/pop-count/.gitignore | 4 ++ .../practice/pop-count/.meta/config.json | 23 +++++++++ .../practice/pop-count/.meta/example.gleam | 19 ++++++++ exercises/practice/pop-count/.meta/tests.toml | 22 +++++++++ exercises/practice/pop-count/gleam.toml | 11 +++++ exercises/practice/pop-count/gleam.toml-e | 11 +++++ exercises/practice/pop-count/manifest.toml | 25 ++++++++++ .../practice/pop-count/src/pop_count.gleam | 3 ++ .../pop-count/test/pop_count_test.gleam | 27 +++++++++++ 12 files changed, 213 insertions(+) create mode 100644 exercises/practice/pop-count/.docs/instructions.md create mode 100644 exercises/practice/pop-count/.docs/introduction.md create mode 100644 exercises/practice/pop-count/.gitignore create mode 100644 exercises/practice/pop-count/.meta/config.json create mode 100644 exercises/practice/pop-count/.meta/example.gleam create mode 100644 exercises/practice/pop-count/.meta/tests.toml create mode 100644 exercises/practice/pop-count/gleam.toml create mode 100644 exercises/practice/pop-count/gleam.toml-e create mode 100644 exercises/practice/pop-count/manifest.toml create mode 100644 exercises/practice/pop-count/src/pop_count.gleam create mode 100644 exercises/practice/pop-count/test/pop_count_test.gleam diff --git a/config.json b/config.json index d26fd79e9..1ec192932 100644 --- a/config.json +++ b/config.json @@ -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 } ] }, diff --git a/exercises/practice/pop-count/.docs/instructions.md b/exercises/practice/pop-count/.docs/instructions.md new file mode 100644 index 000000000..b0c2df593 --- /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 000000000..49eaffd8b --- /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/.gitignore b/exercises/practice/pop-count/.gitignore new file mode 100644 index 000000000..170cca981 --- /dev/null +++ b/exercises/practice/pop-count/.gitignore @@ -0,0 +1,4 @@ +*.beam +*.ez +build +erl_crash.dump diff --git a/exercises/practice/pop-count/.meta/config.json b/exercises/practice/pop-count/.meta/config.json new file mode 100644 index 000000000..2ebdd1e92 --- /dev/null +++ b/exercises/practice/pop-count/.meta/config.json @@ -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" +} diff --git a/exercises/practice/pop-count/.meta/example.gleam b/exercises/practice/pop-count/.meta/example.gleam new file mode 100644 index 000000000..d3f684bab --- /dev/null +++ b/exercises/practice/pop-count/.meta/example.gleam @@ -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) + } + } + } +} diff --git a/exercises/practice/pop-count/.meta/tests.toml b/exercises/practice/pop-count/.meta/tests.toml new file mode 100644 index 000000000..e11683c2e --- /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/gleam.toml b/exercises/practice/pop-count/gleam.toml new file mode 100644 index 000000000..55487aefb --- /dev/null +++ b/exercises/practice/pop-count/gleam.toml @@ -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" diff --git a/exercises/practice/pop-count/gleam.toml-e b/exercises/practice/pop-count/gleam.toml-e new file mode 100644 index 000000000..a740fe17d --- /dev/null +++ b/exercises/practice/pop-count/gleam.toml-e @@ -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" diff --git a/exercises/practice/pop-count/manifest.toml b/exercises/practice/pop-count/manifest.toml new file mode 100644 index 000000000..a8cdf8b1d --- /dev/null +++ b/exercises/practice/pop-count/manifest.toml @@ -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" } diff --git a/exercises/practice/pop-count/src/pop_count.gleam b/exercises/practice/pop-count/src/pop_count.gleam new file mode 100644 index 000000000..ddf0fc0c6 --- /dev/null +++ b/exercises/practice/pop-count/src/pop_count.gleam @@ -0,0 +1,3 @@ +pub fn egg_count(number: Int) -> Int { + todo +} diff --git a/exercises/practice/pop-count/test/pop_count_test.gleam b/exercises/practice/pop-count/test/pop_count_test.gleam new file mode 100644 index 000000000..d50e7de3f --- /dev/null +++ b/exercises/practice/pop-count/test/pop_count_test.gleam @@ -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) +}