3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace JoshDiFabio; /** * Note that I have included the interface and both classes in a single file purely to simplify the * review process. Ordinarily I would use a separate file for each class according to PSR-0 and use * an autoloader. * * Requires PHP >= 5.3.0 */ interface RomanNumeralGeneratorInterface { /** * @param int $number * @return string * @throws \InvalidArgumentException Argument not an integer or out of range * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public function generate($number); } /** * Class defining basic rules of roman numerals */ class RomanNumeral { const ONE = 'I'; const FOUR = 'IV'; const FIVE = 'V'; const NINE = 'IX'; const TEN = 'X'; const FOURTY = 'XL'; const FIFTY = 'L'; const NINETY = 'XC'; const ONE_HUNDRED = 'C'; const FOUR_HUNDRED = 'CD'; const FIVE_HUNDRED = 'D'; const NINE_HUNDRED = 'CM'; const ONE_THOUSAND = 'M'; /** * @return array * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public static function getIntegerToNumeralMap() { return array( 1 => RomanNumeral::ONE, 4 => RomanNumeral::FOUR, 5 => RomanNumeral::FIVE, 9 => RomanNumeral::NINE, 10 => RomanNumeral::TEN, 40 => RomanNumeral::FOURTY, 50 => RomanNumeral::FIFTY, 90 => RomanNumeral::NINETY, 100 => RomanNumeral::ONE_HUNDRED, 400 => RomanNumeral::FOUR_HUNDRED, 500 => RomanNumeral::FIVE_HUNDRED, 900 => RomanNumeral::NINE_HUNDRED, 1000 => RomanNumeral::ONE_THOUSAND, ); } } /** * Class for generating roman numeral strings based on integers */ class RomanNumeralGenerator implements RomanNumeralGeneratorInterface { /** * @param int $inputNumber * @return string * @throws \InvalidArgumentException Argument not an integer or out of range * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public function generate($inputNumber) { if (!is_integer($inputNumber)) { throw new \InvalidArgumentException('Provided argument is not an integer.'); } if (1 > $inputNumber || 3999 < $inputNumber) { throw new \InvalidArgumentException( 'The provided number is not within the allowed range of 1 to 3999.'); } $numeralString = ''; // get integer to numeral map and sort from largest to smallest $integerToNumeralMap = RomanNumeral::getIntegerToNumeralMap(); krsort($integerToNumeralMap); while (0 < $inputNumber) { foreach ($integerToNumeralMap as $_numeralAsInteger => $_numeral) { /* * if input number minus any already selected numerals is larger than the current * numeral, select the current numeral and deduct its integer value from the input * number */ if ($inputNumber >= $_numeralAsInteger) { $numeralString .= $_numeral; $inputNumber -= $_numeralAsInteger; break; } } } return $numeralString; } } $generator = new RomanNumeralGenerator; echo $generator->generate(3999);
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
MMMCMXCIX
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/6E36I on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/6E36I on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/6E36I on line 2
Process exited with code 255.

preferences:
300.34 ms | 401 KiB | 460 Q