0
class Solution:
    def romanToInt(self, s: str) -> int:
        num = 0
        for i in range(len(s)):
            if s[i] == "I":
                try:
                    if s[i+1] == "V" or s[i+1] == "X":
                        num -= 1
                        continue
                except:
                    num += 1
                    continue
                else:
                    num += 1
                    continue
            elif s[i] == "X":
                try:
                    if s[i+1] == "L" or s[i+1] == "C":
                        num -= 10
                        continue
                except:
                    num += 10
                    continue
                else:
                    num += 10
                    continue
            elif s[i] == "C":
                try:
                    if s[i+1] == "D" or s[i+1] == "M":
                        num -= 100
                    continue
                except:
                    num += 100
                    continue
                else:
                    num += 100
                    continue
            elif s[i] == "V":
                num += 5
                continue
            elif s[i] == "L":
                num += 50
                continue
            elif s[i] == "D":
                num += 500
                continue
            else:
                num += 1000
                continue
        return num
    

               

This is obviously not the most optimal approach, but I want to understand what's wrong with it... I know this is wrong because here were the 3 testcases from Leetcode

"III" --> 3 worked "LVIII" ---> 58 worked

BUT "MCMXCIV" --> 1884 expected 1984

This code basically gives each iteration a specific amount that will be added/substracted from the totalsum. But somehow 100 was deducted and I dont get how...

  • The `else` corresponds to the `try`, not the `if`, so it doesn't run. (Also, the expected result should be 1994, not 1984.) – Karl Knechtel Jan 10 '23 at 05:48
  • Welcome to Stack Overflow. For future reference, please read [ask] and [mre] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/. One useful thing you can do before asking is to **check** what the code does, carefully, **step by step**, and figure out **when and where** that differs from your expectation. (You should **have** some expectation, of course, or else it is not possible to write code that makes sense.) – Karl Knechtel Jan 10 '23 at 05:50
  • Once you have identified parts of the program that have a problem, strip it down to those essentials - for example, what about a program that *only* tries to handle 'm', and 'c' symbols? – Karl Knechtel Jan 10 '23 at 05:52
  • @YashMehta when the question is "what's wrong with my code?", it should be closed according to the **problem that was encountered, not** the task that was attempted. The other question is not at all helpful for debugging this code; and if we interpret the question as "how do I convert roman numerals?" then that is too broad for Stack Overflow (close as Needs More Focus) as it becomes effectively a request to write code for a task. – Karl Knechtel Jan 10 '23 at 05:55

0 Answers0