Skip to content

Commit 5ce0afb

Browse files
committed
Convert integer to roman number
1 parent d6b183a commit 5ce0afb

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Each problem directory includes:
77

88
There might still be some personal comments added for my learning purposes.
99

10-
## Algorithms by Topic:
10+
## Algorithms:
1111

1212
- **Math**
1313
* Fibonacci Sequence
@@ -37,6 +37,14 @@ There might still be some personal comments added for my learning purposes.
3737
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/sorting/sorting.js)
3838
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/sorting/sorting.test.js)
3939

40+
- **LeetCode**
41+
* Valid Parentheses
42+
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.js)
43+
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.test.js)
44+
* Integer to Roman
45+
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/integer-to-roman/integer-to-roman.js)
46+
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/integer-to-roman/integer-to-roman.test.js)
47+
4048
- **All**
4149
* Snail
4250
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/snail/snail.js)
@@ -47,9 +55,7 @@ There might still be some personal comments added for my learning purposes.
4755
* Tree relation
4856
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/recursion-tree/recursion-tree.js)
4957
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/recursion-tree/recursion-tree.test.js)
50-
* Valid Parentheses
51-
[`Solution`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.js)
52-
[`Test file`](https://github.com/edignot/algorithm-data-structure-practice/blob/master/src/valid-parentheses/valid-parentheses.test.js)
58+
5359

5460
## Usage
5561
- Clone this repo
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Problem: Given an integer, convert it to a roman numeral
2+
// https://leetcode.com/problems/integer-to-roman/
3+
// Time complexity: O(n)
4+
5+
const integerToRoman = (num) => {
6+
const romans = {
7+
M: 1000,
8+
CM: 900,
9+
D: 500,
10+
CD: 400,
11+
C: 100,
12+
XC: 90,
13+
L: 50,
14+
XL: 40,
15+
X: 10,
16+
IX: 9,
17+
V: 5,
18+
IV: 4,
19+
I: 1,
20+
};
21+
const romanNum = [];
22+
for (const roman in romans) {
23+
if (num >= romans[roman]) {
24+
const amount = Math.floor(num / romans[roman]);
25+
for (let i = 0; i < amount; i++) {
26+
romanNum.push(roman);
27+
}
28+
num -= amount * romans[roman];
29+
}
30+
}
31+
return romanNum.join("");
32+
};
33+
34+
export default integerToRoman;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import integerToRoman from "./integer-to-roman";
2+
3+
describe("Integer to Roman number", () => {
4+
test("Integer to Roman number", () => {
5+
expect(integerToRoman(126)).toBe("CXXVI");
6+
});
7+
test("Integer to Roman number", () => {
8+
expect(integerToRoman(234)).toBe("CCXXXIV");
9+
});
10+
test("Integer to Roman number", () => {
11+
expect(integerToRoman(0)).toBe("");
12+
});
13+
});

0 commit comments

Comments
 (0)