Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on Single digit Roman numeral

Parent

Single digit Roman numeral

+3
−0

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.
  • It is acceptable to have a trailing decimal point . or .0 for some or all of the outputs.

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.

History

2 comment threads

Allowed output formats? (4 comments)
Similar challenge (1 comment)
Post
+1
−0

Python 3.8+, 49 byte

lambda c:5**((i:="IVXLCDM".find(c))&1)*10**(i>>1)

Testing:

f=lambda c:5**((i:="IVXLCDM".find(c))&1)*10**(i>>1)
print(*map(f,"IVXLCDM"))

52 bytes

My first attempt. Really simple and straight forward. If the rules would allow 1000.0 as result for M then it could be written as 1e3, making this solution one byte smaller.

lambda n:[1,5,10,50,100,500,1000]["IVXLCDM".find(n)]
History

1 comment thread

Allowing a trailing decimal point (1 comment)
Allowing a trailing decimal point
trichoplax‭ wrote 6 months ago

Even though your latest version doesn't require this, I've edited the Output section to explicitly allow a trailing . or .0 in some or all of the outputs (since this was never forbidden).