Skip to content

Instantly share code, notes, and snippets.

@namchuai
Created April 24, 2020 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namchuai/f2059e3b07584891c43fedb60958b3f0 to your computer and use it in GitHub Desktop.
Save namchuai/f2059e3b07584891c43fedb60958b3f0 to your computer and use it in GitHub Desktop.
Roman converter
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