Roman numerals/Decode: Difference between revisions

m
mNo edit summary
 
(14 intermediate revisions by 10 users not shown)
Line 2,672:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
procfunc rom2dec rom$ . val .
symbols$[] = [ "M" "D" "C" "L" "X" "V" "I" ]
values[] = [ 1000 500 100 50 10 5 1 ]
Line 2,688:
oldv = v
.
return val
.
callprint rom2dec "MCMXC" v
print vrom2dec "MMVIII"
callprint rom2dec "MMVIIIMDCLXVI" v
print v
call rom2dec "MDCLXVI" v
print v
</syntaxhighlight>
 
Line 2,760 ⟶ 2,758:
RomanDecode('MDCLXVI'); //1666
RomanDecode('MDLXVI'); //1566</syntaxhighlight>
 
=={{header|Ed}}==
 
<syntaxhighlight>
H
g/[^MDCLXVI]/s///g
# simplify the quirky parts
g/CM/s//CCCCCCCCC/
g/D/s//CCCCC/
g/CD/s//CCCC/
g/XC/s//XXXXXXXXX/
g/L/s//XXXXX/
g/XL/s//XXXX/
g/IX/s//IIIIIIIII/
g/V/s//IIIII/
g/IV/s//IIII/
# convert simplified numerals to integers
g/MMMMMMMMM/s//9/
g/MMMMMMMM/s//8/
g/MMMMMMM/s//7/
g/MMMMMM/s//6/
g/MMMMM/s//5/
g/MMMM/s//4/
g/MMM/s//3/
g/MM/s//2/
g/M/s//1/
v/^[0-9].*$/s/.*/0&/
g/CCCCCCCCC/s//9/
g/CCCCCCCC/s//8/
g/CCCCCCC/s//7/
g/CCCCCC/s//6/
g/CCCCC/s//5/
g/CCCC/s//4/
g/CCC/s//3/
g/CC/s//2/
g/C/s//1/
v/^[0-9]{2}.*$/s/^([0-9])(.*)$/\10\2/
g/XXXXXXXXX/s//9/
g/XXXXXXXX/s//8/
g/XXXXXXX/s//7/
g/XXXXXX/s//6/
g/XXXXX/s//5/
g/XXXX/s//4/
g/XXX/s//3/
g/XX/s//2/
g/X/s//1/
v/^[0-9]{3}.*$/s/^([0-9]{2})(.*)$/\10\2/
g/IIIIIIIII/s//9/
g/IIIIIIII/s//8/
g/IIIIIII/s//7/
g/IIIIII/s//6/
g/IIIII/s//5/
g/IIII/s//4/
g/III/s//3/
g/II/s//2/
g/I/s//1/
v/^[0-9]{4}.*$/s/^([0-9]{3})(.*)$/\10\2/
g/^0+([0-9])/s//\1/
,p
Q
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 2,878 ⟶ 2,937:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'collections;
import system'routines;
import system'culture;
static RomanDictionary = Dictionary.new()
Line 2,897 ⟶ 2,957:
{
var minus := 0;
var s := self.upperCasetoUpper();
var total := 0;
for(int i := 0,; i < s.Length,; i += 1)
{
var thisNumeral := RomanDictionary[s[i]] - minus;
Line 4,062 ⟶ 4,122:
MDCLXVI = 1666
MMVIII = 2008</pre>
 
=={{header|Insitux}}==
 
{{Trans|Clojure}}
 
<syntaxhighlight lang="insitux">
(var numerals {"M" 1000 "D" 500 "C" 100 "L" 50 "X" 10 "V" 5 "I" 1})
 
; Approach A
(function ro->ar r
(-> (reverse (upper-case r))
(map numerals)
(split-with val)
(map (.. +0))
(reduce @(((< % %1) + -)))))
 
; Approach B
(function ro->ar r
(-> (upper-case r)
(map numerals)
@(reduce (fn [sum lastv] curr [(+ sum curr ((< lastv curr) (* -2 lastv) 0)) curr]) [0 0])
0))
 
(map ro->ar ["MDCLXVI" "MMMCMXCIX" "XLVIII" "MMVIII"])
</syntaxhighlight>
 
{{out}}
 
<pre>
[1666 3999 48 2008]
</pre>
 
=={{header|J}}==
Line 4,354 ⟶ 4,445:
(fold and foldr examples)
<syntaxhighlight lang="javascript">(() => {
"use strict";
 
// -------------- ROMAN NUMERALS DECODED ---------------
 
// Folding from right to left,
Line 4,363 ⟶ 4,455:
// fromRoman :: String -> Int
const fromRoman = s =>
foldr(l => ([r, n...s]) => [
l,.map(glyphValue)
l >= r ? .reduceRight(
([r, n], l) => n + l[
) : n - l,
])([0, 0])( l >= r
[...s].map(charVal) ? n + l
: n - l
],
[0, 0]
)[1];
 
// charVal :: Char -> Maybe Int
const charVal = k => {
const v = {
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
} [k];
return v !== undefined ? v : 0;
};
 
// ----------------------- TEST ------------------------
const main = () => [
'MDCLXVI', 'MCMXC', 'MMVIII', 'MMXVI', 'MMXVII'
]
.map(fromRoman)
.join('\n');
 
// glyphValue :: Char -> Maybe Int
const glyphValue = k => ({
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}) [k] || 0;
 
// ---------------------- GENERIC FUNCTIONSTEST -----------------------
return [
 
"MDCLXVI", "MCMXC", "MMVIII", "MMXVI", "MMXVII"
// foldr :: (a -> b -> b) -> b -> [a] -> b
]
const foldr = f =>
.map(fromRoman)
// Note that that the Haskell signature of foldr
.join("\n");
// differs from that of foldl - the positions of
// accumulator and current value are reversed.
a => xs => [...xs].reduceRight(
(a, x) => f(x)(a),
a
);
 
// MAIN ---
return main();
})();</syntaxhighlight>
{{Out}}
Line 4,415 ⟶ 4,492:
2016
2017</pre>
 
====Declarative====
<syntaxhighlight lang="javascript">
(() => {
function toNumeric(value) {
return value
.replace(/IV/, 'I'.repeat(4))
.replace(/V/g, 'I'.repeat(5))
.replace(/IX/, 'I'.repeat(9))
.replace(/XC/, 'I'.repeat(90))
.replace(/XL/, 'I'.repeat(40))
.replace(/X/g, 'I'.repeat(10))
.replace(/L/, 'I'.repeat(50))
.replace(/CD/, 'I'.repeat(400))
.replace(/CM/, 'I'.repeat(900))
.replace(/C/g, 'I'.repeat(100))
.replace(/D/g, 'I'.repeat(500))
.replace(/M/g, 'I'.repeat(1000))
.length;
}
 
const numerics = ["MMXVI", "MCMXC", "MMVIII", "MM", "MDCLXVI"]
.map(toNumeric);
 
console.log(numerics);
})();
</syntaxhighlight>
 
{{Out}}
<pre>
[2016, 1990, 2008, 2000, 1666]
</pre>
 
=={{header|jq}}==
Line 5,206 ⟶ 5,315:
t+cur
};</syntaxhighlight>
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
 
function Rim2Arab(S : String) : Integer;
const //римские числа на соответствующие десятичные числа
R : array[1..14] of String[2] = ('M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I',' ');
A : array[1..14] of Integer = (1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1, 0);
begin
var i := 1;
Result := 0;
while S.Length > 0 do
begin
while S.IndexOf(R[i]) = 0 do
begin
S := S.Remove(0, R[i].Length);
Result += A[i]
end;
i += 1;
end;
end;
const
L = 'IVXLCDM';
begin
var S := 'MDCLXVI';//'roman numeral:';
Write(S,': ');
var index := 1;
repeat
if L.IndexOf(S[index]) < 0 then
index += 1
else
begin
var Rim : String := '';
repeat
Rim += S[index];
S := S.Remove(index - 1, 1);
until (S.Length < index) or (L.IndexOf(S[index]) < 0);
S := S.Insert(index - 1, Rim2Arab(Rim).ToString);
end;
until index > S.Length;
WriteLn(S);
end.
 
</syntaxhighlight>
 
{{out}}
<pre>
MDCLXVI: 1666
</pre>
 
=={{header|Perl}}==
Line 5,299 ⟶ 5,458:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<syntaxhighlight lang="phix">
<span style="color: #008080;">function</span> <span style="color: #000000;">romanDec</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
with javascript_semantics
<span style="color: #008080;">constant</span> <span style="color: #000000;">romans</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"MDCLXVI"</span><span style="color: #0000FF;">,</span>
function romanDec(string s)
<span style="color: #000000;">decmls</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">500</span><span style="color: #0000FF;">,</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">50</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span>
integer res = 0, prev = 0
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
for i=length(s) to 1 by -1 do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
integer rdx = find(upper(s[i]),"IVXLCDM"),
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">decmls</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">romans</span><span style="color: #0000FF;">)]</span>
rn = power(10,floor((rdx-1)/2))
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;"><</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">-</span><span style="color: #000000;">n</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if even(rdx) then rn *= 5 end if
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">n</span>
res += iff(rn<prev?-rn:rn)
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
prev = rn
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<span style="color: #000080;font-style:italic;">-- return res</span>
return {s,res} -- (for output)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">}</span> <span style="color: #000080;font-style:italic;">-- (for output)</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
 
