Roman numerals/Encode: Difference between revisions
m
→{{header|Phix}}: use pygments, added cheat version
mNo edit summary |
m (→{{header|Phix}}: use pygments, added cheat version) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 3,130:
<syntaxhighlight lang="text">
values[] = [ 1000 900 500 400 100 90 50 40 10 9 5 4 1 ]
symbol$[] = [ "M" "CM" "D" "CD" "C" "XC" "L" "XL" "X" "IX" "V" "IV" "I" ]
rom$ = ""▼
for i = 1 to len values[]
while dec >= values[i]
Line 3,140 ⟶ 3,139:
.
.
return rom$
.
print
</syntaxhighlight>
Line 3,261 ⟶ 3,258:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import system'collections;
import system'routines;
Line 3,284 ⟶ 3,281:
extension op
{
}
Line 5,701 ⟶ 5,698:
=={{header|Phix}}==
<!--
with javascript_semantics
function toRoman(integer v)
sequence roman = {"M", "CM", "D","CD", "C","XC","L","XL","X","IX","V","IV","I"},
decml = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 }
integer val = v
for i=1 to length(roman) do
while val>=decml[i] do
res &= roman[i]
val -= decml[i]
end while
end for
return {v,res} -- (for output)
end function
?apply({1990,2008,1666},toRoman)
▲<!--</syntaxhighlight>-->
</syntaxhighlight>
{{out}}
<pre>
{{1990,"MCMXC"},{2008,"MMVIII"},{1666,"MDCLXVI"}}
</pre>
=== cheating slightly ===
<syntaxhighlight lang="phix">
with javascript_semantics
requires("1.0.5")
function toRoman(integer n)
return {n,sprintf("%R",n)}
end function
</syntaxhighlight>
same output (builtins\VM\pprntfN.e/toRoman() is somewhat more obfuscated and faster than the above)
=={{header|Phixmonti}}==
Line 7,466 ⟶ 7,473:
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")];
templates encodeRoman
@: 1
'$ -> ($)"1" -> #;' !
when <$digits($@)::value..> do
Line 7,472 ⟶ 7,479:
$ - $digits($@)::value -> #
when <1"1"..> do
@:$@ + 1
$ -> #
end encodeRoman
Line 7,739 ⟶ 7,746:
=={{header|Wren}}==
{{trans|Kotlin}}
<syntaxhighlight lang="
[1000, "M"],
[900, "CM"],
|