Ad

This is a python implementation of the Roman Numerals program.

Code
Diff
  • 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

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.

Code
Diff
  • def Calculator(operator: str = "+", arg1: float = 0, arg2: float = 0):
        try:
            return eval(f"{arg1} {operator} {arg2}")
        except:
            return 0
    • import operator
    • def 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.

Code
Diff
  • 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 reduce
    • def 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