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
+1
−0

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.

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

1 comment thread

Input methods (5 comments)
Input methods
trichoplax‭ wrote 6 months ago

The same applies to this answer as I mentioned on your Python answer. It just needs an input method to make it valid.

trichoplax‭ wrote 6 months ago

I don't know Haskell, but I tested your latest code using the following:

main = do
    let result = map(\n->(scanl(*)1$cycle[5,2])!!(length$takeWhile(/=n)"IVXLCDM")) ['I', 'V', 'X', 'L', 'C', 'D', 'M']
    print result

This gives the correct results for all inputs:

[1,5,10,50,100,500,1000]

I don't see any reason that the enclosing parentheses would be necessary, so I believe the following would be a valid answer, saving 2 bytes:

\n->(scanl(*)1$cycle[5,2])!!(length$takeWhile(/=n)"IVXLCDM")
Arpad Horvath‭ wrote 6 months ago

I can do

f=(\n->(scanl(*)1$cycle[5,2])!!(length$takeWhile(/=n)"IVXLCDM"))

but without the parenthesis it failes. You use the parenthesis after the map, and Haskell don't need a parenthesis after the function name just to call the function. It is needed to show what exactly is the first argument and what part is the second. So I think we need them.

trichoplax‭ wrote 6 months ago

I see what you mean. I tried the map without the parentheses and it fails, and I tried omitting the map and just applying to a single argument 'I' and again it only works with the parentheses. Sorry for the false hope...

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

Why should you be sorry? Somebody reads my code in a language not known for him, trying to understand it. And even you found out how to test it.