Python simply implements conversion between Arabic numerals and Roman numerals

不言
Release: 2023-03-24 07:44:02
Original
3097 people have browsed it

This article mainly introduces Python’s simple implementation of converting Arabic numerals and Roman numerals to and from each other, involving Python’s related operating skills for string and list traversal, operations, etc. Friends in need can refer to it

The example in this article describes how Python implements the conversion function between Arabic numerals and Roman numerals. Share it with everyone for your reference, the details are as follows:

The previous article introduced "Java Implementation of Solving the Problem of Converting Classic Roman Numerals and Arabic Numerals", here is a look at the implementation method in Python.

The question is very simple. If you have done this kind of question before, I believe you will not be unfamiliar with what Roman numerals are. Roman numerals are a very ancient counting method and are still seen in some places today. Its use, let’s simply post two pictures from Wikipedia to briefly review the Roman numerals:

Let’s simply implement it today, Arabic numerals The problem of mutual conversion between Roman numerals and Roman numerals is very simple, so I won’t go into details. The following is the specific implementation:

#!usr/bin/env python
#encoding:utf-8
'''''
__Author__:沂水寒城
功能:阿拉伯数字和罗马数字的互相转换
'''
def transform_alabo2_roman_num(one_num):
  '''''
  将阿拉伯数字转化为罗马数字
  '''
  num_list=[1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
  str_list=["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
  res=''
  for i in range(len(num_list)):
    while one_num>=num_list[i]:
      one_num-=num_list[i]
      res+=str_list[i]
  return res
def transform_roman_num2_alabo(one_str):
  '''''
  将罗马数字转化为阿拉伯数字
  '''
  define_dict={'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
  if one_str=='0':
    return 0
  else:
    res=0
    for i in range(0,len(one_str)):
      if i==0 or define_dict[one_str[i]]<=define_dict[one_str[i-1]]:
        res+=define_dict[one_str[i]]
      else:
        res+=define_dict[one_str[i]]-2*define_dict[one_str[i-1]]
    return res
    # #下面这种写法也可以
    # for i in range(len(one_str)):
    #   if i > 0 and define_dict[one_str[i]] > define_dict[one_str[i - 1]]:
    #     res -= define_dict[one_str[i - 1]]
    #     res += define_dict[one_str[i]] - define_dict[one_str[i - 1]]
    #   else:
    #     res += define_dict[one_str[i]]
    # return res
if __name__ == &#39;__main__&#39;:
  print &#39;**************将罗马数字转化为阿拉伯数字**************&#39;
  one_str_list=[&#39;DII&#39;,&#39;XV&#39;,&#39;MDCLXVI&#39;,&#39;XII&#39;,&#39;VIII&#39;,&#39;XCIX&#39;,&#39;XII&#39;]
  for one_str in one_str_list:
    print one_str,&#39;----->&#39;,transform_roman_num2_alabo(one_str)
  print &#39;**************将阿拉伯数字转化为罗马数字**************&#39;
  one_num_list=[77,66,55,8,1200,34,65,3,21,99]
  for one_num in one_num_list:
    print one_num,&#39;----->&#39;,transform_alabo2_roman_num(one_num)
Copy after login

The results are as follows:

Related recommendations:

Python implements Baidu speech recognition api

Python implements the method of solving the greatest common divisor

The above is the detailed content of Python simply implements conversion between Arabic numerals and Roman numerals. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!