Skip to content

Instantly share code, notes, and snippets.

@tomasbaran
Last active June 29, 2023 02:35
Show Gist options
  • Save tomasbaran/adb39a3b66ed243f8236ce366a33f606 to your computer and use it in GitHub Desktop.
Save tomasbaran/adb39a3b66ed243f8236ce366a33f606 to your computer and use it in GitHub Desktop.
new-test
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