This is a python implementation of the Roman Numerals program.
def solution(s): roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000} total = 0 for index, char in enumerate(s): current_value = roman_map[char] next_value = roman_map[s[index + 1]] if index + 1 < len(s) else 0 total += current_value if current_value >= next_value else -current_value return total
var solution = (s) => {let total = 0;for(let index = 0; index < s.length; index++){((s.length >= index+1) && (romanMap[s[index]] < romanMap[s[index+1]]))? (total +=romanMap[s[index+1]] - romanMap[s[index]]) && index++: total += romanMap[s[index]];}return total;};- def solution(s):
- roman_map = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
- total = 0
- for index, char in enumerate(s):
- current_value = roman_map[char]
- next_value = roman_map[s[index + 1]] if index + 1 < len(s) else 0
- total += current_value if current_value >= next_value else -current_value
const romanMap = {'I': 1,'V': 5,'X': 10,'L': 50,'C': 100,'D': 500,'M': 1000};- return total
import unittest from solution import solution class Test(unittest.TestCase): def test(self): self.assertEqual(solution('XXI'), 21) self.assertEqual(solution('I'), 1) self.assertEqual(solution('IV'), 4) self.assertEqual(solution('MMVIII'), 2008) self.assertEqual(solution('MDCLXVI'), 1666)
const strictEqual = require('chai').assert.strictEqual;function doTest (romanString, expected) {const actual = solution(romanString);strictEqual(actual, expected, `for roman number ${romanString}`);}describe("Tests", () => {it("sample tests", () => {doTest('XXI', 21);doTest('I', 1);doTest('IV', 4);doTest('MMVIII', 2008);doTest('MDCLXVI', 1666);});});- import unittest
- from solution import solution
- class Test(unittest.TestCase):
- def test(self):
- self.assertEqual(solution('XXI'), 21)
- self.assertEqual(solution('I'), 1)
- self.assertEqual(solution('IV'), 4)
- self.assertEqual(solution('MMVIII'), 2008)
- self.assertEqual(solution('MDCLXVI'), 1666)
This new calculator uses the eval method instead of a predefined dictionary, taking fewer operations and also expanding the type of opperations which can be done, exponent "**" for example and modulus "%".
Other changes include, Type hints and more meaningful names for the parameters.
def Calculator(operator: str = "+", arg1: float = 0, arg2: float = 0): try: return eval(f"{arg1} {operator} {arg2}") except: return 0
import operatordef Calculator(c="+", a=0, b=0):operators = {"+": operator.add,"-": operator.sub,"*": operator.mul,"/": operator.truediv,}- def Calculator(operator: str = "+", arg1: float = 0, arg2: float = 0):
- try:
return operators[c](a, b)- return eval(f"{arg1} {operator} {arg2}")
- except:
I changed it to a lambda function to make it shorter and require less operations.
import operator def Calculator(c="+", a=0, b=0): operators = { "+": operator.add, "-": operator.sub, "*": operator.mul, "/": operator.truediv, } try: return operators[c](a, b) except: return 0
- import operator
from functools import reducedef Calculator(c = "+", a = 0, b = 0):- def Calculator(c="+", a=0, b=0):
- operators = {
'+': operator.add,'-': operator.sub,'*': operator.mul,'/': operator.truediv- "+": operator.add,
- "-": operator.sub,
- "*": operator.mul,
- "/": operator.truediv,
- }
try: return reduce(operators[c], [a, b])except: return 0- try:
- return operators[c](a, b)
- except:
- return 0