Skip to content

Instantly share code, notes, and snippets.

@tomasbaran
Created June 28, 2023 22:20
Show Gist options
  • Save tomasbaran/e44a93a7ac83421bfb83adad83bdce4b to your computer and use it in GitHub Desktop.
Save tomasbaran/e44a93a7ac83421bfb83adad83bdce4b to your computer and use it in GitHub Desktop.
Amco challenge: romanToDecimalNumber
class RomanNumbers {
static int romanToDecimal(String romanNumber) {
Map<String, int> romanMap = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000,
};
int decimalValue = 0;
for (int i = 0; i < romanNumber.length; i++) {
int currentValue = romanMap[romanNumber[i]] ?? 0;
int nextValue = (i + 1 < romanNumber.length)
? romanMap[romanNumber[i + 1]] ?? 0
: 0;
if (currentValue < nextValue) {
decimalValue -= currentValue;
} else {
decimalValue += currentValue;
}
}
return decimalValue;
}
}
void main() {
int value = RomanNumbers.romanToDecimal("XLIV");
if (value == 44) {
print("Success!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment