Roman numerals/Decode: Difference between revisions
Content deleted Content added
→Insitux: implementation |
|||
(11 intermediate revisions by 10 users not shown) | |||
Line 2,509:
MDCLXVI: 1666
MMVIII: 2008</pre>
Solution 2.
<syntaxhighlight lang="lisp">
(defun chr->num (chr)
(nth (position chr "IVXLCDM") '(1 5 10 50 100 500 1000)))
(defun parse-rmn (str)
(let ((lst (map 'list #'chr->num str)))
(apply '+
(mapcar
(lambda (a b) (if (< a b) (- a) a))
(cons 0 lst)
(append lst '(0))))))
</syntaxhighlight>
=={{header|Cowgol}}==
Line 2,758 ⟶ 2,774:
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,899 ⟶ 2,976:
var total := 0;
for(int i := 0
{
var thisNumeral := RomanDictionary[s[i]] - minus;
Line 4,075 ⟶ 4,152:
(split-with val)
(map (.. +0))
(reduce
; Approach B
Line 4,384 ⟶ 4,461:
(fold and foldr examples)
<syntaxhighlight lang="javascript">(() => {
"use strict";
//
// Folding from right to left,
Line 4,393 ⟶ 4,471:
// fromRoman :: String -> Int
const fromRoman = s =>
([r, n], l) =>
: n - l
],
[0, 0]
)[1];
// glyphValue :: Char -> Maybe Int
const glyphValue = k => ({
I: 1,
V: 5,
X: 10,
L: 50,
C: 100,
D: 500,
M: 1000
}) [k] || 0;
// ----------------------
return [
"MDCLXVI", "MCMXC", "MMVIII", "MMXVI", "MMXVII"
]
.map(fromRoman)
.join("\n");
})();</syntaxhighlight>
{{Out}}
Line 4,445 ⟶ 4,508:
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,236 ⟶ 5,331:
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>
=== Alternate ===
{{trans|Kotlin}}
<syntaxhighlight lang="delphi">
function romanDecode(roman: String): Integer;
begin
result := 0;
if roman = '' then exit;
var n := 0;
var last := 'O';
foreach var c in roman do
begin
case c of
'I': n += 1;
'V': if (last = 'I') then n += 3 else n += 5;
'X': if (last = 'I') then n += 8 else n += 10;
'L': if (last = 'X') then n += 30 else n += 50;
'C': if (last = 'X') then n += 80 else n += 100;
'D': if (last = 'C') then n += 300 else n += 500;
'M': if (last = 'C') then n += 800 else n += 1000;
end;
last := c;
end;
result := n;
end;
begin
var romans := |'I', 'III', 'IV', 'VIII', 'XLIX', 'CCII',
'CDXXXIII', 'MCMXC', 'MMVIII', 'MDCLXVI'|;
foreach var roman in romans do
writeln(roman:10, romanDecode(roman):10);
end.
</syntaxhighlight>
=={{header|Perl}}==
Line 5,329 ⟶ 5,506:
=={{header|Phix}}==
<!--
<syntaxhighlight lang="phix">
with javascript_semantics
function romanDec(string s)
integer res = 0, prev = 0
for i=length(s) to 1 by -1 do
integer rdx = find(upper(s[i]),"IVXLCDM"),
rn = power(10,floor((rdx-1)/2))
if even(rdx) then rn *= 5 end if
res += iff(rn<prev?-rn:rn)
prev = rn
end for
return {s,res} -- (for output)
end function
?apply({"MCMXC","MMVIII","MDCLXVI"},romanDec)
{{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,239 ⟶ 6,425:
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 7,319 ⟶ 7,535:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var decode = Fn.new { |r|
|