1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-10-29 22:17:23 +01:00

feat(challenges): add roman-numerals

This commit is contained in:
Divlo 2021-06-30 15:03:39 +02:00
parent 57950426d3
commit 213ae08fb3
No known key found for this signature in database
GPG Key ID: 185ED2F15F104E52
90 changed files with 177 additions and 0 deletions

View File

@ -0,0 +1,45 @@
# roman-numerals
Created by [@Divlo](https://github.com/Divlo) on 30 June 2021.
## Instructions
The objective of this challenge is to create a function that translates a number into Roman numerals or the other way around.
We will use the letters `I`, `V`, `X`, `L`, `C`, `D`, `M` to build the Roman numerals.
Here are the rules for building a Roman numeral:
- The numbers `1`, `2` and `3` are written respectively as `I`, `II` and `III`
- The number `5` is written as `V`
- The number `10` is written as `X`
- The number `50` is written as `L`
- The number `100` is written as `C`
- The number `500` is written as `D`
- The number `1000` is written as `M`
- When writing two letters in a row, if the numerical value of the first is greater than the numerical value of the second, their numerical values are added. For example the number `6` is written `VI`. We add `V` (5) + `I` (1) = 6.
- When writing two letters in a row, if the numerical value of the first is less than the numerical value of the second, the value of the first is subtracted from the second. For example the number `4` is written `IV`. We subtract `V` (5) - `I` (1) = 4.
- Subtractions of values are limited to 2 letters only. For example we **cannot** write `8` while doing `IIX`. We must use the addition of letters like this `VIII`.
- Therefore, the first ten numbers are written as `I`, `II`, `III`, `IV`, `V`, `VI`, `VII`, `VIII`, `IX`, `X` . Larger numbers follow the same pattern.
- You can associate as many symbols as you want to write larger numbers, for example:
- `36` is written as `XXXVI`
- `42` is written as `XLII`
- `2448` is written as `MMCDXLVIII`.
| Symbol | I | V | X | L | C | D | M |
|--------|---|---|----|----|-----|-----|------|
| Value | 1 | 5 | 10 | 50 | 100 | 500 | 1000 |
### Input
- **Line 1:** The string : `arabic to roman` or `roman to arabic` to determine how to convert the number
- **Line 2:** The number to convert
## Source
- [Wikipedia - Roman numerals](https://en.wikipedia.org/wiki/Roman_numerals)
- [Wikipedia - Arabic numerals](https://en.wikipedia.org/wiki/Arabic_numerals)
## Examples
See the `test` folder for examples of input/output.

View File

@ -0,0 +1,2 @@
arabic to roman
1

View File

@ -0,0 +1 @@
I

View File

@ -0,0 +1,2 @@
arabic to roman
59

View File

@ -0,0 +1 @@
LIX

View File

@ -0,0 +1,2 @@
arabic to roman
93

View File

@ -0,0 +1 @@
XCIII

View File

@ -0,0 +1,2 @@
arabic to roman
99

View File

@ -0,0 +1 @@
XCIX

View File

@ -0,0 +1,2 @@
arabic to roman
141

View File

@ -0,0 +1 @@
CXLI

View File

@ -0,0 +1,2 @@
arabic to roman
163

View File

@ -0,0 +1 @@
CLXIII

View File

@ -0,0 +1,2 @@
arabic to roman
369

View File

@ -0,0 +1 @@
CCCLXIX

View File

@ -0,0 +1,2 @@
arabic to roman
402

View File

@ -0,0 +1 @@
CDII

View File

@ -0,0 +1,2 @@
arabic to roman
575

View File

@ -0,0 +1 @@
DLXXV

View File

@ -0,0 +1,2 @@
arabic to roman
911

View File

@ -0,0 +1 @@
CMXI

View File

@ -0,0 +1,2 @@
arabic to roman
999

View File

@ -0,0 +1 @@
CMXCIX

View File

@ -0,0 +1,2 @@
arabic to roman
2

View File

@ -0,0 +1 @@
II

View File

@ -0,0 +1,2 @@
arabic to roman
1024

View File

@ -0,0 +1 @@
MXXIV

View File

@ -0,0 +1,2 @@
arabic to roman
2751

View File

@ -0,0 +1 @@
MMDCCLI

View File

@ -0,0 +1,2 @@
arabic to roman
3000

View File

@ -0,0 +1 @@
MMM

View File

@ -0,0 +1,2 @@
roman to arabic
I

View File

@ -0,0 +1 @@
1

View File

@ -0,0 +1,2 @@
roman to arabic
II

View File

@ -0,0 +1 @@
2

View File

@ -0,0 +1,2 @@
roman to arabic
III

View File

@ -0,0 +1 @@
3

View File

@ -0,0 +1,2 @@
roman to arabic
IV

View File

@ -0,0 +1 @@
4

View File

@ -0,0 +1,2 @@
roman to arabic
V

View File

@ -0,0 +1 @@
5

View File

@ -0,0 +1,2 @@
roman to arabic
VI

View File

@ -0,0 +1 @@
6

View File

@ -0,0 +1,2 @@
roman to arabic
IX

View File

@ -0,0 +1 @@
9

View File

@ -0,0 +1,2 @@
arabic to roman
3

View File

@ -0,0 +1 @@
III

View File

@ -0,0 +1,2 @@
roman to arabic
XXVII

View File

@ -0,0 +1 @@
27

View File

@ -0,0 +1,2 @@
roman to arabic
XLVIII

View File

@ -0,0 +1 @@
48

View File

@ -0,0 +1,2 @@
roman to arabic
LIX

View File

@ -0,0 +1 @@
59

View File

@ -0,0 +1,2 @@
roman to arabic
XCIII

View File

@ -0,0 +1 @@
93

View File

@ -0,0 +1,2 @@
roman to arabic
XCIX

View File

@ -0,0 +1 @@
99

View File

@ -0,0 +1,2 @@
roman to arabic
CXLI

View File

@ -0,0 +1 @@
141

View File

@ -0,0 +1,2 @@
roman to arabic
CLXIII

View File

@ -0,0 +1 @@
163

View File

@ -0,0 +1,2 @@
roman to arabic
CCCLXIX

View File

@ -0,0 +1 @@
369

View File

@ -0,0 +1,2 @@
roman to arabic
CDII

View File

@ -0,0 +1 @@
402

View File

@ -0,0 +1,2 @@
roman to arabic
DLXXV

View File

@ -0,0 +1 @@
575

View File

@ -0,0 +1,2 @@
arabic to roman
4

View File

@ -0,0 +1 @@
IV

View File

@ -0,0 +1,2 @@
roman to arabic
CMXI

View File

@ -0,0 +1 @@
911

View File

@ -0,0 +1,2 @@
roman to arabic
CMXCIX

View File

@ -0,0 +1 @@
999

View File

@ -0,0 +1,2 @@
roman to arabic
MXXIV

View File

@ -0,0 +1 @@
1024

View File

@ -0,0 +1,2 @@
roman to arabic
MMDCCLI

View File

@ -0,0 +1 @@
2751

View File

@ -0,0 +1,2 @@
roman to arabic
MMM

View File

@ -0,0 +1 @@
3000

View File

@ -0,0 +1,2 @@
arabic to roman
5

View File

@ -0,0 +1 @@
V

View File

@ -0,0 +1,2 @@
arabic to roman
6

View File

@ -0,0 +1 @@
VI

View File

@ -0,0 +1,2 @@
arabic to roman
9

View File

@ -0,0 +1 @@
IX

View File

@ -0,0 +1,2 @@
arabic to roman
27

View File

@ -0,0 +1 @@
XXVII

View File

@ -0,0 +1,2 @@
arabic to roman
48

View File

@ -0,0 +1 @@
XLVIII