-
Notifications
You must be signed in to change notification settings - Fork 0
/
romanNumerals.js
162 lines (152 loc) · 5.19 KB
/
romanNumerals.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/**
*
* I, II, III, IV, V, VI, VII, VIII, IX, X
*
*
* The rules of roman numerals:
* ==============================
* There are six basic rules when it comes to roman numerals.
*
* Rule 1: The numerals:
* **************************
* All roman numerals are written using a combination of base numerals and subtractive numerals.
* There are only seven base numerals which can be used to create any numeral sequence or number representation:
* Utilizing the base numerals, there are also six subtractive numerals which act as shortcuts to represent numerals which would otherwise require four or five individual numerals:
* I, V, X, L, C, D, M
*
* Rule 2: The calculation:
* **************************
* The value of a roman numeral sequence is calculated by simply adding together the numerals, from left to right.
* For example, the number 1988 converted to roman numerals is MCMLXXXVIII which is calculated from left-to-right as:
* M+CM+L+X+X+X+V+I+I+I
*
* Rule 3: Number limits:
* **************************
* Any sequence of standard roman numerals can only represent numbers from 1 to 3999.
* There is no zero in roman numerals.
* Using bracket notation or vinculum notation we can write roman numerals from 4000 to 3999999.
*
* Rule 4: Sequencing:
* **************************
* Roman numerals are written from left to right, and from highest to lowest (in terms of individual numeral value).
* A higher value numeral cannot appear after a lower value numeral (unless in the context of an individual subtractive numeral).
*
* Rule 5: Repetition:
* **************************
* An individual numeral cannot appear more than three times consecutively in any sequence.
* If you find yourself with four same-value numerals consecutively, it can always be simplified by using a subtractive numeral in its place.
* A special case for this simplification rule is four M's. This exceeds the 3999 limit and would be invalid unless vinculum notation
* or bracket notation were adopted. Using bracket notation, four M's becomes (IV).
*
* Rule 6: Single use numerals:
* **************************
* There are individual numerals which can only appear once in any sequence.
* All subtractive numerals can only appear once in any sequence.
* From the set of base numerals: V, L, and D should only appear once.
* The only caveat to this is when using another notation such as bracket notation.
* A single-use numeral can appear once within the brackets and once outside the brackets.
* For example, 5005 in bracket notation is (V)V.
*
*/
function romanNumeral(num) {
// roman numeral object mapping
const rNumeral = {
1: {
1: 'I',
2: 'II',
3: 'III',
4: 'IV',
5: 'V',
6: 'VI',
7: 'VII',
8: 'VIII',
9: 'IX',
},
2: {
1: 'X',
2: 'XX',
3: 'XXX',
4: 'XL',
5: 'L',
6: 'LX',
7: 'LXX',
8: 'LXXX',
9: 'XC',
},
3: {
1: 'C',
2: 'CC',
3: 'CCC',
4: 'CD',
5: 'D',
6: 'DC',
7: 'DCC',
8: 'DCCC',
9: 'CM',
},
4: {
1: 'M',
2: 'MM',
3: 'MMM',
4: 'MV\u{0305}',
5: 'V\u{0305}',
6: 'V\u{0305}M',
7: 'V\u{0305}MM',
8: 'V\u{0305}MMM',
9: 'X\u{0305}',
},
5: {
1: 'X\u{0305}',
2: 'X\u{0305}X\u{0305}',
3: 'X\u{0305}X\u{0305}X\u{0305}',
4: 'X\u{0305}L\u{0305}',
5: 'L\u{0305}',
6: 'L\u{0305}X\u{0305}',
7: 'L\u{0305}X\u{0305}X\u{0305}',
8: 'L\u{0305}X\u{0305}X\u{0305}X\u{0305}',
9: 'X\u{0305}C\u{0305}',
},
6: {
1: 'C\u{0305}',
2: 'C\u{0305}C\u{0305}',
3: 'C\u{0305}C\u{0305}C\u{0305}',
4: 'C\u{0305}D\u{0305}',
5: 'D\u{0305}',
6: 'D\u{0305}C\u{0305}',
7: 'D\u{0305}C\u{0305}C\u{0305}',
8: 'D\u{0305}C\u{0305}C\u{0305}C\u{0305}',
9: 'C\u{0305}M\u{0305}',
},
7: {
1: 'M\u{0305}',
2: 'M\u{0305}M\u{0305}',
3: 'M\u{0305}M\u{0305}M\u{0305}',
}
}
// Driver Code
// Range Limit
return (num <= 0 || num > 3999999) ? {
range: '1 >>> 3999999'
} : num
.toString()
.split('')
.map((item, index) => item = rNumeral[num.toString().length - index][item])
.join('')
}
// test cases
console.log(romanNumeral(1))
console.log(romanNumeral(10))
console.log(romanNumeral(20))
console.log(romanNumeral(3878))
console.log(romanNumeral(45))
console.log(romanNumeral(585))
console.log(romanNumeral(60000))
console.log(romanNumeral(77227))
console.log(romanNumeral(82))
console.log(romanNumeral(978))
console.log(romanNumeral(999999))
console.log(romanNumeral(1000000))
console.log(romanNumeral(3999999))
console.log(romanNumeral(0))
console.log(romanNumeral(4000000))
console.log(romanNumeral(-21))