Created
April 24, 2020 05:02
-
-
Save namchuai/f2059e3b07584891c43fedb60958b3f0 to your computer and use it in GitHub Desktop.
Roman converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
final VALUES = { | |
'I': 1, | |
'V': 5, | |
'X': 10, | |
'L': 50, | |
'C': 100, | |
'D': 500, | |
'M': 1000 | |
}; | |
// INPUT here | |
final ROMAN_INPUT = 'XXVII'; | |
List<int> convertedList = []; | |
ROMAN_INPUT.split('').forEach((ch) { | |
convertedList.add(VALUES[ch]); | |
}); | |
int result = 0; | |
int index = 0; | |
int length = convertedList.length; | |
while(index < length) { | |
int sign = 1; | |
if (index + 1 < length && convertedList[index] < convertedList[index+1]) { | |
sign = -1; | |
} | |
result+=sign*convertedList[index]; | |
index++; | |
} | |
print(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment