-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger-to-roman.ts
48 lines (43 loc) · 994 Bytes
/
integer-to-roman.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const romanComp: Record<number, number[]> = {
"0": [],
"1": [1],
"2": [1, 1],
"3": [1, 1, 1],
"4": [1, 5],
"5": [5],
"6": [5, 1],
"7": [5, 1, 1],
"8": [5, 1, 1, 1],
"9": [1, 10],
};
const expRoman: Record<number, string[]> = {
"1": ["I", "X", "C", "M"],
"5": ["V", "L", "D"],
};
function toExpRoman(digit: number, exp: number): string {
let result = "";
let comps = romanComp[digit];
for (let comp of comps) {
let char: string;
if (comp === 10) {
char = expRoman[1][exp + 1];
} else {
char = expRoman[comp][exp];
}
result += char;
}
return result;
}
export function intToRoman(num: number): string {
let result = "";
let exp = 0;
let init = num;
let last: number;
while (init !== 0) {
last = init % 10;
init = ~~(init / 10);
result = `${toExpRoman(last, exp)}${result}`;
++exp;
}
return result;
}