roman-numbers
This commit is contained in:
parent
8a0b02049f
commit
26a8976b52
|
@ -0,0 +1,34 @@
|
||||||
|
{
|
||||||
|
"authors": [
|
||||||
|
"rubysolo"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"andrewsardone",
|
||||||
|
"angelikatyborska",
|
||||||
|
"cetinajero",
|
||||||
|
"Cohen-Carlisle",
|
||||||
|
"dalexj",
|
||||||
|
"devonestes",
|
||||||
|
"jinyeow",
|
||||||
|
"lpil",
|
||||||
|
"neenjaw",
|
||||||
|
"parkerl",
|
||||||
|
"sotojuan",
|
||||||
|
"Teapane",
|
||||||
|
"waiting-for-dev"
|
||||||
|
],
|
||||||
|
"files": {
|
||||||
|
"solution": [
|
||||||
|
"lib/roman_numerals.ex"
|
||||||
|
],
|
||||||
|
"test": [
|
||||||
|
"test/roman_numerals_test.exs"
|
||||||
|
],
|
||||||
|
"example": [
|
||||||
|
".meta/example.ex"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"blurb": "Convert modern Arabic numbers into Roman numerals.",
|
||||||
|
"source": "The Roman Numeral Kata",
|
||||||
|
"source_url": "https://codingdojo.org/kata/RomanNumerals/"
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
{"track":"elixir","exercise":"roman-numerals","id":"a78bab796579451fae7b60be21481bff","url":"https://exercism.org/tracks/elixir/exercises/roman-numerals","handle":"negrienko","is_requester":true,"auto_approve":false}
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Used by "mix format"
|
||||||
|
[
|
||||||
|
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
|
||||||
|
]
|
|
@ -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
|
||||||
|
|
|
@ -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.
|
|
@ -0,0 +1,103 @@
|
||||||
|
# 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`.
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Today, most people in the world use Arabic numerals (0–9).
|
||||||
|
But if you travelled back two thousand years, you'd find that most Europeans were using Roman numerals instead.
|
||||||
|
|
||||||
|
To write a Roman numeral we use the following Latin letters, each of which has a value:
|
||||||
|
|
||||||
|
| M | D | C | L | X | V | I |
|
||||||
|
| ---- | --- | --- | --- | --- | --- | --- |
|
||||||
|
| 1000 | 500 | 100 | 50 | 10 | 5 | 1 |
|
||||||
|
|
||||||
|
A Roman numeral is a sequence of these letters, and its value is the sum of the letters' values.
|
||||||
|
For example, `XVIII` has the value 18 (`10 + 5 + 1 + 1 + 1 = 18`).
|
||||||
|
|
||||||
|
There's one rule that makes things trickier though, and that's that **the same letter cannot be used more than three times in succession**.
|
||||||
|
That means that we can't express numbers such as 4 with the seemingly natural `IIII`.
|
||||||
|
Instead, for those numbers, we use a subtraction method between two letters.
|
||||||
|
So we think of `4` not as `1 + 1 + 1 + 1` but instead as `5 - 1`.
|
||||||
|
And slightly confusingly to our modern thinking, we write the smaller number first.
|
||||||
|
This applies only in the following cases: 4 (`IV`), 9 (`IX`), 40 (`XL`), 90 (`XC`), 400 (`CD`) and 900 (`CM`).
|
||||||
|
|
||||||
|
Order matters in Roman numerals!
|
||||||
|
Letters (and the special compounds above) must be ordered by decreasing value from left to right.
|
||||||
|
|
||||||
|
Here are some examples:
|
||||||
|
|
||||||
|
```text
|
||||||
|
105 => CV
|
||||||
|
---- => --
|
||||||
|
100 => C
|
||||||
|
+ 5 => V
|
||||||
|
```
|
||||||
|
|
||||||
|
```text
|
||||||
|
106 => CVI
|
||||||
|
---- => --
|
||||||
|
100 => C
|
||||||
|
+ 5 => V
|
||||||
|
+ 1 => I
|
||||||
|
```
|
||||||
|
|
||||||
|
```text
|
||||||
|
104 => CIV
|
||||||
|
---- => ---
|
||||||
|
100 => C
|
||||||
|
+ 4 => IV
|
||||||
|
```
|
||||||
|
|
||||||
|
And a final more complex example:
|
||||||
|
|
||||||
|
```text
|
||||||
|
1996 => MCMXCVI
|
||||||
|
----- => -------
|
||||||
|
1000 => M
|
||||||
|
+ 900 => CM
|
||||||
|
+ 90 => XC
|
||||||
|
+ 5 => V
|
||||||
|
+ 1 => I
|
||||||
|
```
|
||||||
|
|
||||||
|
## Instructions
|
||||||
|
|
||||||
|
Your task is to convert a number from Arabic numerals to Roman numerals.
|
||||||
|
|
||||||
|
For this exercise, we are only concerned about traditional Roman numerals, in which the largest number is MMMCMXCIX (or 3,999).
|
||||||
|
|
||||||
|
~~~~exercism/note
|
||||||
|
There are lots of different ways to convert between Arabic and Roman numerals.
|
||||||
|
We recommend taking a naive approach first to familiarise yourself with the concept of Roman numerals and then search for more efficient methods.
|
||||||
|
|
||||||
|
Make sure to check out our Deep Dive video at the end to explore the different approaches you can take!
|
||||||
|
~~~~
|
||||||
|
|
||||||
|
## 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/
|
|
@ -0,0 +1,33 @@
|
||||||
|
defmodule RomanNumerals do
|
||||||
|
@doc """
|
||||||
|
Convert the number to a roman number.
|
||||||
|
"""
|
||||||
|
@romans %{
|
||||||
|
1 => ["I", "X", "C", "M"],
|
||||||
|
2 => ["II", "XX", "CC", "MM"],
|
||||||
|
3 => ["III", "XXX", "CCC", "MMM"],
|
||||||
|
4 => ["IV", "XL", "CD"],
|
||||||
|
5 => ["V", "L", "D"],
|
||||||
|
6 => ["VI", "LX", "DC"],
|
||||||
|
7 => ["VII", "LXX", "DCC"],
|
||||||
|
8 => ["VIII", "LXXX", "DCCC"],
|
||||||
|
9 => ["IX", "XC", "CM"]
|
||||||
|
}
|
||||||
|
|
||||||
|
@spec numeral(pos_integer) :: String.t()
|
||||||
|
def numeral(number) do
|
||||||
|
number
|
||||||
|
|> Integer.digits()
|
||||||
|
|> Enum.reverse()
|
||||||
|
|> Enum.with_index(&to_roman_digit/2)
|
||||||
|
|> Enum.reverse()
|
||||||
|
|> Enum.join()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp to_roman_digit(0, _index), do: ""
|
||||||
|
defp to_roman_digit(digit, index) do
|
||||||
|
@romans
|
||||||
|
|> Map.fetch!(digit)
|
||||||
|
|> Enum.at(index)
|
||||||
|
end
|
||||||
|
end
|
|
@ -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
|
|
@ -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 "16" do
|
||||||
|
assert RomanNumerals.numeral(16) == "XVI"
|
||||||
|
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 "66" do
|
||||||
|
assert RomanNumerals.numeral(66) == "LXVI"
|
||||||
|
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 "666" do
|
||||||
|
assert RomanNumerals.numeral(666) == "DCLXVI"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "911" do
|
||||||
|
assert RomanNumerals.numeral(911) == "CMXI"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "1024" do
|
||||||
|
assert RomanNumerals.numeral(1024) == "MXXIV"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "1666" do
|
||||||
|
assert RomanNumerals.numeral(1666) == "MDCLXVI"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "3000" do
|
||||||
|
assert RomanNumerals.numeral(3000) == "MMM"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "3001" do
|
||||||
|
assert RomanNumerals.numeral(3001) == "MMMI"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "3888" do
|
||||||
|
assert RomanNumerals.numeral(3888) == "MMMDCCCLXXXVIII"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "3999" do
|
||||||
|
assert RomanNumerals.numeral(3999) == "MMMCMXCIX"
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,2 @@
|
||||||
|
ExUnit.start()
|
||||||
|
ExUnit.configure(exclude: :pending, trace: true)
|
Loading…
Reference in New Issue