Maharramoff icon

Roman To Integer

Maharramoff | PRO | 03/23/20 01:16:30 PM UTC | 0 ⭐ | 10622 👁️ | Never ⏰ | []
Dart |

540 B

|

None

|

0 👍

/

0 👎

final romanNumbers = {
    "I": 1,
    "V": 5,
    "X": 10,
    "L": 50,
    "C": 100,
    "D": 500,
    "M": 1000
  };  
 
void main() 
{
    print('III: ${romanToInt('III')}');
    print('IV: ${romanToInt('IV')}');
    print('IX: ${romanToInt('IX')}');
    print('LVIII: ${romanToInt('LVIII')}');
    print('MCMXCIV: ${romanToInt('MCMXCIV')}');
}
 
int romanToInt(String roman)
{
    return roman.split('')
                .reversed
                .map((n) => romanNumbers[n])
                .reduce((curr, next) => curr > 2 * next ? curr - next : curr + next);
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