Space
Open
From Fieldwork
Scales
Archive
It's intimidating, but it's simply a mapping exercise. 4s and 9s are simply additional symbols in a bucket.
It's very concatenative. It's very no-brainer. To add, they simply merge them together, sort them, and simplify by the rule of 5.
It's a very good system for when you're in a low-literacy society.
class Solution:
def intToRoman(self, num: int) -> str:
# Define the mapping in descending order
roman_map = [
(1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
(100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
(10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")
]
result = []
for value, symbol in roman_map:
# Check how many times the current symbol fits into num
count = num // value
if count > 0:
result.append(symbol * count)
# Reduce num by the amount we just converted
num %= value
return "".join(result)Practice bench
A private scratchpad for this reading. Nothing is sent or scored.
What is still unclear, or what would change the explanation?
Saved on this device · one draft per mode