Ad
Code
Diff
  • var tr = {
      "I": 1,
      "V": 5,
      "X": 10,
      "L": 50,
      "C": 100,
      "D": 500,
      "M": 1000
    }
    
    function solution (roman_string) {
      // Check if input is a string
      if (typeof roman_string !== 'string') {
        throw new TypeError('Input must be a string');
      }
    
      // Check if string is empty
      if (roman_string.length === 0) {
        throw new Error('String cannot be empty');
      }
    
      // Check if string contains only valid Roman numerals
      if (!/^M*(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.test(roman_string)) {
        throw new Error('Invalid Roman numeral');
      }
    
      return roman_string.split('').map((letter, index, arr) => {
        const current_value = tr[letter];
        const next_value = tr[arr[index+1]];
        return next_value && current_value < next_value ? 
          (- current_value) : current_value;
      }).reduce((acc, item) => acc + item, 0);
    }
    
    • var tr = {
    • "I": 1,
    • "V": 5,
    • "X": 10,
    • "L": 50,
    • "C": 100,
    • "D": 500,
    • "M": 1000
    • }
    • function solution (roman_string) {
    • return roman_string.split('').map((letter, index, a) => {
    • // Check if input is a string
    • if (typeof roman_string !== 'string') {
    • throw new TypeError('Input must be a string');
    • }
    • // Check if string is empty
    • if (roman_string.length === 0) {
    • throw new Error('String cannot be empty');
    • }
    • // Check if string contains only valid Roman numerals
    • if (!/^M*(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.test(roman_string)) {
    • throw new Error('Invalid Roman numeral');
    • }
    • return roman_string.split('').map((letter, index, arr) => {
    • const current_value = tr[letter];
    • const next_value = tr[a[index+1]];
    • const next_value = tr[arr[index+1]];
    • return next_value && current_value < next_value ?
    • (- current_value) : current_value;
    • }).reduce((acc, item) => acc + item, 0);
Strings
Mathematics
Code
Diff
  • fn math(s: &str) -> Result<i32, &'static str> {
        let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect();
        let (num1, num2) = math_str
            .split_once(|ch: char| matches!(ch, '+' | '-' | '*' | '/' | '='))
            .unwrap_or_else(|| panic!("missing operator, expected `+`, `-`, `*`, `/` or `=`"));
        let operator = &math_str[num1.len()..=num1.len()];
        let num1: i32 = num1.parse().unwrap_or_else(|_| panic!("invalid first operand"));
        let num2: i32 = num2.parse().unwrap_or_else(|_| panic!("invalid second operand"));
    
        match operator {
            "+" => Ok(num1 + num2),
            "-" => Ok(num1 - num2),
            "*" => Ok(num1 * num2),
            "/" => {
                if num2 != 0 {
                    Ok(num1 / num2)
                } else {
                    Err("Cannot divide by zero")
                }
            },
            "=" => Ok(i32::from(num1 == num2)),
            _ => Err("Invalid operator"),
        }
    }
    
    • fn math(s: &str) -> i32 {
    • // remove whitespace characters so all we have left are digits and the operator
    • fn math(s: &str) -> Result<i32, &'static str> {
    • let math_str: String = s.chars().filter(|ch| !ch.is_whitespace()).collect();
    • // split the string by the operator character
    • let (num1, num2) = math_str
    • .split_once(|ch: char| matches!(ch, '+' | '-' | '*' | '/' | '='))
    • .expect("missing operator, expected `+`, `-`, `*`, `/` or `=`");
    • // slice out the operator character we just splitted by
    • .unwrap_or_else(|| panic!("missing operator, expected `+`, `-`, `*`, `/` or `=`"));
    • let operator = &math_str[num1.len()..=num1.len()];
    • let num1: i32 = num1.parse().unwrap_or_else(|_| panic!("invalid first operand"));
    • let num2: i32 = num2.parse().unwrap_or_else(|_| panic!("invalid second operand"));
    • // parse both operands as i32s
    • let num1: i32 = num1.parse().expect("invalid first operand");
    • let num2: i32 = num2.parse().expect("invalid second operand");
    • // pattern-match the operator character
    • match operator {
    • "+" => num1 + num2,
    • "-" => num1 - num2,
    • "*" => num1 * num2,
    • "/" => num1 / num2,
    • "=" => i32::from(num1 == num2),
    • _ => unreachable!(),
    • "+" => Ok(num1 + num2),
    • "-" => Ok(num1 - num2),
    • "*" => Ok(num1 * num2),
    • "/" => {
    • if num2 != 0 {
    • Ok(num1 / num2)
    • } else {
    • Err("Cannot divide by zero")
    • }
    • },
    • "=" => Ok(i32::from(num1 == num2)),
    • _ => Err("Invalid operator"),
    • }