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
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 i32slet 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"),
- }
#[cfg(test)] mod tests { use super::*; #[test] fn test_math() { assert_eq!(math("1 + 2").unwrap(), 3); assert_eq!(math("12+ 3456789").unwrap(), 3456801); assert_eq!(math("50 + 10 0").unwrap(), 150); assert_eq!(math("1-3").unwrap(), -2); assert_eq!(math(" 500 - 150 ").unwrap(), 350); assert_eq!(math("0-2").unwrap(), -2); assert_eq!(math("1* 30").unwrap(), 30); assert_eq!(math("20 *5").unwrap(), 100); assert_eq!(math("10/5").unwrap(), 2); assert_eq!(math("500 / 2 ").unwrap(), 250); assert_eq!(math("10 = 10").unwrap(), 1); assert_eq!(math(" 2 =50").unwrap(), 0); assert_eq!(math("0=0").unwrap(), 1); } }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
fn test_add() {assert_eq!(math("1 + 2"), 3);assert_eq!(math("12+ 3456789"), 3456801);assert_eq!(math("50 + 10 0"), 150);}#[test]fn test_sub() {assert_eq!(math("1-3"), -2);assert_eq!(math(" 500 - 150 "), 350);assert_eq!(math("0-2"), -2);}#[test]fn test_multiply() {assert_eq!(math("1* 30"), 30);assert_eq!(math("20 *5"), 100);}#[test]fn test_divide() {assert_eq!(math("10/5"), 2);assert_eq!(math("500 / 2 "), 250);}#[test]fn test_equal() {assert_eq!(math("10 = 10"), 1);assert_eq!(math(" 2 =50"), 0);assert_eq!(math("0=0"), 1);- fn test_math() {
- assert_eq!(math("1 + 2").unwrap(), 3);
- assert_eq!(math("12+ 3456789").unwrap(), 3456801);
- assert_eq!(math("50 + 10 0").unwrap(), 150);
- assert_eq!(math("1-3").unwrap(), -2);
- assert_eq!(math(" 500 - 150 ").unwrap(), 350);
- assert_eq!(math("0-2").unwrap(), -2);
- assert_eq!(math("1* 30").unwrap(), 30);
- assert_eq!(math("20 *5").unwrap(), 100);
- assert_eq!(math("10/5").unwrap(), 2);
- assert_eq!(math("500 / 2 ").unwrap(), 250);
- assert_eq!(math("10 = 10").unwrap(), 1);
- assert_eq!(math(" 2 =50").unwrap(), 0);
- assert_eq!(math("0=0").unwrap(), 1);
- }
- }