-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Add Pop Count practice exercise #1385
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. |
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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Used by "mix format" | ||
[ | ||
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"kahgoh" | ||
], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since she suggested a new test, I would suggest to add @angelikatyborska to the list of contributors (currently empty therefore missing) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's fine, I have enough attributions on the Elixir track 😁 time to merge! |
||
"files": { | ||
"solution": [ | ||
"lib/pop_count.ex" | ||
], | ||
"test": [ | ||
"test/pop_count_test.exs" | ||
], | ||
"example": [ | ||
".meta/example.ex" | ||
] | ||
}, | ||
"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" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Bitwise | ||
|
||
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 | ||
do_count(number, 0) | ||
end | ||
|
||
defp do_count(0, acc), do: acc | ||
|
||
defp do_count(number, acc) when number > 0 do | ||
do_count(number >>> 1, acc + (number &&& 1)) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# 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" | ||
include = false | ||
|
||
[97223282-f71e-490c-92f0-b3ec9e275aba] | ||
description = "1 egg" | ||
|
||
[1f8fd18f-26e9-4144-9a0e-57cdfc4f4ff5] | ||
description = "4 eggs" | ||
|
||
[0c18be92-a498-4ef2-bcbb-28ac4b06cb81] | ||
description = "13 eggs" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 | ||
kahgoh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ExUnit.start() | ||
ExUnit.configure(exclude: :pending, trace: true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what other concepts are necessary here. Definitely not just bit-manipulation because on the Elixir track, you can learn bit-manipulation together with anonymous functions literally as a second exercise, right after basics.
Your solution uses multiple clause functions, guards, and tail call recursion. I assume it could also be solved with normal recursion? If it could be solved with normal recursion only, that would be better as a prerequisite than tail call recursion as it's easier.
Do you know which language already published this exercise and has some community solutions that I could look at?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some published on the Elm track.
Some use, tail recursion, recursion, some don't use recursion at all (Elm compile to JS, so you know you have at most 32 bits).