Roman numerals/Decode: Difference between revisions
m
→JS Folding from the right: Tidied
mNo edit summary |
m (→JS Folding from the right: Tidied) |
||
(14 intermediate revisions by 10 users not shown) | |||
Line 2,672:
=={{header|EasyLang}}==
<syntaxhighlight lang="text">
symbols$[] = [ "M" "D" "C" "L" "X" "V" "I" ]
values[] = [ 1000 500 100 50 10 5 1 ]
Line 2,688:
oldv = v
.
return val
.
print
</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
<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.
var total := 0;
for(int i := 0
{
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";
//
// Folding from right to left,
Line 4,363 ⟶ 4,455:
// 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,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">
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,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
[ <digit>* ] -> \(@: 0"1"; $... -> @: $@ + $; $@ !\)
rule digit: <value>* (@: $@ + 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="
var decode = Fn.new { |r|
|