Check if an input is a valid roman numeral

后端 未结 5 610
醉话见心
醉话见心 2021-01-14 10:46

I got a program that converts Roman numerals to integers and vice versa. My problem is that I don´t really know how to create a function that checks if the user input is a v

5条回答
  •  终归单人心
    2021-01-14 11:05

    Instead of looping, you can convert both input and valid literals to sets and then substract them:

    def checkIfRomanNumeral(numeral):
        numeral = {c for c in numeral.upper()}
        validRomanNumerals = {c for c in "MDCLXVI()"}
        return not numeral - validRomanNumerals
    

    Returns True if numeral is valid, False otherwise. (Assuming that the empty string is valid)

提交回复
热议问题