Roman numerals/Decode: Difference between revisions
Add a declarative way of achieving the transformation
(Add a declarative way of achieving the transformation) |
|||
(9 intermediate revisions by 7 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,878 ⟶ 2,876:
=={{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,896:
{
var minus := 0;
var s := self.
var total := 0;
for(int i := 0
{
var thisNumeral := RomanDictionary[s[i]] - minus;
Line 4,062 ⟶ 4,061:
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,415 ⟶ 4,445:
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,299 ⟶ 5,361:
=={{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,280:
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,940 ⟶ 7,041:
{{out}}
<pre>
1990"1"
2008"1"
1666"1"
</pre>
Line 7,289 ⟶ 7,390:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var decode = Fn.new { |r|
|