Avatar of ljwoodward

ljwoodward's solution

to Roman Numerals in the PL/SQL Track

Published at Sep 04 2019 · 0 comments
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

Setup

Go through the setup instructions for PL/SQL to get ready to code:

http://exercism.io/languages/plsql

Running the Tests

Execute the tests by calling the run method in the respective ut_<exercise># package. The necessary code should be contained at the end of the test package. As an example, the test for the hamming exercise would be run using

begin
  ut_hamming#.run;
end;
/

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.

ut_numeral#.plsql

create or replace package ut_numeral#
is
  procedure run;
end ut_numeral#;
/
 
create or replace package body ut_numeral#
is
  procedure test (
    i_descn                                       varchar2
   ,i_exp                                         varchar2
   ,i_act                                         varchar2
  )
  is
  begin
    if i_exp = i_act then
      dbms_output.put_line('SUCCESS: ' || i_descn);
    else
      dbms_output.put_line('FAILURE: ' || i_descn || ' - expected ' || nvl('' || i_exp, 'null') || ', but received ' || nvl('' || i_act, 'null'));
    end if;
  end test;
 
  procedure run
  is
  begin
    test(i_descn => 'test_1',    i_exp => 'I',      i_act => numeral#.to_roman(1   ));
    test(i_descn => 'test_2',    i_exp => 'II',     i_act => numeral#.to_roman(2   ));
    test(i_descn => 'test_3',    i_exp => 'III',    i_act => numeral#.to_roman(3   ));
    test(i_descn => 'test_4',    i_exp => 'IV',     i_act => numeral#.to_roman(4   ));
    test(i_descn => 'test_5',    i_exp => 'V',      i_act => numeral#.to_roman(5   ));
    test(i_descn => 'test_6',    i_exp => 'VI',     i_act => numeral#.to_roman(6   ));
    test(i_descn => 'test_9',    i_exp => 'IX',     i_act => numeral#.to_roman(9   ));
    test(i_descn => 'test_27',   i_exp => 'XXVII',  i_act => numeral#.to_roman(27  ));
    test(i_descn => 'test_48',   i_exp => 'XLVIII', i_act => numeral#.to_roman(48  ));
    test(i_descn => 'test_59',   i_exp => 'LIX',    i_act => numeral#.to_roman(59  ));
    test(i_descn => 'test_93',   i_exp => 'XCIII',  i_act => numeral#.to_roman(93  ));
    test(i_descn => 'test_141',  i_exp => 'CXLI',   i_act => numeral#.to_roman(141 ));
    test(i_descn => 'test_163',  i_exp => 'CLXIII', i_act => numeral#.to_roman(163 ));
    test(i_descn => 'test_402',  i_exp => 'CDII',   i_act => numeral#.to_roman(402 ));
    test(i_descn => 'test_575',  i_exp => 'DLXXV',  i_act => numeral#.to_roman(575 ));
    test(i_descn => 'test_911',  i_exp => 'CMXI',   i_act => numeral#.to_roman(911 ));
    test(i_descn => 'test_1024', i_exp => 'MXXIV',  i_act => numeral#.to_roman(1024));
    test(i_descn => 'test_3000', i_exp => 'MMM',    i_act => numeral#.to_roman(3000));
  end run;
end ut_numeral#;
/
 
begin
  ut_numeral#.run;
end;
/
CREATE OR REPLACE PACKAGE numeral# AS
    FUNCTION to_roman (p_n NUMBER) RETURN  VARCHAR2;
    FUNCTION dig_to_num (p_dig NUMBER, p_group VARCHAR2) RETURN VARCHAR2;
END numeral#;
/

CREATE OR REPLACE PACKAGE BODY numeral# AS
    FUNCTION to_roman (p_n NUMBER) RETURN  VARCHAR2
    IS
      v_ones        NUMBER;
      v_tens        NUMBER;
      v_hundreds    NUMBER;
      v_thousands   NUMBER;
      v_numerals    VARCHAR2(100) := '';
      over_4000     EXCEPTION;
    BEGIN
      v_ones := mod(p_n, 10);
      v_tens := mod(p_n - v_ones, 100);
      v_hundreds := mod(p_n - v_ones - v_tens, 1000);
      v_thousands := mod(p_n - v_ones - v_tens - v_hundreds, 10000);
      v_tens := v_tens / 10;
      v_hundreds := v_hundreds / 100;
      v_thousands := v_thousands / 1000;

      -- Thousands
      IF v_thousands > 3 THEN
        RAISE over_4000;
      ELSIF v_thousands > 0 THEN
      -- Don't need to go above 3,000
        FOR i IN 1..v_thousands LOOP
          v_numerals := v_numerals ||'M';
        END LOOP;
      END IF;

      v_numerals := v_numerals || dig_to_num(v_hundreds, 'H') || dig_to_num(v_tens, 'T') || dig_to_num(v_ones, 'O');

      RETURN v_numerals;
    EXCEPTION
      WHEN over_4000 THEN
        dbms_output.put_line('Number too high, please use a number under 4000');
        RETURN NULL;
    END to_roman;

    FUNCTION dig_to_num (p_dig NUMBER, p_group VARCHAR2) RETURN VARCHAR2
    IS
      v_dig NUMBER := p_dig;
      v_one CHAR;
      v_five CHAR;
      v_ten CHAR;
      v_num VARCHAR2(20) := '';
      wrong_group EXCEPTION;
    BEGIN
      CASE p_group
        WHEN 'O' THEN
          v_one := 'I';
          v_five := 'V';
          v_ten := 'X';
        WHEN 'T' THEN
          v_one := 'X';
          v_five := 'L';
          v_ten := 'C';
        WHEN 'H' THEN
          v_one := 'C';
          v_five := 'D';
          v_ten := 'M';
        ELSE
          v_num := '?';
          RAISE wrong_group;
      END CASE;

      CASE
        WHEN p_dig =  9 THEN
          v_num := v_num || v_one || v_ten;
        WHEN p_dig > 4 THEN
          v_num := v_num || v_five;
          v_dig := p_dig - 5;
          FOR i IN 1..v_dig LOOP
            v_num := v_num || v_one;
          END LOOP;
        WHEN p_dig = 4 THEN
          v_num := v_num || v_one || v_five;
        WHEN p_dig < 4 THEN
          FOR i IN 1..p_dig LOOP
              v_num := v_num || v_one;
          END LOOP;
        END CASE;

      RETURN v_num;
    EXCEPTION
      WHEN wrong_group THEN
        dbms_output.put_line('Wrong Group name given. Use ''O'' for Ones, ''T''' ||
          'for Tens, or ''H'' for Hundreds.');
        RETURN NULL;
    END dig_to_num;
END numeral#;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned 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 you could read more about to improve your understanding?