Code example:
func romanToDecimal(romeChar: String) { var result = 0 var maxValue = 0 let romeCh = romeChar.uppercased().replacingOccurrences(of: "\n", with: "", options: NSString.CompareOptions.literal, range: nil) let romeNumerals = ["M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1] for vp in romeNumerals { let val = vp.value let key = vp.key if (romeCh.range(of: key) != nil) { maxValue = max(val, maxValue) result += val == maxValue ? val : -val } } print(result) } The problem is that there is an incorrect calculus of the final result.
romeCh.range(of: key)probably checks for the presence of a digit, but does not take into account the number of digits and VIII is likely to be counted as 5 + 1 - MikeIshould give 1 according to this algorithm, ButIIIby the fact that I can see here, will also be equal to 1, because I don’t see where the number of digits would be considered here -