Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,3 +1,5 @@
|
|||
using Printf
|
||||
|
||||
function romanencode(n::Integer)
|
||||
if n < 1 || n > 4999 throw(DomainError()) end
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
(define encodeGlyphs
|
||||
ACC 0 _ -> ACC
|
||||
ACC N [Glyph Value | Rest] -> (encodeGlyphs (@s ACC Glyph) (- N Value) [Glyph Value | Rest]) where (>= N Value)
|
||||
ACC N [Glyph Value | Rest] -> (encodeGlyphs ACC N Rest)
|
||||
)
|
||||
|
||||
(define encodeRoman
|
||||
N -> (encodeGlyphs "" N ["M" 1000 "CM" 900 "D" 500 "CD" 400 "C" 100 "XC" 90 "L" 50 "XL" 40 "X" 10 "IX" 9 "V" 5 "IV" 4 "I" 1])
|
||||
)
|
||||
26
Task/Roman-numerals-Encode/Vala/roman-numerals-encode.vala
Normal file
26
Task/Roman-numerals-Encode/Vala/roman-numerals-encode.vala
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
string to_roman(int n)
|
||||
requires (n > 0 && n < 5000)
|
||||
{
|
||||
const int[] weights = {1000, 900, 500, 400, 100, 90,
|
||||
50, 40, 10, 9, 5, 4, 1};
|
||||
const string[] symbols = {"M","CM","D","CD","C","XC","L",
|
||||
"XL","X","IX","V","IV","I"};
|
||||
|
||||
var roman = "", count = 0;
|
||||
foreach (var w in weights) {
|
||||
while (n >= w) {
|
||||
roman += symbols[count];
|
||||
n -= w;
|
||||
}
|
||||
if (n == 0)
|
||||
break;
|
||||
count++;
|
||||
}
|
||||
return roman;
|
||||
}
|
||||
|
||||
void main() {
|
||||
print("%s\n", to_roman(455));
|
||||
print("%s\n", to_roman(3456));
|
||||
print("%s\n", to_roman(2488));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue