Roman Numeral to integer function [duplicate]
Asked Answered
S

1

7

basically i am trying to create a function that will turn a Roman numeral into a integer.

I have an array:

$roman_numerals=[
    'M'  => 1000,
    'CM' => 900,
    'D'  => 500,
    'CD' => 400,
    'C'  => 100,
    'XC' => 90,
    'L'  => 50,
    'XL' => 40,
    'X'  => 10,
    'IX' => 9,
    'V'  => 5,
    'IV' => 4,
    'I'  => 1
];

I'm fairly new to PHP so i'm still getting used to the way of think so please bear in mind i'm still learning :)

here is my function - or what i have so far:

//Array
function romanToInteger($key)
{
$roman_numerals=[
    'M'  => 1000,
    'CM' => 900,
    'D'  => 500,
    'CD' => 400,
    'C'  => 100,
    'XC' => 90,
    'L'  => 50,
    'XL' => 40,
    'X'  => 10,
    'IX' => 9,
    'V'  => 5,
    'IV' => 4,
    'I'  => 1
];

$roman = intval($key);
$result = 0;

foreach ($roman_numerals as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
var_dump($roman); //test
echo $result;
}

i have been at this for hours and would just like the see the light of it, any advice would be greatly appreciated.

when i run it in the command line with

echo romanToInteger('I');

i just get returned 0 and i think its something to do with my intval?

Sorry again for being a noob, help appreciated though or any pointers! :)

Selfconceit answered 13/5, 2015 at 14:7 Comment(0)
H
4

Yes it has something to do with the intval.

You're basically casting your roman input into an integer rendering it into 0.

Remove that:

function romanToInteger($key)
{
    $romans = [
        'M' => 1000,
        'CM' => 900,
        'D' => 500,
        'CD' => 400,
        'C' => 100,
        'XC' => 90,
        'L' => 50,
        'XL' => 40,
        'X' => 10,
        'IX' => 9,
        'V' => 5,
        'IV' => 4,
        'I' => 1,
    ];

    $roman = $key;
    $result = 0;

    foreach ($romans as $key => $value) {
        while (strpos($roman, $key) === 0) {
            $result += $value;
            $roman = substr($roman, strlen($key));
        }
    }
    echo $result;
}

romanToInteger('IV');

Sample Output

Henrion answered 13/5, 2015 at 14:12 Comment(10)
Thanks! Not too sure why i thought of having it in there now... thanks again i understand it now :)Selfconceit
@Ghost romanToInteger('IV'); that outputs 10 in your updated codepad paste. That should be 4. In Roman methods, the I is substracted from V if the first value is lower than the next. As opposed to VI would be 6.Sundin
@Fred-ii- yeah, that logic was to painful, just use the keyHenrion
Just checking buddy, just in case of... you know what ;-) CheersSundin
@Fred-ii- actually (i think) the OP never mean to create sequences of roman numerals, VIX should literally mix the return values. should be easier if the input was an array.Henrion
@Ghost Hmm... VIX that could pose a problem. It's not a valid Roman numeral number. In order to have what I think you or the OP may think as being 16, would theoretically amount to 6 minus 10, logically that is. We were taught Roman numerals in school, way back when. I doubt they still teach it lolSundin
@Fred-ii- yeah, that is indeed invalid, that should be a negative :D yeah i don't know still no kids yet i don't know if they still teach it :DHenrion
@Ghost I feel that the complexity of this, is far too great in order to get the right sequences done and in proper order. VV is basically X but that could be misinterpreted. Same for XIIX I think you know what I mean ;-) Probably why the system didn't stand the test of time. The Romans didn't foresee a digital age.Sundin
@Fred-ii- by the way, i found this, and its good, i'l find a way and time to convert this in PHP onlineconversion.com/roman_numerals_advanced.htm is on JS, i'll have to transcribe each, check out the inspect element JSHenrion
@Ghost Cool. I tried it with XIIX and gave me an invalid input; smart. If you can pull this off, you are a code God-Guru ;-)Sundin

© 2022 - 2024 — McMap. All rights reserved.