Created with <3 with dartpad.dev.
Last active
June 29, 2023 02:35
-
-
Save tomasbaran/adb39a3b66ed243f8236ce366a33f606 to your computer and use it in GitHub Desktop.
new-test
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 { | |
int romanCharToInt(String romanChar) { | |
switch (romanChar) { | |
case 'I': | |
return 1; | |
case 'V': | |
return 5; | |
case 'X': | |
return 10; | |
case 'L': | |
return 50; | |
case 'C': | |
return 100; | |
case 'D': | |
return 500; | |
case 'M': | |
return 1000; | |
} | |
return 0; | |
} | |
int romanToDecimal(String romanNumber) { | |
int decimalValue = 0; | |
for (int i = 0; i < romanNumber.length; i++) { | |
int firstValue = romanCharToInt(romanNumber[i]); | |
int secondValue = | |
romanCharToInt(romanNumber[i + 1 == romanNumber.length ? i : i + 1]); | |
if (firstValue < secondValue) { | |
decimalValue -= firstValue; | |
} else { | |
decimalValue += firstValue; | |
} | |
} | |
return decimalValue; | |
} | |
} | |
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