June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,20 @@
(def roman-map
(sorted-map-by >
1 "I", 4 "IV", 5 "V", 9 "IX",
10 "X", 40 "XL", 50 "L", 90 "XC",
100 "C", 400 "CD", 500 "D", 900 "CM"
1000 "M"))
(defn a2r
([r]
(reduce str (a2r r (keys roman-map))))
([r n]
(when-not (empty? n)
(let [e (first n)
v (- r e)
roman (roman-map e)]
(cond
(< v 0) (a2r r (rest n))
(= v 0) (cons roman [])
(>= v e) (cons roman (a2r v n))
(< v e) (cons roman (a2r v (rest n))))))))

View file

@ -0,0 +1,5 @@
(a2r 1666)
"MDCLXVI"
(map a2r [1000 1 389 45])
("M" "I" "CCCLXXXIX" "XLV")

View file

@ -0,0 +1,31 @@
import extensions.
import system'collections.
import system'routines.
static RomanDictionary = Dictionary new;
setAt(1000, "M");
setAt(900, "CM");
setAt(500, "D");
setAt(400, "CD");
setAt(100, "C");
setAt(90, "XC");
setAt(50, "L");
setAt(40, "XL");
setAt(10, "X");
setAt(9, "IX");
setAt(5, "V");
setAt(4, "IV");
setAt(1, "I").
extension $op
{
toRoman
= RomanDictionary accumulate(String new("I",self)) with(:m:kv)(m replace(String new("I",kv key), kv value)).
}
program =
[
console printLine("1990 : ", 1990 toRoman).
console printLine("2008 : ", 2008 toRoman).
console printLine("1666 : ", 1666 toRoman).
].

View file

@ -1,35 +1,32 @@
function romanencode(n::Integer)
const DR = [["I", "X", "C", "M"] ["V", "L", "D", "MMM"]]
if n < 1 || n > 4999 throw(DomainError()) end
DR = [["I", "X", "C", "M"] ["V", "L", "D", "MMM"]]
rnum = ""
if n > 4999 || n < 1
throw(DomainError())
end
for (omag, d) in enumerate(digits(n))
if d == 0
omr = ""
elseif d < 4
omr = DR[omag, 1]^d
elseif d < 4
omr = DR[omag, 1] ^ d
elseif d == 4
omr = DR[omag, 1]*DR[omag, 2]
omr = DR[omag, 1] * DR[omag, 2]
elseif d == 5
omr = DR[omag, 2]
elseif d < 9
omr = DR[omag, 2]*(DR[omag, 1]^(d - 5))
elseif d < 9
omr = DR[omag, 2] * DR[omag, 1] ^ (d - 5)
else
omr = DR[omag, 1]*DR[(omag +1), 1]
omr = DR[omag, 1] * DR[omag + 1, 1]
end
rnum = omr*rnum
rnum = omr * rnum
end
return rnum
end
testcases = Int64[1990, 2008, 1668]
for i in 1:12
push!(testcases, rand(1:4999))
end
testcases = [1990, 2008, 1668]
append!(testcases, rand(1:4999, 12))
testcases = unique(testcases)
println("Test romanencode, arabic => roman:")
for i in testcases
println(i, " => ", romanencode(i))
for n in testcases
@printf("%-4i => %s\n", n, romanencode(n))
end

View file

@ -0,0 +1,19 @@
fun Int.toRomanNumeral(): String {
fun digit(k: Int, unit: String, five: String, ten: String): String {
return when (k) {
in 1..3 -> unit.repeat(k)
4 -> unit + five
in 5..8 -> five + unit.repeat(k - 5)
9 -> unit + ten
else -> throw IllegalArgumentException("$k not in range 1..9")
}
}
return when (this) {
0 -> ""
in 1..9 -> digit(this, "I", "V", "X")
in 10..99 -> digit(this / 10, "X", "L", "C") + (this % 10).toRomanNumeral()
in 100..999 -> digit(this / 100, "C", "D", "M") + (this % 100).toRomanNumeral()
in 1000..3999 -> "M" + (this - 1000).toRomanNumeral()
else -> throw IllegalArgumentException("${this} not in range 0..3999")
}
}

