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

Post History

75%
+4 −0
Challenges Single digit Roman numeral

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...

posted 1y ago by Arpad Horvath‭  ·  edited 1y ago by Arpad Horvath‭

Answer
#6: Post edited by user avatar Arpad Horvath‭ · 2023-10-27T14:31:11Z (about 1 year ago)
Thanks
  • # 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 by user avatar Arpad Horvath‭ · 2023-10-27T14:26:02Z (about 1 year ago)
More readable version
  • # 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 by user avatar Arpad Horvath‭ · 2023-10-27T14:10:41Z (about 1 year ago)
Comment
  • # 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 by user avatar Arpad Horvath‭ · 2023-10-27T14:08:03Z (about 1 year ago)
walrus to be able to use lambda
  • # 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 by user avatar Arpad Horvath‭ · 2023-10-26T05:46:53Z (about 1 year ago)
  • # 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))
  • ```
#1: Initial revision by user avatar Arpad Horvath‭ · 2023-10-25T14:29:43Z (about 1 year ago)
# 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))

```