python - 该程序没有正确比较两个数字
问题描述
比较运算符无法正常工作
class Solution:
def romanToInt(self, s: str) -> int:
dic = {"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
result = 0
s = list(s)
while True:
if len(s) == 1:
x = s.pop(0)
result+=dic[x]
break
elif len(s) == 0:
break
else:
比较运算符无法正常工作
if (dic[s[0]] < dic[s[1]]) == True: #gives true for 1000<10
result += dic[s[1]] - dic[s[0]]
s.pop(0)
s.pop(0)
直接跳到这里
elif (dic[s[0]] >= dic[s[1]] )== True:
result += dic[s[1]] + dic[s[0]]
s.pop(0)
s.pop(0)
return result
o = Solution()
print(o.romanToInt("MCMXCIV"))
解决方案
推荐阅读
- docker - 来自守护进程的错误响应:Mount denied - 运行昨晚工作的 docker 应用程序时出错
- javascript - 如何在 JavaScript 中平滑更改背景(动画)?
- python - 用于 NLP 的 Python 知识图
- python - print 函数如何在函数内部工作?
- virtualization - SLAT 在 HV 中可以避免吗?
- algolia - Algolia Places API:如何获取某个地方的街道?
- python - 使用 numpy 数组加速 pandas 数据帧中的迭代
- javascript - 在实例对象内的函数中获取类(this)的实例 - 打字稿/角度
- sql - 值未更新但显示受影响的行
- excel - Excel 设置和公式