gpt4 book ai didi

python - 检查输入是否为有效的罗马数字

转载 作者:行者123 更新时间:2023-11-28 20:46:21 26 4
gpt4 key购买 nike

我有一个程序可以将罗马数字转换为整数,反之亦然。我的问题是我真的不知道如何创建一个函数来检查用户输入是否是有效的罗马数字。我现在的代码:

def checkIfRomanNumeral(numeral):
"""Controls that the userinput only contains valid roman numerals"""
numeral = numeral.upper()
validRomanNumerals = ["M", "D", "C", "L", "X", "V", "I", "(", ")"]
for letters in numeral:
if letters not in validRomanNumerals:
print("Sorry that is not a valid roman numeral")
return True
elif letters in validRomanNumerals:
romanToInt(numeral)
break

我认为现在的问题是由于 for 循环,该函数只检查输入(数字)中的第一个字母。如果输入的任何字母不是罗马数字,有人可以帮我让函数检查整个输入并打印(“抱歉,这不是有效的罗马数字”)。列表 validRomanNumerals 中的括号用于转换大于 4000 的数字,因此它们必须存在。

最佳答案

编写一个从整数到罗马的转换器是一个标准的面试问题。我曾经写过以下双向实现(toString-- 十进制到罗马;parse -- 罗马到十进制)。该实现满足了一些关于罗马数字表示的附加标准,这些标准不是强制性的,但通常会遵循:

'''
Created on Feb 7, 2013

@author: olegs
'''

ROMAN_CONSTANTS = (
( "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" ),
( "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" ),
( "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" ),
( "", "M", "MM", "MMM", "", "", "-", "", "", "" ),
)

ROMAN_SYMBOL_MAP = dict(I=1, V=5, X=10, L=50, C=100, D=500, M=1000)

CUTOFF = 4000
BIG_DEC = 2900
BIG_ROMAN = "MMCM"
ROMAN_NOUGHT = "nulla"

def digits(num):
if num < 0:
raise Exception('range error: negative numbers not supported')
if num % 1 != 0.0:
raise Exception('floating point numbers not supported')
res = []
while num > 0:
res.append(num % 10)
num //= 10
return res

def toString(num, emptyZero=False):
if num < CUTOFF:
digitlist = digits(num)
if digitlist:
res = reversed([ ROMAN_CONSTANTS[order][digit] for order, digit in enumerate(digitlist) ])
return "".join(res)
else:
return "" if emptyZero else ROMAN_NOUGHT
else:
if num % 1 != 0.0:
raise Exception('floating point numbers not supported')
# For numbers over or equal the CUTOFF, the remainder of division by 2900
# is represented as above, prepended with the multiples of MMCM (2900 in Roman),
# which guarantees no more than 3 repetitive Ms.
return BIG_ROMAN * (num // BIG_DEC) + toString(num % BIG_DEC, emptyZero=True)

def parse(numeral):
numeral = numeral.upper()
result = 0
if numeral == ROMAN_NOUGHT.upper():
return result
lastVal = 0
lastCount = 0
subtraction = False
for symbol in numeral[::-1]:
value = ROMAN_SYMBOL_MAP.get(symbol)
if not value:
raise Exception('incorrect symbol')
if lastVal == 0:
lastCount = 1
lastVal = value
elif lastVal == value:
lastCount += 1
# exceptions
else:
result += (-1 if subtraction else 1) * lastVal * lastCount
subtraction = lastVal > value
lastCount = 1
lastVal = value
return result + (-1 if subtraction else 1) * lastVal * lastCount

关于python - 检查输入是否为有效的罗马数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com