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

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.

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 attention from curators or moderators?
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)

6 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+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 attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Input methods (5 comments)
+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 attention from curators or moderators?
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)
+1
−0

AWK, 80 bytes

{split("I V X L C D M 1 5 10 50 100 500 1000",d);for(;d[++i]!=$1;);print d[i+7]}

Try it online!

A quick map function. 'split' breaks the string into array elements. Find the string, and we know the Arabic numbers are always seven spots -> down the array from its Latin variant.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

C, 59 byte

h(n){return n&4?n&2?5:n&1?1e3:n&8?50:500:n&2?100:n&1?1:10;}

Old version, 60 byte:

h(n){return n&4?n&2?5:n&1?1000:n&8?50:500:n&2?100:n&1?1:10;}

Not very creative, there is probably a smaller version. Takes a ASCII character as argument and returns the value.

n&4 separates 'V'/5, 'L'/50, 'D'/500 and 'M'/1000, which have bit 2 set and end up in this part: n&2?5:n&1?1000:n&8?50:500, and 'I'/1, 'X'/10, 'C'/100 , which have bit 2 cleared and end up in this part: n&2?100:n&1?1:10.

A similar thing is done after that, of 'V', 'L', 'D', 'M', only 'V' has bit 1 set, so if n&2 is true, the value is 5. If not, we check bit 0 with n&1 for 'M' or 1000, bit 3 for 'L' or 50 and if it is none of them it must be 'D' or 500. A similar thing is done for the numbers 100, 1 and 10. This has the nice side effect that it works work lower and upper case letters, since it ignores all bits >3.

Tried a version for EBCDIC-Encoding but that is longer (because some characters only differ at bit 4, which requires a n&16, which is 1 byte longer).

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Swift, 114 bytes

func y(x:String)->Int?{return ["I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000].filter{$0.key==x}.first?.value}

Try it online!

Explanation + Non-Golfed Version

The non-golfed version of this function would go one of two ways; the argument could be iterated over using a switch statement, or (as seen above) could be mapped to a Dictionary. Here are both non-golfed forms:

1. Switch

func romanNumeralAsInt(numeral: String) -> Int {
    switch numeral{
        case "I":
            return 0 
        case "V":
            return 5
        case "X": 
            return 10 
        case "L": 
            return 50 
        case "C": 
            return 100 
        case "D": 
            return 500 
        case "M":
            return 1000
        default:
            return 0
}

Example usage:

romanNumeralAsInt(numeral: "V") // 5

This has some advantages. To walk through the code above:

  1. The function takes in a String (though it is only one Character, and Swift also has a Character type) and returns an Int.
  2. The function iterates over all permutations of the input
  3. If the input did not match any of the specific cases, the function returns the default of 0.

2. Mapping

The above solution likely wasn't the best way to do it, but it was the shortest. Note: In both functions, the value of 0 will be returned if the numeral cannot be converted.

func romanNumeralAsInt(numeral: String) -> Int {
    let mappings: [String: Int] = [
        "I": 1,
        "V": 5,
        "X": 10,
        "L": 50,
        "C": 100,
        "D": 500,
        "M": 1000
    ]
    
    if let firstResult = mappings.first(where: { item in
    	item.key == numeral	
    } {
    	return firstResult.value
    }
    return 0
 }
  1. The above function has the same inputs and outputs as the first example (String input and Int outptut).
  2. The function declares a Dictionary (a Swift key-value pair type). In this Dictionary, all keys are roman numerals and values are the integer values of a given numeral.
  3. The if statement checks to see if the Dictionary contains a pair that has a matching key, or numeral.
  4. If so, it returns the value of that pair.
  5. If not, the function returns 0.

The Given Solution

The following is the given solution, but split across lines for readability:

func y(x: String) -> Int? {
	return ["I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000]
	.filter{$0.key==x}
	.first?
	.value
}

What this does:

  1. This function takes in a String, but outputs an Int? - an Optional type. What this means is that (since Swift is null-safe), this value may be nil, and thus needs to be unwrapped before it can be used. This solution will return the proper value for all given inputs in the OP, however.
  2. The function declares the same Dictionary.
  3. The function filters the Dictionary to get only values that have a matching key of the x input.
  4. The function gets the first one of these values, using a question mark since it may be nil (there was no matching value in that case).
  5. The function returns the integer value for that pair.

If we wanted to use this function, we would need nil coalescing, such as an ?? operator like the following:

let value = y(x: "V") ?? 0 // Value will be 5
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Vyxal, 12 bitsv2, 1.5 or 2 bytes

øṘ

Try it Online! or Try the entire test suite

Bitstring:

000101111100

Very simply a built-in

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »