"for i in X" 罗马数字计算器
Roman to Numeral calculator with "for i in X"
我试图构建一个可以轻松将罗马转换为数字的计算器
有人能告诉我如何选择所有 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
我是新来的所以请耐心回答我
我建议先将罗马语逐一翻译成阿拉伯语,然后将其中一些乘以 -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))
虽然有 可以执行此操作,但它遵循您自己的代码风格,因此希望更容易理解。
主要更改是用 while
循环和手动计数器替换 for
循环。这使得当我们遇到 double-chars 之一时可以向前跳过两步。接下来我们提取字符并检查它们——first 向前看两个字符,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),但希望这不是问题!
如果代码有任何不清楚的地方,请告诉我,我会添加更多评论。
Rosetta Code经典问题有经典答案: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) )
我试图构建一个可以轻松将罗马转换为数字的计算器 有人能告诉我如何选择所有 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
我是新来的所以请耐心回答我
我建议先将罗马语逐一翻译成阿拉伯语,然后将其中一些乘以 -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))
虽然有
主要更改是用 while
循环和手动计数器替换 for
循环。这使得当我们遇到 double-chars 之一时可以向前跳过两步。接下来我们提取字符并检查它们——first 向前看两个字符,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),但希望这不是问题!
如果代码有任何不清楚的地方,请告诉我,我会添加更多评论。
Rosetta Code经典问题有经典答案: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) )