?apply({"MCMXC","MMVIII","MDCLXVI"},romanDec)
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"MCMXC"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MMVIII"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MDCLXVI"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">romanDec</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
{{"MCMXC",1990},{"MMVIII",2008},{"MDCLXVI",1666}}
</pre>
=== cheating slightly ===
<syntaxhighlight lang="phix">
with javascript_semantics
requires("1.0.5")
function romanDec(string s)
return {s,scanf(s,"%R")[1][1]}
end function
</syntaxhighlight>
same output, if applied the same way as above, error handling omitted
 
=={{header|Phixmonti}}==
Line 6,209 ⟶ 6,377:
print roman-to-arabic "MMXVI"
</syntaxhighlight>
 
=={{header|REFAL}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <RomanDecode 'MCMXC'>>
<Prout <RomanDecode 'MMVIII'>>
<Prout <RomanDecode 'MDCLXVI'>>;
};
 
RomanDecode {
= 0;
e.D, <Upper e.D>: {
'M' e.R = <+ 1000 <RomanDecode e.R>>;
'CM' e.R = <+ 900 <RomanDecode e.R>>;
'D' e.R = <+ 500 <RomanDecode e.R>>;
'CD' e.R = <+ 400 <RomanDecode e.R>>;
'C' e.R = <+ 100 <RomanDecode e.R>>;
'XC' e.R = <+ 90 <RomanDecode e.R>>;
'L' e.R = <+ 50 <RomanDecode e.R>>;
'XL' e.R = <+ 40 <RomanDecode e.R>>;
'X' e.R = <+ 10 <RomanDecode e.R>>;
'IX' e.R = <+ 9 <RomanDecode e.R>>;
'V' e.R = <+ 5 <RomanDecode e.R>>;
'IV' e.R = <+ 4 <RomanDecode e.R>>;
'I' e.R = <+ 1 <RomanDecode e.R>>;
};
};</syntaxhighlight>
{{out}}
<pre>1990
2008
1666</pre>
 
=={{header|REXX}}==
Line 6,924 ⟶ 7,122:
def digits: [(M:1000"1"), (CM:900"1"), (D:500"1"), (CD:400"1"), (C:100"1"), (XC:90"1"), (L:50"1"), (XL:40"1"), (X:10"1"), (IX:9"1"), (V:5"1"), (IV:4"1"), (I:1"1")];
composer decodeRoman
@: 1"1";
[ <digit>* ] -> \(@: 0"1"; $... -> @: $@ + $; $@ !\)
rule digit: <value>* (@: $@ + 1"1";)
rule value: <='$digits($@)::key;'> -> $digits($@)::value
end decodeRoman
Line 6,940 ⟶ 7,138:
{{out}}
<pre>
1990"1"
2008"1"
1666"1"
</pre>
 
Line 7,289 ⟶ 7,487:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var decode = Fn.new { |r|
9,659

edits