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

+2
−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.

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
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

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

Python 3.8+, 51 byte

lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)

Testing the code:

f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)

for s in "IVXLCDM":
    print(s, f(s))

The walrus operator stores the index in the variable i, that is used in the exponent.

A much more readable version with the same logic:

def f(n):
    i = "IVXLCDM".index(n)
    return (i%2*4+1) * 10 ** (i // 2)

Thanks for [Object object]‭ for getting rid of 5 bytes.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

3 comment threads

Input methods (7 comments)
Remove one () pair (1 comment)
The ==1 is not needed (1 comment)
Input methods
trichoplax‭ wrote 6 months ago

Welcome to Code Golf Codidact!

We have a guide to acceptable input and output methods on Meta, and this currently only specifies that input may be taken by direct insertion into the source code for languages that have no other option.

For languages that can take input, it can be taken by any of STDIN, command line arguments, or function arguments. For example, in Python this is often as the argument to a lambda function.

Currently this answer will not be valid until n is taken as an input, but if you think the rule should be changed then anyone is free to post a new rule under that Meta post, to see if the community votes to support it.

Arpad Horvath‭ wrote 6 months ago · edited 6 months ago

I've changed both. I made lambda functions from both expressions. Is this enough?

trichoplax‭ wrote 6 months ago

I haven't tested the Haskell code, but there's a problem with the Python code that I'll explain below.

In Python, a lambda function cannot have multiple statements. If you try to run the current code, it will give an error. I can think of 2 different approaches to avoid this problem:

  • Use a lambda function with only a single statement
  • Use a def function to allow multiple statements

I'll put examples of these 2 approaches in the next 2 comments for comparison.

trichoplax‭ wrote 6 months ago
lambda n:(4*("IVXLCDM".index(n)%2==1)+1)*10**("IVXLCDM".index(n)//2)

This can be tested as follows:

f=lambda n:(4*("IVXLCDM".index(n)%2==1)+1)*10**("IVXLCDM".index(n)//2)

for c in "IVXLCDM":
    print(c, f(c))
trichoplax‭ wrote 6 months ago · edited 6 months ago
def f(n):i="IVXLCDM".index(n);return(4*(i%2==1)+1)*10**(i//2)

This can be tested as follows:

def f(n):i="IVXLCDM".index(n);return(4*(i%2==1)+1)*10**(i//2)

for c in "IVXLCDM":
    print(c, f(c))    
trichoplax‭ wrote 6 months ago

To summarise the difference between def and lambda in Python, the following give equivalent results:

def double(n):
    return n * 2
double = lambda n: n * 2
Arpad Horvath‭ wrote 6 months ago

The problem is fixed, and thanks to the walrus operator I can still use the lambda function. And it is even shorter than the wrong version.