In the hovertext of this xkcd:
There's an "encoding" based on replacing runs of roman numerals with their sums. To do this:
- Find all runs of at least 1 roman numeral (
IVXLCDM
) - Replace them with their sums, as numbers
The Roman Numerals are IVXLCDM
, corresponding respectively to 1, 5, 10, 50, 100, 500, 1000
. You should match them in either case.
For example, if we take encoding
, c
is the roman numeral 100, so it gets replaced with 100, leaving en100oding
. Then, di
are both roman numerals, respectively 500 and 1, so it gets replaced with their sum, 501, leaving en100o501ng
.
Your challenge is to implement this, given a string. This is code-golf, shortest wins!
Note: the xkcd implements some more complex rules regarding consecutive characters like iv
. This can create some ambiguities so I'm going with this simpler version.
Testcases
xkcd -> 10k600
encodingish -> en100o501ng1sh
Hello, World! -> He100o, Wor550!
Potatoes are green -> Potatoes are green
Divide -> 1007e
bacillicidic -> ba904