Skip to content

Instantly share code, notes, and snippets.

@acirtautas
Created May 2, 2014 07:34
Show Gist options
  • Save acirtautas/11469377 to your computer and use it in GitHub Desktop.
Save acirtautas/11469377 to your computer and use it in GitHub Desktop.
Roman numerals

Roman Numerals

Installation

Download composer.phar from https://getcomposer.org/

curl -sS https://getcomposer.org/installer | php

Install dependencies

php composer.phar install

Usage

Run unit tests

vendor/bin/phpunit RomanNumeralsTest.php
{
"require-dev": {
"phpunit/phpunit": "4.*"
},
"autoload": {
"files": ["RomanNumerals.php"]
}
}
<?php
class RomanNumerals
{
public function arabicToRoman($arabicNumber)
{
$roman = '';
$chars = [
'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,
];
foreach ($chars as $c => $n) {
while ($arabicNumber >= $n) {
$roman .= $c;
$arabicNumber -= $n;
}
}
return $roman;
}
}
<?php
// Composer genereated autoloader
require __DIR__.'/vendor/autoload.php';
class RomanNumeralsTest extends PHPUnit_Framework_TestCase
{
public function testDataProvider()
{
return array(
array(1, 'I'),
array(2, 'II'),
array(3, 'III'),
array(4, 'IV'),
array(5, 'V'),
array(6, 'VI'),
array(7, 'VII'),
array(8, 'VIII'),
array(9, 'IX'),
array(10, 'X'),
array(12, 'XII'),
array(66, 'LXVI'),
array(1970, 'MCMLXX'),
array(2000, 'MM'),
);
}
/**
* @dataProvider testDataProvider
*/
public function testArabicToRoman1($number, $result)
{
$Roman = new RomanNumerals();
$this->assertEquals($result, $Roman->arabicToRoman($number));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment