Skip to content

Commit 590f6ab

Browse files
setting up roman to integer problem from leetcode
1 parent 425f7b4 commit 590f6ab

File tree

3 files changed

+113
-49
lines changed

3 files changed

+113
-49
lines changed
Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
const chunkArrayInGroups = require("../freecodecamp/chunkArrayInGroups.js");
1+
// const chunkArrayInGroups = require("../freecodecamp/chunkArrayInGroups.js");
22

3-
test("Test #1", () => {
4-
expect(chunkArrayInGroups(["a", "b", "c", "d"], 2)).toStrictEqual([
5-
["a", "b"],
6-
["c", "d"],
7-
]);
8-
});
3+
// test("Test #1", () => {
4+
// expect(chunkArrayInGroups(["a", "b", "c", "d"], 2)).toStrictEqual([
5+
// ["a", "b"],
6+
// ["c", "d"],
7+
// ]);
8+
// });
99

10-
test("Test #2", () => {
11-
expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)).toStrictEqual([
12-
[0, 1, 2],
13-
[3, 4, 5],
14-
]);
15-
});
10+
// test("Test #2", () => {
11+
// expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3)).toStrictEqual([
12+
// [0, 1, 2],
13+
// [3, 4, 5],
14+
// ]);
15+
// });
1616

17-
test("Test #3", () => {
18-
expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)).toStrictEqual([
19-
[0, 1],
20-
[2, 3],
21-
[4, 5],
22-
]);
23-
});
17+
// test("Test #3", () => {
18+
// expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2)).toStrictEqual([
19+
// [0, 1],
20+
// [2, 3],
21+
// [4, 5],
22+
// ]);
23+
// });
2424

25-
test("Test #4", () => {
26-
expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)).toStrictEqual([
27-
[0, 1, 2, 3],
28-
[4, 5],
29-
]);
30-
});
25+
// test("Test #4", () => {
26+
// expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4)).toStrictEqual([
27+
// [0, 1, 2, 3],
28+
// [4, 5],
29+
// ]);
30+
// });
3131

32-
test("Test #5", () => {
33-
expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)).toStrictEqual([
34-
[0, 1, 2],
35-
[3, 4, 5],
36-
[6],
37-
]);
38-
});
32+
// test("Test #5", () => {
33+
// expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3)).toStrictEqual([
34+
// [0, 1, 2],
35+
// [3, 4, 5],
36+
// [6],
37+
// ]);
38+
// });
3939

40-
test("Test #6", () => {
41-
expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)).toStrictEqual([
42-
[0, 1],
43-
[2, 3],
44-
[4, 5],
45-
[6, 7],
46-
[8],
47-
]);
48-
});
40+
// test("Test #6", () => {
41+
// expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2)).toStrictEqual([
42+
// [0, 1],
43+
// [2, 3],
44+
// [4, 5],
45+
// [6, 7],
46+
// [8],
47+
// ]);
48+
// });
4949

50-
test("Test #7", () => {
51-
expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)).toStrictEqual([
52-
[0, 1, 2, 3],
53-
[4, 5, 6, 7],
54-
[8],
55-
]);
56-
});
50+
// test("Test #7", () => {
51+
// expect(chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4)).toStrictEqual([
52+
// [0, 1, 2, 3],
53+
// [4, 5, 6, 7],
54+
// [8],
55+
// ]);
56+
// });

leetcode/RomanToInteger.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
function romanToInt(s) {
2+
const romanNumerals = new Map([
3+
["I", 1],
4+
["V", 5],
5+
["X", 10],
6+
["L", 50],
7+
["C", 100],
8+
["D", 500],
9+
["M", 1000],
10+
]);
11+
const romanNums = s.split(""); //O(n)
12+
let count = 0;
13+
let j = 0;
14+
15+
for (let i = 0; i < romanNums.length; i++) {
16+
// Lookup time constant by map.
17+
j++;
18+
if (romanNumerals.get(romanNums[i]) < romanNumerals.get(romanNums[j])) {
19+
let existentIndex = romanNums[j] ? romanNums[j] : romanNums[j - 1];
20+
let subSubstract =
21+
romanNumerals.get(romanNums[i]) - romanNumerals.get(existentIndex);
22+
count = count - subSubstract;
23+
} else if (
24+
romanNumerals.get(romanNums[i]) > romanNumerals.get(romanNums[j])
25+
) {
26+
let existentIndex = romanNums[j] ? romanNums[j] : romanNums[j - 1];
27+
let subPlus =
28+
romanNumerals.get(romanNums[i]) + romanNumerals.get(existentIndex);
29+
count = count + subPlus;
30+
} else {
31+
count = count + romanNumerals.get(romanNums[i]);
32+
}
33+
}
34+
return count;
35+
}
36+
37+
console.log(romanToInt("V"));
38+
39+
module.exports = romanToInt;

leetcode/RomanToInteger.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const romanToInt = require("./RomanToInteger.js");
2+
3+
test("Test #1", () => {
4+
expect(romanToInt("III")).toStrictEqual(3);
5+
});
6+
7+
test("Test #2", () => {
8+
expect(romanToInt("XIX")).toStrictEqual(19);
9+
});
10+
11+
test("Test #3", () => {
12+
expect(romanToInt("XV")).toStrictEqual(15);
13+
});
14+
15+
test("Test #4", () => {
16+
expect(romanToInt("XVIII")).toStrictEqual(18);
17+
});
18+
19+
test("Test #5", () => {
20+
expect(romanToInt("LVIII")).toStrictEqual(58);
21+
});
22+
23+
test("Test #6", () => {
24+
expect(romanToInt("MCMXCIV")).toStrictEqual(1994);
25+
});

0 commit comments

Comments
 (0)