dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
2.4 KiB
2.4 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 68adce01c0e1144d0a90295c | Challenge 28: Roman Numeral Parser | 29 | challenge-28 |
--description--
Given a string representing a Roman numeral, return its integer value.
Roman numerals consist of the following symbols and values:
| Symbol | Value |
|---|---|
| I | 1 |
| V | 5 |
| X | 10 |
| L | 50 |
| C | 100 |
| D | 500 |
| M | 1000 |
- Numerals are read left to right. If a smaller numeral appears before a larger one, the value is subtracted. Otherwise, values are added.
--hints--
parse_roman_numeral("III") should return 3.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("III"), 3)`)
}})
parse_roman_numeral("IV") should return 4.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("IV"), 4)`)
}})
parse_roman_numeral("XXVI") should return 26.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("XXVI"), 26)`)
}})
parse_roman_numeral("XCIX") should return 99.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("XCIX"), 99)`)
}})
parse_roman_numeral("CDLX") should return 460.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("CDLX"), 460)`)
}})
parse_roman_numeral("DIV") should return 504.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("DIV"), 504)`)
}})
parse_roman_numeral("MMXXV") should return 2025.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_roman_numeral("MMXXV"), 2025)`)
}})
--seed--
--seed-contents--
def parse_roman_numeral(numeral):
return numeral
--solutions--
def parse_roman_numeral(numeral):
roman_map = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
}
total = 0
for i in range(len(numeral)):
current = roman_map[numeral[i]]
next_val = roman_map[numeral[i + 1]] if i + 1 < len(numeral) else 0
if current < next_val:
total -= current
else:
total += current
return total