Post History
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)) T...
Answer
#6: Post edited
- # Python 3.8+, 51 byte
- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
- The [walrus operator](https://realpython.com/python-walrus-operator/) stores the index in the variable `i`, that is used in the exponent.
- A much more readable version with the same logic:
- ```python
- def f(n):
- i = "IVXLCDM".index(n)
- return (i%2*4+1) * 10 ** (i // 2)
```
- # Python 3.8+, 51 byte
- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
- The [walrus operator](https://realpython.com/python-walrus-operator/) stores the index in the variable `i`, that is used in the exponent.
- A much more readable version with the same logic:
- ```python
- 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.
#5: Post edited
- # Python 3.8+, 51 byte
- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
The [walrus operator](https://realpython.com/python-walrus-operator/) stores the index in the variable `i`, that is used in the exponent.
- # Python 3.8+, 51 byte
- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
- The [walrus operator](https://realpython.com/python-walrus-operator/) stores the index in the variable `i`, that is used in the exponent.
- A much more readable version with the same logic:
- ```python
- def f(n):
- i = "IVXLCDM".index(n)
- return (i%2*4+1) * 10 ** (i // 2)
- ```
#4: Post edited
# Python 3.8+, 62 bytes, 51 byte- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
- # Python 3.8+, 51 byte
- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
- The [walrus operator](https://realpython.com/python-walrus-operator/) stores the index in the variable `i`, that is used in the exponent.
#3: Post edited
# Python, 62 bytes- ```python
lambda n:i="IVXLCDM".index(n);print((4*(i%2==1)+1)*10**(i//2))- ```
- Testing the code:
- ```python
for n in "IVXLCDM":i = "IVXLCDM".index(n)print(n, (4*(i%2==1)+1) * 10**(i//2))- ```
- # Python 3.8+, 62 bytes, 51 byte
- ```python
- lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- ```
- Testing the code:
- ```python
- f=lambda n:((i:="IVXLCDM".index(n))%2*4+1)*10**(i//2)
- for s in "IVXLCDM":
- print(s, f(s))
- ```
#2: Post edited
# Python, 53 bytes- ```python
i="IVXLCDM".index(n);print((4*(i%2==1)+1)*10**(i//2))- ```
- Testing the code:
- ```python
- for n in "IVXLCDM":
- i = "IVXLCDM".index(n)
- print(n, (4*(i%2==1)+1) * 10**(i//2))
- ```
- # Python, 62 bytes
- ```python
- lambda n:i="IVXLCDM".index(n);print((4*(i%2==1)+1)*10**(i//2))
- ```
- Testing the code:
- ```python
- for n in "IVXLCDM":
- i = "IVXLCDM".index(n)
- print(n, (4*(i%2==1)+1) * 10**(i//2))
- ```