Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1
PHP Solution: $romans = array( '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 = 'MMMCMXCIX'; $result = 0; foreach ($romans as $key => $value) { while (strpos($roman, $key) === 0) { $result += $value; $roman = substr($roman, strlen($key)); } } echo $result; Hope it works for you. Source: https://goo.gl/CcIvly
10th Apr 2017, 10:31 PM
Jean-louis du Plessis
Jean-louis du Plessis - avatar