all_your_base

This commit is contained in:
Danil Negrienko 2024-06-26 20:24:48 -04:00
parent 22b46519fd
commit e0c8beb31f
10 changed files with 356 additions and 0 deletions

View File

@ -0,0 +1,27 @@
{
"authors": [
"ananthamapod"
],
"contributors": [
"angelikatyborska",
"ChristianTovar",
"Cohen-Carlisle",
"devonestes",
"neenjaw",
"parkerl",
"sotojuan",
"ybod"
],
"files": {
"solution": [
"lib/all_your_base.ex"
],
"test": [
"test/all_your_base_test.exs"
],
"example": [
".meta/example.ex"
]
},
"blurb": "Convert a number, represented as a sequence of digits in one base, to any other base."
}

View File

@ -0,0 +1 @@
{"track":"elixir","exercise":"all-your-base","id":"98d3bf453a6d4f1383988013aadb22c0","url":"https://exercism.org/tracks/elixir/exercises/all-your-base","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/all-your-base/.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").
all_your_base-*.tar

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/all_your_base.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,55 @@
# All Your Base
Welcome to All Your Base on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Convert a number, represented as a sequence of digits in one base, to any other base.
Implement general base conversion.
Given a number in base **a**, represented as a sequence of digits, convert it to base **b**.
## Note
- Try to implement the conversion yourself.
Do not use something else to perform the conversion for you.
## About [Positional Notation][positional-notation]
In positional notation, a number in base **b** can be understood as a linear combination of powers of **b**.
The number 42, _in base 10_, means:
`(4 * 10^1) + (2 * 10^0)`
The number 101010, _in base 2_, means:
`(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)`
The number 1120, _in base 3_, means:
`(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)`
I think you got the idea!
_Yes. Those three numbers above are exactly the same. Congratulations!_
[positional-notation]: https://en.wikipedia.org/wiki/Positional_notation
## Source
### Created by
- @ananthamapod
### Contributed to by
- @angelikatyborska
- @ChristianTovar
- @Cohen-Carlisle
- @devonestes
- @neenjaw
- @parkerl
- @sotojuan
- @ybod

View File

@ -0,0 +1,31 @@
defmodule AllYourBase do
@doc """
Given a number in input base, represented as a sequence of digits, converts it to output base,
or returns an error tuple if either of the bases are less than 2
"""
@spec convert(list, integer, integer) :: {:ok, list} | {:error, String.t()}
def convert(_digits, input_base, _output_base) when input_base < 2,
do: {:error, "input base must be >= 2"}
def convert(_digits, _input_base, output_base) when output_base < 2,
do: {:error, "output base must be >= 2"}
def convert([0 | digits], input_base, output_base), do: convert(digits, input_base, output_base)
def convert(digits, input_base, output_base) do
unless Enum.all?(digits, &valid_digit?(&1, input_base)) do
{:error, "all digits must be >= 0 and < input base"}
else
result =
digits
|> Integer.undigits(input_base)
|> Integer.digits(output_base)
{:ok, result}
end
end
defp valid_digit?(digit, base) when digit >= 0 and digit < base, do: true
defp valid_digit?(_digit, _base), do: false
end

View File

@ -0,0 +1,28 @@
defmodule AllYourBase.MixProject do
use Mix.Project
def project do
[
app: :all_your_base,
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,109 @@
defmodule AllYourBaseTest do
use ExUnit.Case
test "convert single bit one to decimal" do
assert AllYourBase.convert([1], 2, 10) == {:ok, [1]}
end
# @tag :pending
test "convert binary to single decimal" do
assert AllYourBase.convert([1, 0, 1], 2, 10) == {:ok, [5]}
end
# @tag :pending
test "convert single decimal to binary" do
assert AllYourBase.convert([5], 10, 2) == {:ok, [1, 0, 1]}
end
# @tag :pending
test "convert binary to multiple decimal" do
assert AllYourBase.convert([1, 0, 1, 0, 1, 0], 2, 10) == {:ok, [4, 2]}
end
# @tag :pending
test "convert decimal to binary" do
assert AllYourBase.convert([4, 2], 10, 2) == {:ok, [1, 0, 1, 0, 1, 0]}
end
# @tag :pending
test "convert trinary to hexadecimal" do
assert AllYourBase.convert([1, 1, 2, 0], 3, 16) == {:ok, [2, 10]}
end
# @tag :pending
test "convert hexadecimal to trinary" do
assert AllYourBase.convert([2, 10], 16, 3) == {:ok, [1, 1, 2, 0]}
end
# @tag :pending
test "convert 15-bit integer" do
assert AllYourBase.convert([3, 46, 60], 97, 73) == {:ok, [6, 10, 45]}
end
# @tag :pending
test "convert empty list" do
assert AllYourBase.convert([], 2, 10) == {:ok, [0]}
end
# @tag :pending
test "convert single zero" do
assert AllYourBase.convert([0], 10, 2) == {:ok, [0]}
end
# @tag :pending
test "convert multiple zeros" do
assert AllYourBase.convert([0, 0, 0], 10, 2) == {:ok, [0]}
end
# @tag :pending
test "convert leading zeros" do
assert AllYourBase.convert([0, 6, 0], 7, 10) == {:ok, [4, 2]}
end
# @tag :pending
test "convert first base is one" do
assert AllYourBase.convert([0], 1, 10) == {:error, "input base must be >= 2"}
end
# @tag :pending
test "convert first base is zero" do
assert AllYourBase.convert([], 0, 10) == {:error, "input base must be >= 2"}
end
# @tag :pending
test "convert first base is negative" do
assert AllYourBase.convert([1], -2, 10) == {:error, "input base must be >= 2"}
end
# @tag :pending
test "convert negative digit" do
assert AllYourBase.convert([1, -1, 1, 0, 1, 0], 2, 10) ==
{:error, "all digits must be >= 0 and < input base"}
end
# @tag :pending
test "convert invalid positive digit" do
assert AllYourBase.convert([1, 2, 1, 0, 1, 0], 2, 10) ==
{:error, "all digits must be >= 0 and < input base"}
end
# @tag :pending
test "convert second base is one" do
assert AllYourBase.convert([1, 0, 1, 0, 1, 0], 2, 1) == {:error, "output base must be >= 2"}
end
# @tag :pending
test "convert second base is zero" do
assert AllYourBase.convert([7], 10, 0) == {:error, "output base must be >= 2"}
end
# @tag :pending
test "convert second base is negative" do
assert AllYourBase.convert([1], 2, -7) == {:error, "output base must be >= 2"}
end
# @tag :pending
test "convert both bases are negative" do
assert AllYourBase.convert([1], -2, -7) == {:error, "input base must be >= 2"}
end
end

View File

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