How to resolve the algorithm Roman numerals/Decode step by step in the ECL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Roman numerals/Decode step by step in the ECL programming language

Table of Contents

Problem Statement

Create a function that takes a Roman numeral as its argument and returns its value as a numeric decimal integer. You don't need to validate the form of the Roman numeral. Modern Roman numerals are written by expressing each decimal digit of the number to be encoded separately, starting with the leftmost decimal digit and skipping any 0s   (zeroes). 1990 is rendered as   MCMXC     (1000 = M,   900 = CM,   90 = XC)     and 2008 is rendered as   MMVIII       (2000 = MM,   8 = VIII). The Roman numeral for 1666,   MDCLXVI,   uses each letter in descending order.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Roman numerals/Decode step by step in the ECL programming language

Source code in the ecl programming language

MapChar(STRING1 c) := CASE(c,'M'=>1000,'D'=>500,'C'=>100,'L'=>50,'X'=>10,'V'=>5,'I'=>1,0);

RomanDecode(STRING s) := FUNCTION
  dsS := DATASET([{s}],{STRING Inp});
  R := { INTEGER2 i; };

  R Trans1(dsS le,INTEGER pos) := TRANSFORM
    SELF.i := MapChar(le.Inp[pos]) * IF ( MapChar(le.Inp[pos]) < MapChar(le.Inp[pos+1]), -1, 1 );
  END;
                
  RETURN SUM(NORMALIZE(dsS,LENGTH(TRIM(s)),Trans1(LEFT,COUNTER)),i);
END;

RomanDecode('MCMLIV');   //1954
RomanDecode('MCMXC');    //1990  
RomanDecode('MMVIII');   //2008
RomanDecode('MDCLXVI');  //1666
RomanDecode('MDLXVI');   //1566


IMPORT STD;
RomanDecode(STRING s) := FUNCTION
  SetWeights := [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
  SetSymbols := ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
  ProcessRec := RECORD
    UNSIGNED val;
    STRING Roman;
  END;
  dsSymbols := DATASET(13,TRANSFORM(ProcessRec,SELF.Roman := s, SELF := []));
	
  RECORDOF(dsSymbols) XF(dsSymbols L, dsSymbols R, INTEGER C) := TRANSFORM
    ThisRoman := IF(C=1,R.Roman,L.Roman);
    IsDone := ThisRoman = '';
    Repeatable := C IN [1,5,9,13];
    SymSize := IF(C % 2 = 0, 2, 1);  
    IsNext := STD.Str.StartsWith(ThisRoman,SetSymbols[C]);
    SymLen := IF(IsNext,
                 IF(NOT Repeatable,
                    SymSize,
                    MAP(NOT IsDone AND ThisRoman[1] = ThisRoman[2] AND ThisRoman[1] = ThisRoman[3] => 3,
                        NOT IsDone AND ThisRoman[1] = ThisRoman[2] => 2,
                        NOT IsDone  => 1,
                        0)),
                 0);

    SymbolWeight(STRING s) := IF(NOT Repeatable,
                                 SetWeights[C],
                                 CHOOSE(LENGTH(s),SetWeights[C],SetWeights[C]*2,SetWeights[C]*3,0));

    SELF.Roman := IF(IsDone,ThisRoman,ThisRoman[SymLen+1..]);
    SELF.val   := IF(IsDone,L.val,L.Val + IF(IsNext,SymbolWeight(ThisRoman[1..SymLen]),0));
  END;
  i := ITERATE(dsSymbols,XF(LEFT,RIGHT,COUNTER));
  RETURN i[13].val;
END;

RomanDecode('MCMLIV');   //1954
RomanDecode('MCMXC');    //1990  
RomanDecode('MMVIII');   //2008
RomanDecode('MDCLXVI');  //1666
RomanDecode('MDLXVI');   //1566


  

You may also check:How to resolve the algorithm Permutations step by step in the GAP programming language
You may also check:How to resolve the algorithm Random numbers step by step in the PL/I programming language
You may also check:How to resolve the algorithm Factorial step by step in the Plain English programming language
You may also check:How to resolve the algorithm Variable size/Get step by step in the Swift programming language
You may also check:How to resolve the algorithm Multifactorial step by step in the VBScript programming language