-
Notifications
You must be signed in to change notification settings - Fork 1
Tricks
Tricks is a collection of functions that are used to help with the translation process. All methods convert inputs to lowercase before processing.
Requires: char
(any single character)
Returns: boolean
The isAVowel
method checks if the input character (only takes 1), is a vowel. It returns true if it is, and false if it is not.
Requires: char
(any single character)
Returns: boolean
The isARomanDigit
method checks if the input character (only takes 1), is a roman digit. It returns true if it is, and false if it is not.
Requires: char
(any single character)
Returns: int
(value of the digit)
The romanDigitToNumber
method takes a roman digit as input and returns the corresponding number. Possibl inputs are: I
, V
, X
, L
, C
, D
, M
. If the input is not a roman digit, the method returns 0.
Requires: string
(any string)
Returns: boolean
The onlyRomanDigits
method takes a string as input and returns true if the string only contains roman digits, and false if it contains any other characters.
Requires: string
(any string)
Returns: string
(modified string, only if I or J are first)
The switchFirstIorJ
method allows for I's and J's to be treated the same. If the first character is an I it is replaced with a J, and if the first character is a J it is replaced with an I. If the first letter is not I or J, the original string
is returned. If it is not, then the switched string
is returned.
Requires: string
(any string)
Returns: string
(modified string)
The switchUorV
method allows for U's and V's to be treated the same. If the string contains V's but no U's, all V's are replaced with U's. If the string contains U's and V's or only U's, all U's are replaced with V's. The method then returns the modified string.
Requires: string
(any string)
Returns: int
The evaluateRomanNumerals
method evaluates a Roman numeral string and returns its equivalent decimal value as a number. The method takes a single parameter, text
, which is the Roman numeral string to be evaluated.
The method starts by initializing a total
variable to 0 and converts the text
parameter to uppercase to ensure that all characters in the string are in uppercase for consistent processing. It also initializes a currentNumeral
variable with the length of the Roman numeral string minus 1.
The method checks if the Roman numeral string contains only valid Roman numerals by calling the onlyRomanDigits
method. If any invalid characters are found, the method returns 0.
The method then enters a while loop that evaluates each Roman numeral character in the string from right to left. It follows the standard rules of converting Roman numerals to decimal, adding the value of each numeral to the total
variable.
The method checks each numeral character's position and applies the appropriate rules for converting that numeral to decimal. For example, if the character is I
, it checks if it is legal in the ones position, and if so, it adds 1 to the total
variable. If the character is V
, it checks if it is legal in the ones position, and if so, it adds 5 to the total
variable.
If a numeral character is invalid for its position, the method returns 0. If a numeral character is legal but has already been counted too many times, the method returns 0.
After each numeral character is evaluated, the currentNumeral
variable is decremented to move to the next character in the string. If the currentNumeral
variable becomes less than 0, the loop exits, and the method returns the final total
value.
Roman numeral conversion rules
Numerals in a string are added: CC = 200 ; CCX = 210. One numeral to the left of a larger numeral is subtracted: IX = 9 ; XL = 40.
Subtract only a single letter from a single numeral. VIII for 8, not IIX; 19 is XIX, not IXX.
Subtract only powers of ten (I, X, or C, but not V or L or D). Not VL for 45, but XLV; not LD for 450, but CDL.
Don't subtract a number from one more than 10 times greater. Only subtract I from V or X; only X from L or C Not IL for 49, but XLIX; MIM is illegal.
Only if any numeral preceding is at least 10 times greater. Not VIX for 14, but XIV Not IIX, but VIII Only if any numeral following is smaller Not XCL for 140, but CXL
I
II
III
IIII
IV
V
VI
VII
VIII
VIII
IX
X
XX
XXX
XXXX
XL
L
LX
LXX
LXXX
LXXXX
XC