Created
June 28, 2023 22:20
-
-
Save tomasbaran/e44a93a7ac83421bfb83adad83bdce4b to your computer and use it in GitHub Desktop.
Amco challenge: romanToDecimalNumber
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
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