Skip to content

Commit

Permalink
Implement roman-numerals in Elixir
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgoettschkes committed Dec 9, 2023
1 parent 92ef1bb commit f0d0aa0
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 0 deletions.
4 changes: 4 additions & 0 deletions elixir/roman-numerals/.formatter.exs
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}"]
]
24 changes: 24 additions & 0 deletions elixir/roman-numerals/.gitignore
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").
roman_numerals-*.tar

75 changes: 75 additions & 0 deletions elixir/roman-numerals/HELP.md
Original file line number Diff line number Diff line change
@@ -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/roman_numerals.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.
72 changes: 72 additions & 0 deletions elixir/roman-numerals/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Roman Numerals

Welcome to Roman Numerals on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch.
They conquered most of Europe and ruled it for hundreds of years.
They invented concrete and straight roads and even bikinis.
One thing they never discovered though was the number zero.
This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today.
For example the BBC uses Roman numerals to date their programs.

The Romans wrote numbers using letters - I, V, X, L, C, D, M.
(notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

```text
1 => I
10 => X
7 => VII
```

The maximum number supported by this notation is 3,999.
(The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M
900=CM
90=XC

2008 is written as MMVIII:

2000=MM
8=VIII

Learn more about [Roman numerals on Wikipedia][roman-numerals].

[roman-numerals]: https://wiki.imperivm-romanvm.com/wiki/Roman_Numerals

## Source

### Created by

- @rubysolo

### Contributed to by

- @andrewsardone
- @angelikatyborska
- @cetinajero
- @Cohen-Carlisle
- @dalexj
- @devonestes
- @jinyeow
- @lpil
- @neenjaw
- @parkerl
- @sotojuan
- @Teapane
- @waiting-for-dev

### Based on

The Roman Numeral Kata - https://codingdojo.org/kata/RomanNumerals/
48 changes: 48 additions & 0 deletions elixir/roman-numerals/lib/roman_numerals.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
defmodule RomanNumerals do
@doc """
Convert the number to a roman number.
"""
@spec numeral(pos_integer) :: String.t()
def numeral(number) do
result =
[1000, 500, 100, 50, 10, 5, 1]
|> Enum.reduce({"", number}, fn cur, {result, num} ->
case round(:math.floor(num / cur)) do
0 ->
{result, num}

amount ->
result =
result <>
(Range.to_list(1..amount) |> Enum.map(fn _ -> to_literal(cur) end) |> Enum.join())

{result, num - amount * cur}
end
end)
|> then(fn {result, _} -> result end)

Enum.reduce(
[
{"DCCCC", "CM"},
{"CCCC", "CD"},
{"LXXXX", "XC"},
{"XXXX", "XL"},
{"VIIII", "IX"},
{"IIII", "IV"}
],
result,
fn {src, dest}, acc ->
String.replace(acc, src, dest)
end
)
end

defp to_literal(1), do: "I"
defp to_literal(5), do: "V"
defp to_literal(10), do: "X"
defp to_literal(50), do: "L"
defp to_literal(100), do: "C"
defp to_literal(500), do: "D"
defp to_literal(1000), do: "M"
defp to_literal(_), do: ""
end
28 changes: 28 additions & 0 deletions elixir/roman-numerals/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule RomanNumerals.MixProject do
use Mix.Project

def project do
[
app: :roman_numerals,
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
103 changes: 103 additions & 0 deletions elixir/roman-numerals/test/roman_numerals_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
defmodule RomanNumeralsTest do
use ExUnit.Case

test "1" do
assert RomanNumerals.numeral(1) == "I"
end

test "2" do
assert RomanNumerals.numeral(2) == "II"
end

test "3" do
assert RomanNumerals.numeral(3) == "III"
end

test "4" do
assert RomanNumerals.numeral(4) == "IV"
end

test "5" do
assert RomanNumerals.numeral(5) == "V"
end

test "6" do
assert RomanNumerals.numeral(6) == "VI"
end

test "9" do
assert RomanNumerals.numeral(9) == "IX"
end

test "27" do
assert RomanNumerals.numeral(27) == "XXVII"
end

test "48" do
assert RomanNumerals.numeral(48) == "XLVIII"
end

test "59" do
assert RomanNumerals.numeral(59) == "LIX"
end

test "93" do
assert RomanNumerals.numeral(93) == "XCIII"
end

test "141" do
assert RomanNumerals.numeral(141) == "CXLI"
end

test "163" do
assert RomanNumerals.numeral(163) == "CLXIII"
end

test "402" do
assert RomanNumerals.numeral(402) == "CDII"
end

test "575" do
assert RomanNumerals.numeral(575) == "DLXXV"
end

test "911" do
assert RomanNumerals.numeral(911) == "CMXI"
end

test "1024" do
assert RomanNumerals.numeral(1024) == "MXXIV"
end

test "3000" do
assert RomanNumerals.numeral(3000) == "MMM"
end

test "16" do
assert RomanNumerals.numeral(16) == "XVI"
end

test "66" do
assert RomanNumerals.numeral(66) == "LXVI"
end

test "166" do
assert RomanNumerals.numeral(166) == "CLXVI"
end

test "666" do
assert RomanNumerals.numeral(666) == "DCLXVI"
end

test "1666" do
assert RomanNumerals.numeral(1666) == "MDCLXVI"
end

test "3001" do
assert RomanNumerals.numeral(3001) == "MMMI"
end

test "3999" do
assert RomanNumerals.numeral(3999) == "MMMCMXCIX"
end
end
2 changes: 2 additions & 0 deletions elixir/roman-numerals/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

0 comments on commit f0d0aa0

Please sign in to comment.