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! :)

psmears
  • 26,070
  • 4
  • 40
  • 48
Kolvin
  • 185
  • 2
  • 16

1 Answers1

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

Kevin
  • 41,694
  • 12
  • 53
  • 70
  • Thanks! Not too sure why i thought of having it in there now... thanks again i understand it now :) – Kolvin May 13 '15 at 14:24
  • @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. – Funk Forty Niner May 13 '15 at 14:57
  • @Fred-ii- yeah, that logic was to painful, just use the key – Kevin May 13 '15 at 15:04
  • Just checking buddy, just in case of... you know what ;-) *Cheers* – Funk Forty Niner May 13 '15 at 15:05
  • @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. – Kevin May 13 '15 at 15:06
  • @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 lol – Funk Forty Niner May 13 '15 at 15:17
  • @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 :D – Kevin May 13 '15 at 15:24
  • 1
    @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. – Funk Forty Niner May 13 '15 at 15:26
  • @Fred-ii- by the way, i found this, and its good, i'l find a way and time to convert this in PHP http://www.onlineconversion.com/roman_numerals_advanced.htm is on JS, i'll have to transcribe each, check out the inspect element JS – Kevin May 13 '15 at 15:30
  • @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 ;-) – Funk Forty Niner May 13 '15 at 15:31