我尝试构建一个可以轻松将Roman转换为Numeral的计算器 有人可以告诉我如何才能选择所有2个字符的高优先级?
** roman是一个字符串
#Roman Numeral calculator
def RomanToDecimal(roman):
DecimalValue=0
for i in roman:
if i == "CM":
DecimalValue += 900
if i == "IV":
DecimalValue += 4
if i == "IX":
DecimalValue += 9
if i == "XL":
DecimalValue += 40
if i == "XC":
DecimalValue += 90
if i == "CD":
DecimalValue += 400
elif i in roman:
if i == "I":
DecimalValue += 1
if i == "V":
DecimalValue += 5
if i == "X":
DecimalValue += 10
if i == "L":
DecimalValue += 50
if i == "C":
DecimalValue += 100
if i == "D":
DecimalValue += 500
if i == "M":
DecimalValue += 1000
return DecimalValue
我是新来的,所以请耐心地回答我
答案 0 :(得分:4)
我建议先将罗马语翻译成阿拉伯语,然后将其中的一些乘以-1,最后加总:
roman = 'MMXIV'
D = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
tmp = [D[x] for x in roman]
for i in range(len(tmp) - 1):
if tmp[i + 1] > tmp[i]:
tmp[i] *= -1
print(sum(tmp))
答案 1 :(得分:0)
虽然有more efficient ways来执行此操作,但这遵循您自己的代码的样式,因此希望更容易遵循。
主要的变化是用for
循环和手动计数器替换while
循环。当我们遇到一个双字符时,这可以跳过两个步骤。接下来,我们提取字符并检查它们 - 首先向前看两个字符, next 单独查找。如果我们找到2个字符长的匹配,我们前进2(经过它们)并重新启动循环:
def RomanToDecimal(roman):
DecimalValue=0
i = 0 # Position in string
while i < len(roman):
# Test for 2 char first
if i < len(roman)-1: # Make sure we're not on the last letter
# Get current + next letters
s = roman[i:i+2]
add = False
if s == "CM":
add = 900
elif s == "IV":
add = 4
elif s == "IX":
add = 9
elif s == "XL":
add = 40
elif s == "XC":
add = 90
elif s == "CD":
add = 400
if add: # We've found a match
i += 2 # Step 2
DecimalValue += add # Add the value
continue # Next loop
# If we get here the 2 character match failed
# Single char match
s = roman[i]
if s == "I":
DecimalValue += 1
elif s == "V":
DecimalValue += 5
elif s == "X":
DecimalValue += 10
elif s == "L":
DecimalValue += 50
elif s == "C":
DecimalValue += 100
elif s == "D":
DecimalValue += 500
elif s == "M":
DecimalValue += 1000
i += 1
return DecimalValue
RomanToDecimal('XIXIX')
28
应该注意的是,上面的确实是一个有效的罗马数字字符串(28正确地由XXVIII表示),但希望这不是问题!
如果代码在任何地方都不清楚,请告诉我,我会添加更多评论。
答案 2 :(得分:0)
经典问题在罗塞塔代码中有经典答案:http://rosettacode.org/wiki/Roman_numerals/Decode#Python
_rdecode = dict(zip('MDCLXVI', (1000, 500, 100, 50, 10, 5, 1)))
def decode( roman ):
result = 0
for r, r1 in zip(roman, roman[1:]):
rd, rd1 = _rdecode[r], _rdecode[r1]
result += -rd if rd < rd1 else rd
return result + _rdecode[roman[-1]]
if __name__ == '__main__':
for r in 'MCMXC MMVIII MDCLXVI'.split():
print( r, decode(r) )