View file

@ -0,0 +1,46 @@
MODULE RomanNumeralsEncode;
FROM Strings IMPORT
Append;
FROM STextIO IMPORT
WriteString, WriteLn;
CONST
MaxChars = 15;
(* 3888 or MMMDCCCLXXXVIII (15 chars) is the longest string properly encoded
with these symbols. *)
TYPE
TRomanNumeral = ARRAY [0 .. MaxChars - 1] OF CHAR;
PROCEDURE ToRoman(AValue: CARDINAL; VAR OUT Destination: ARRAY OF CHAR);
TYPE
TRomanSymbols = ARRAY [0 .. 1] OF CHAR;
TWeights = ARRAY [0 .. 12] OF CARDINAL;
TSymbols = ARRAY [0 .. 12] OF TRomanSymbols;
CONST
Weights = TWeights {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
Symbols = TSymbols {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX",
"V", "IV", "I"};
VAR
I: CARDINAL;
BEGIN
Destination := "";
I := 0;
WHILE (I <= HIGH(Weights)) AND (AValue > 0) DO
WHILE AValue >= Weights[I] DO
Append(Symbols[I], Destination);
AValue := AValue - Weights[I]
END;
INC(I);
END;
END ToRoman;
VAR
Numeral: TRomanNumeral;
BEGIN
ToRoman(1990, Numeral); WriteString(Numeral); WriteLn; (* MCMXC *)
ToRoman(2018, Numeral); WriteString(Numeral); WriteLn; (* MMXVIII *)
ToRoman(3888, Numeral); WriteString(Numeral); WriteLn; (* MMMDCCCLXXXVIII *)
END RomanNumeralsEncode.

View file

@ -0,0 +1,22 @@
my %symbols =
1 => "I", 5 => "V", 10 => "X", 50 => "L", 100 => "C",
500 => "D", 1_000 => "M";
my @subtractors =
1_000, 100, 500, 100, 100, 10, 50, 10, 10, 1, 5, 1, 1, 0;
multi sub roman (0) { '' }
multi sub roman (Int $n) {
for @subtractors -> $cut, $minus {
$n >= $cut
and return %symbols{$cut} ~ roman($n - $cut);
$n >= $cut - $minus
and return %symbols{$minus} ~ roman($n + $minus);
}
}
# Sample usage
for 1 .. 2_010 -> $x {
say roman($x);
}

View file

@ -0,0 +1,22 @@
def arabic_to_roman(dclxvi):
#===========================
'''Convert an integer from the decimal notation to the Roman notation'''
org = dclxvi; # 666 #
out = "";
for scale, arabic_scale in enumerate(arabic):
if org == 0: break
multiples = org // arabic_scale;
org -= arabic_scale * multiples;
out += roman[scale] * multiples;
if (org >= -adjust_arabic[scale] + arabic_scale):
org -= -adjust_arabic[scale] + arabic_scale;
out += adjust_roman[scale] + roman[scale]
return out
if __name__ == "__main__":
test = (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,25,30,40,50,60,69,70,
80,90,99,100,200,300,400,500,600,666,700,800,900,1000,1009,1444,1666,1945,1997,1999,
2000,2008,2500,3000,4000,4999,5000,6666,10000,50000,100000,500000,1000000);
for val in test:
print("%8d %s" %(val, arabic_to_roman(val)))

View file

@ -0,0 +1,17 @@
examples := [2008, 1666, 1990];
for example in examples loop
print( roman_numeral(example) );
end loop;
proc roman_numeral( n );
R := [[1000, 'M'], [900, 'CM'], [500, 'D'], [400, 'CD'], [100, 'C'], [90, 'XC'], [50, 'L'], [40, 'XL'], [10, 'X'], [9, 'IX'], [5, 'V'], [4, 'IV'], [1, 'I']];
roman := '';
for numeral in R loop
while n >= numeral(1) loop
n := n - numeral(1);
roman := roman + numeral(2);
end loop;
end loop;
return roman;
end;