pop-count

This commit is contained in:
Danil Negrienko 2023-12-20 23:21:06 -05:00
parent ec644472b3
commit 833fe9c0f5
10 changed files with 304 additions and 0 deletions

View File

@ -0,0 +1,19 @@
{
"authors": [
"kahgoh"
],
"files": {
"solution": [
"lib/pop_count.ex"
],
"test": [
"test/pop_count_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Help Eliud count the number of eggs in her chicken coop by counting the number of 1 bits in a binary representation.",
"source": "Christian Willner, Eric Willigers",
"source_url": "https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5"
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"pop-count","id":"d2363c035a2748d9a3b9561ce18f0315","url":"https://exercism.org/tracks/elixir/exercises/pop-count","handle":"negrienko","is_requester":true,"auto_approve":false}

View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

24
elixir/pop-count/.gitignore vendored Normal file
View File

@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/
# If you run "mix test --cover", coverage assets end up here.
/cover/
# The directory Mix downloads your dependencies sources to.
/deps/
# Where third-party dependencies like ExDoc output generated docs.
/doc/
# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
isogram-*.tar

75
elixir/pop-count/HELP.md Normal file
View File

@ -0,0 +1,75 @@
# Help
## Running the tests
From the terminal, change to the base directory of the exercise then execute the tests with:
```bash
$ mix test
```
This will execute the test file found in the `test` subfolder -- a file ending in `_test.exs`
Documentation:
* [`mix test` - Elixir's test execution tool](https://hexdocs.pm/mix/Mix.Tasks.Test.html)
* [`ExUnit` - Elixir's unit test library](https://hexdocs.pm/ex_unit/ExUnit.html)
## Pending tests
In test suites of practice exercises, all but the first test have been tagged to be skipped.
Once you get a test passing, you can unskip the next one by commenting out the relevant `@tag :pending` with a `#` symbol.
For example:
```elixir
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
```
If you wish to run all tests at once, you can include all skipped test by using the `--include` flag on the `mix test` command:
```bash
$ mix test --include pending
```
Or, you can enable all the tests by commenting out the `ExUnit.configure` line in the file `test/test_helper.exs`.
```elixir
# ExUnit.configure(exclude: :pending, trace: true)
```
## Useful `mix test` options
* `test/<FILE>.exs:LINENUM` - runs only a single test, the test from `<FILE>.exs` whose definition is on line `LINENUM`
* `--failed` - runs only tests that failed the last time they ran
* `--max-failures` - the suite stops evaluating tests when this number of test failures
is reached
* `--seed 0` - disables randomization so the tests in a single file will always be ran
in the same order they were defined in
## Submitting your solution
You can submit your solution using the `exercism submit lib/pop_count.ex` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Elixir track's documentation](https://exercism.org/docs/tracks/elixir)
- The [Elixir track's programming category on the forum](https://forum.exercism.org/c/programming/elixir)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
If you're stuck on something, it may help to look at some of the [available resources](https://exercism.org/docs/tracks/elixir/resources) out there where answers might be found.

View File

@ -0,0 +1,71 @@
# Eliud's Eggs
Welcome to Eliud's Eggs on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## 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
```
## 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.
## Source
### Created by
- @kahgoh
### Based on
Christian Willner, Eric Willigers - https://forum.exercism.org/t/new-exercise-suggestion-pop-count/7632/5

View File

@ -0,0 +1,51 @@
defmodule PopCount do
@doc """
Given the number, count the number of eggs.
"""
@spec egg_count(number :: integer()) :: non_neg_integer()
def egg_count(number) do
for(<<bit::1 <- :binary.encode_unsigned(number)>>, do: bit) |> Enum.sum
end
# @spec egg_count(number :: integer()) :: non_neg_integer()
# def egg_count(number) do
# result =
# number
# |> Integer.digits(2)
# |> Enum.reduce(%{binary: "", count: 0, coop: "|", length: 0}, &egg_counter/2)
# """
# Chicken Coop:
# #{String.duplicate(" _", result.length)}
# #{result.coop}
# Resulting Binary:
# #{result.binary}
# Decimal number on the display:
# #{number}
# Actual eggs in the coop:
# #{result.count}
# """
# |> IO.puts()
# result.count
# end
# defp egg_counter(element, %{binary: binary, count: count, coop: coop, length: length} = _acc) do
# char =
# case element do
# 0 -> " "
# 1 -> "E"
# end
# %{
# coop: coop <> char <> "|",
# count: count + element,
# length: length + 1,
# binary: binary <> "#{element}"
# }
# end
end

28
elixir/pop-count/mix.exs Normal file
View File

@ -0,0 +1,28 @@
defmodule PopCount.MixProject do
use Mix.Project
def project do
[
app: :pop_count,
version: "0.1.0",
# elixir: "~> 1.8",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

View File

@ -0,0 +1,29 @@
defmodule PopCountTest do
use ExUnit.Case
describe "egg count" do
test "0 eggs" do
assert PopCount.egg_count(0) == 0
end
# @tag :pending
test "1 egg" do
assert PopCount.egg_count(16) == 1
end
# @tag :pending
test "4 eggs" do
assert PopCount.egg_count(89) == 4
end
# @tag :pending
test "13 eggs" do
assert PopCount.egg_count(2_000_000_000) == 13
end
# @tag :pending
test "100 eggs" do
assert PopCount.egg_count(1_267_650_600_228_229_401_496_703_205_375) == 100
end
end
end

View File

@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)