Avatar of 4d47
0
0
Genius
0
1

4d47's solution

to Roman Numerals in the PHP Track

0
0
Genius
0
1
Instructions
Test suite
Solution

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

 1  => I
10  => X
 7  => VII

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

See also: http://www.novaroma.org/via_romana/numbers.html

Running the tests

  1. Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php. To find the Exercism workspace run

     % exercism debug | grep Workspace
    
  2. Get PHPUnit if you don't have it already.

     % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
     % chmod +x phpunit.phar
    
  3. Execute the tests:

     % ./phpunit.phar roman-numerals/roman-numerals_test.php
    

Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

roman-numerals_test.php

<?php

require_once "roman-numerals.php";

class RomanTest extends PHPUnit\Framework\TestCase
{
    public function test1()
    {
        $this->assertSame('I', toRoman(1));
    }

    public function test2()
    {
        $this->markTestSkipped();
        $this->assertSame('II', toRoman(2));
    }

    public function test3()
    {
        $this->markTestSkipped();
        $this->assertSame('III', toRoman(3));
    }

    public function test4()
    {
        $this->markTestSkipped();
        $this->assertSame('IV', toRoman(4));
    }

    public function test5()
    {
        $this->markTestSkipped();
        $this->assertSame('V', toRoman(5));
    }

    public function test6()
    {
        $this->markTestSkipped();
        $this->assertSame('VI', toRoman(6));
    }

    public function test9()
    {
        $this->markTestSkipped();
        $this->assertSame('IX', toRoman(9));
    }

    public function test27()
    {
        $this->markTestSkipped();
        $this->assertSame('XXVII', toRoman(27));
    }

    public function test48()
    {
        $this->markTestSkipped();
        $this->assertSame('XLVIII', toRoman(48));
    }

    public function test49()
    {
        $this->markTestSkipped();
        $this->assertSame('XLIX', toRoman(49));
    }

    public function test59()
    {
        $this->markTestSkipped();
        $this->assertSame('LIX', toRoman(59));
    }

    public function test93()
    {
        $this->markTestSkipped();
        $this->assertSame('XCIII', toRoman(93));
    }

    public function test141()
    {
        $this->markTestSkipped();
        $this->assertSame('CXLI', toRoman(141));
    }

    public function test163()
    {
        $this->markTestSkipped();
        $this->assertSame('CLXIII', toRoman(163));
    }

    public function test402()
    {
        $this->markTestSkipped();
        $this->assertSame('CDII', toRoman(402));
    }

    public function test575()
    {
        $this->markTestSkipped();
        $this->assertSame('DLXXV', toRoman(575));
    }

    public function test911()
    {
        $this->markTestSkipped();
        $this->assertSame('CMXI', toRoman(911));
    }

    public function test1024()
    {
        $this->markTestSkipped();
        $this->assertSame('MXXIV', toRoman(1024));
    }

    public function test1998()
    {
        $this->markTestSkipped();
        $this->assertSame('MCMXCVIII', toRoman(1998));
    }

    public function test2999()
    {
        $this->markTestSkipped();
        $this->assertSame('MMCMXCIX', toRoman(2999));
    }

    public function test3000()
    {
        $this->markTestSkipped();
        $this->assertSame('MMM', toRoman(3000));
    }
}
<?php

function toRoman($num)
{
    if ($num <= 0)
        throw new InvalidArgumentException('Number must be positive');
    if ($num >= 5000)
        throw new InvalidArgumentException('Large roman numbers not supported');

    static $glyphs = [
         '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 = '';
    foreach  ($glyphs as $glyph => $limit) {
        while ($num >= $limit) {
            $roman .= $glyph;
            $num -= $limit;
        }
    }
    return $roman;
}

What can you learn from this solution?

A huge amount can be learnt from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that I could read more about to develop my understanding?

Community comments

See what others have said about this solution
about 2 years ago
4d47 says

Stealing @cyb3r solution because ya know, sometimes copy/paste produce so much better ;)