Two roman numbers are passed as strings.
You need to compare them and return -1, if the first is greater, 0 if they are equal and 1 otherwise
function fromRoman(roman) { const letters = roman.split(''); const keys = { M: 1_000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 }; return letters.reduce((n, key, i) => { keys[letters[i + 1]] > keys[key] ? n -= keys[key] : n += keys[key] return n; }, 0); } function compareRoman(r1, r2) { const [n1, n2] = [r1, r2].map(fromRoman); if (n1 === n2) return 0; return n1 > n2 ? 1 : -1; }
def from_roman(num):roman_numerals = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}result = 0for i, c in enumerate(num):if (i+1) == len(num) or roman_numerals[c] >= roman_numerals[num[i+1]]:result += roman_numerals[c]else:result -= roman_numerals[c]return result- function fromRoman(roman) {
- const letters = roman.split('');
- const keys = { M: 1_000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1 };
def compare_roman(a, b):- return letters.reduce((n, key, i) => {
- keys[letters[i + 1]] > keys[key] ? n -= keys[key] : n += keys[key]
- return n;
- }, 0);
- }
a = from_roman(a)b = from_roman(b)if a < b:return -1elif a == b:return 0else:return 1- function compareRoman(r1, r2) {
- const [n1, n2] = [r1, r2].map(fromRoman);
- if (n1 === n2) return 0;
- return n1 > n2 ? 1 : -1;
- }
const assert = require('chai').assert; describe("sample tests", () => { it("sample tests", () => { assert.strictEqual(compareRoman('XX', 'XL'), -1); assert.strictEqual(compareRoman('MM', 'MM'), 0); assert.strictEqual(compareRoman('X', 'V'), 1); }); });
test.assert_equals(compare_roman('XX', 'XL'), -1)test.assert_equals(compare_roman('MM', 'MM'), 0)test.assert_equals(compare_roman('X', 'V'), 1)- const assert = require('chai').assert;
- describe("sample tests", () => {
- it("sample tests", () => {
- assert.strictEqual(compareRoman('XX', 'XL'), -1);
- assert.strictEqual(compareRoman('MM', 'MM'), 0);
- assert.strictEqual(compareRoman('X', 'V'), 1);
- });
- });
function prime_checker(num) { if (num <= 1) return false; if (num == 2) return true; if (num % 2 == 0) return false; const boundary = Math.floor(Math.sqrt(num)); for (let i = 3; i <= boundary; i += 2) if (num % i === 0) return false; return true; }
function prime_checker (n) {// Write a javascript function to check if N is a prime number- function prime_checker(num) {
- if (num <= 1) return false;
- if (num == 2) return true;
- if (num % 2 == 0) return false;
- const boundary = Math.floor(Math.sqrt(num));
- for (let i = 3; i <= boundary; i += 2)
- if (num % i === 0) return false;
- return true;
- }