Avatar of 4d47
0
0
Genius
0
0

4d47's solution

to Roman Numerals in the Perl 6 Track

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

Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension .t) will attempt to run routines from the module (a file with the extension .pm6). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command perl6 --doc *.t (* being the name of the test suite), and run the test suite for the exercise by executing the command prove . --exec=perl6 in the exercise directory. You can also add the -v flag e.g. prove . --exec=perl6 -v to display all tests, including any optional tests marked as 'TODO'.

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.t

#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib $?FILE.IO.dirname;
use RomanNumerals;
plan 19;

my Version:D $version = v3;

if RomanNumerals.^ver !~~ $version {
  warn "\nExercise version mismatch. Further tests may fail!"
    ~ "\nRomanNumerals is {RomanNumerals.^ver.gist}. "
    ~ "Test is {$version.gist}.\n";
}

my $c-data = from-json $=pod.pop.contents;
for $c-data<cases>.values {
  is to-roman(.<input><number>), |.<expected description>;
}

=head2 Canonical Data
=begin code
{
  "exercise": "roman-numerals",
  "version": "1.2.0",
  "cases": [
    {
      "description": "1 is a single I",
      "property": "roman",
      "input": {
        "number": 1
      },
      "expected": "I"
    },
    {
      "description": "2 is two I's",
      "property": "roman",
      "input": {
        "number": 2
      },
      "expected": "II"
    },
    {
      "description": "3 is three I's",
      "property": "roman",
      "input": {
        "number": 3
      },
      "expected": "III"
    },
    {
      "description": "4, being 5 - 1, is IV",
      "property": "roman",
      "input": {
        "number": 4
      },
      "expected": "IV"
    },
    {
      "description": "5 is a single V",
      "property": "roman",
      "input": {
        "number": 5
      },
      "expected": "V"
    },
    {
      "description": "6, being 5 + 1, is VI",
      "property": "roman",
      "input": {
        "number": 6
      },
      "expected": "VI"
    },
    {
      "description": "9, being 10 - 1, is IX",
      "property": "roman",
      "input": {
        "number": 9
      },
      "expected": "IX"
    },
    {
      "description": "20 is two X's",
      "property": "roman",
      "input": {
        "number": 27
      },
      "expected": "XXVII"
    },
    {
      "description": "48 is not 50 - 2 but rather 40 + 8",
      "property": "roman",
      "input": {
        "number": 48
      },
      "expected": "XLVIII"
    },
    {
      "description": "49 is not 40 + 5 + 4 but rather 50 - 10 + 10 - 1",
      "property": "roman",
      "input": {
        "number": 49
      },
      "expected": "XLIX"
    },
    {
      "description": "50 is a single L",
      "property": "roman",
      "input": {
        "number": 59
      },
      "expected": "LIX"
    },
    {
      "description": "90, being 100 - 10, is XC",
      "property": "roman",
      "input": {
        "number": 93
      },
      "expected": "XCIII"
    },
    {
      "description": "100 is a single C",
      "property": "roman",
      "input": {
        "number": 141
      },
      "expected": "CXLI"
    },
    {
      "description": "60, being 50 + 10, is LX",
      "property": "roman",
      "input": {
        "number": 163
      },
      "expected": "CLXIII"
    },
    {
      "description": "400, being 500 - 100, is CD",
      "property": "roman",
      "input": {
        "number": 402
      },
      "expected": "CDII"
    },
    {
      "description": "500 is a single D",
      "property": "roman",
      "input": {
        "number": 575
      },
      "expected": "DLXXV"
    },
    {
      "description": "900, being 1000 - 100, is CM",
      "property": "roman",
      "input": {
        "number": 911
      },
      "expected": "CMXI"
    },
    {
      "description": "1000 is a single M",
      "property": "roman",
      "input": {
        "number": 1024
      },
      "expected": "MXXIV"
    },
    {
      "description": "3000 is three M's",
      "property": "roman",
      "input": {
        "number": 3000
      },
      "expected": "MMM"
    }
  ]
}
=end code
unit module RomanNumerals:ver<3>;

sub to-roman ($number is copy --> Str:D) is export {
    my @glyphs = (
        :1000M,
        :900CM,
        :500D,
        :400CD,
        :100C,
        :90XC,
        :50L,
        :40XL,
        :10X,
        :9IX,
        :5V,
        :4IV,
        :1I,
    );
    my $roman = '';
    for @glyphs -> $p {
        while $number >= $p.value {
            $roman ~= $p.key;
            $number -= $p.value;
        }
    }
    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?