栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在python中将罗马数字转换为整数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在python中将罗马数字转换为整数

考虑一下此附加的伪代码和提示(其中一些是有效的Python,一些不是有效的,但有注释)。

def numberOfNumeral(n):    """ Return the number represented by the single numeral """    # e.g. "v" -> 5, "i" -> 5 (and handle v/V cases, etc.)# avoid "string" as a variable name# I chose "ns" for "numerals" (which might be better),# but I'm also a bit terse .. anyway, name variables for what they represents.ns = str(input("Enter a roman numeral"))while ns:   firstNum = numberOfNumeral(ns[0])   # This makes secondValue = -1 when there is only one numeral left   # so firstNum is always "at least" secondNum when len(ns) == 1.    secondNum = numberOfNumeral(ns[1]) if len(ns) > 1 else -1   if firstNum is at least secondNum:      # Add firstNum to total.      # Remove the character - so that the loop state advances.      # If we don't don't his, as in the original, it will never end.      # Here we use "slice notation".      ns = ns[1:]    else:      # Add the difference, secondNum - firstNum, to total.      # Remove both characters - again, so we advance state.      ns = ns[2:]


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/639069.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号