简体   繁体   English

使用 Python 编程编写将给定整数转换为罗马数字的函数

[英]Writing a function to convert a given integer into a roman numeral using Python programming

I'm doing some coding exercises and tried to research how to write a function to convert a given integer into a roman numeral.我正在做一些编码练习,并试图研究如何编写一个函数来将给定的整数转换为罗马数字。 Below is the code I have written:下面是我写的代码:

roman_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC'), (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), (1, 'I')] roman_map = [(1000, 'M'), (900, 'CM'), (500, 'D'), (400, 'CD'), (100, 'C'), (90, 'XC') , (50, 'L'), (40, 'XL'), (10, 'X'), (9, 'IX'), (5, 'V'), (4, 'IV'), ( 1, '我')]

def solution(n):
    
    roman = '' #Set the variable roman to empty
    
    while n>0:  #While the entered integer is greater than 0
        for key,value in roman_map: #loop through every key value in the roman numeral map
            while n>=key: #while the entered integer is more than or equal to the key
                roman+=value #Take the value of that key and add it to your empty roman numeral set
                n-=key #Deduct the key from the entered integer
                
    return roman

I tried understanding the logic used on the code and understood the following:我尝试理解代码中使用的逻辑并理解以下内容:

  1. Take an integer 1取一个整数 1
  2. As long as 1 is greater than 0, loop through every key value pair in the roman numeral map只要1大于0,循环遍历罗马数字映射中的每个键值对
  3. As long as the integer is greater than or equal to the key, take the value of that key and add it to the empty variable 'roman'.只要整数大于或等于键,就取该键的值并将其添加到空变量 'roman' 中。

I'm lost understanding the last step which is why does the key have to be then deducted from the entered integer.我对最后一步失去了理解,这就是为什么必须从输入的整数中扣除密钥。 Does that remove that key value pair from the map after it has been recorded?是否会在记录后从地图中删除该键值对? Can someone please help me understand this logic有人可以帮我理解这个逻辑吗

You're simply adding each key as many times as needed.您只需根据需要多次添加每个键。 You do not alter your reference list roman map .您不会更改您的参考列表roman map Step 3 is generic, so that you can handle the repeated letters in something such as 1234. That inner while loop will add M , then C twice, then X three times and finally IV .第 3 步是通用的,因此您可以处理诸如 1234 之类的重复字母。该内部while循环将添加M ,然后是C两次,然后是X三次,最后是IV

If you want to understand the code more fully, perform your due diligence: insert a couple of strategic print statements to trace the data and execution.如果您想更全面地理解代码,请执行尽职调查:插入几个战略性print语句来跟踪数据和执行。 For instance例如

print(key, value, n, roman)

should give you a good trace of what's going on.应该给你一个很好的跟踪发生了什么。

The last step being n-=key , no this does not remove anything from the dict roman_map .最后一步是n-=key ,不,这不会从dict roman_map删除任何dict roman_map What it does is to subtract from n , the amount for which a Roman part was found in the loop just completed.它所做的是从n减去在刚刚完成的循环中找到罗马部分的数量。

Day the Integer is 1001. The code will (in short), will check for {1000: 'M'} , add M to roman .整数日为 1001。代码将(简而言之)检查{1000: 'M'} ,将M添加到roman Now it must subtract the amount key (1000) from n , leaving 1 , which will eventually be processed later in the for loop, at which point it will append I to the string roman .现在它必须从n减去数量key (1000) ,留下1 ,最终将在 for 循环中稍后处理,此时它将I附加到字符串roman

Overall, each time the code finds a roman numeral to add, it must subtract the amount it represents from the integer.总的来说,每次代码找到要添加的罗马数字时,它必须从整数中减去它所代表的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM