Comments on Single digit Roman numeral
Parent
Single digit Roman numeral
Given a single character, which is a valid Roman numeral, output its value.
Values
There are 7 valid single character Roman numerals, with the following values:
Character | Value |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Input
- A single character, which will always be one of
IVXLCDM
.
Output
- The corresponding value.
- The output value must not be a Roman numeral.
Test cases
As there are only 7 valid inputs, the list of test cases is exhaustive.
Test cases are in the format "input" : output
.
"I" : 1
"V" : 5
"X" : 10
"L" : 50
"C" : 100
"D" : 500
"M" : 1000
Scoring
This is a code golf challenge. Your score is the number of bytes in your code.
Explanations are optional, but I'm more likely to upvote answers that have one.
Python 3.8+, 51 byte ```pyt …
1y ago
[AWK], 80 bytes {split( …
6mo ago
[Swift], 114 bytes …
1y ago
C, 59 byte ```c h(n){retur …
1y ago
Haskell, 62 bytes ``` (\n- …
1y ago
Vyxal, 12 bitsv2, 1.5 or 2 byt …
1y ago
Post
Haskell, 62 bytes
(\n->(scanl(*)1$cycle[5,2])!!(length$takeWhile(/=n)"IVXLCDM"))
No import needed. It just uses standard Prelude functions.
scanl(*)1$cycle[5,2]
will give you the infinite list of [1,5,10,50...]. With
length$takeWhile(/=n)"IVXLCDM"
you will get the index of the element you would like to look up.
If you set
n='V'
the length part will be 1, and the index 1 means the 2nd element, so you'll get 5.
2 comment threads