0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

自然数ローマ数字変換 Python3

Posted at

自然数とローマ数字の変換をします。

romanumber.py
#!/usr/bin/python3
import sys

TABINT=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
TABROMA=["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
ERRPAT=['DD','LL','VV', # 500,50,5 がダブらないように
        'CMC','CDC','XCX','XLX','IXI','IVI',
                  #CMCM,CMCのようなエラーパターンは'CMC'にまとめてある
        'DCD','LXL','VIV'] # 515のパターン

def toint(s):
  s=s.upper()
  for i in ERRPAT:
    if (i in s): return(-1)
  a=0
  i=0
  j=0
  while(s):
    if (s.startswith(TABROMA[i])):
      j=j+1
      a+=TABINT[i]
      s=s.replace(TABROMA[i],'',1)
      if (j<3):
        continue
    j=0
    i+=1
    if (i==len(TABROMA)):
      break
  if (s): # IF ILLEGAL FORMAT
    return(-1)
  return(a)

def toroman(a):
  s=""
  i=0
  while(a!=0):
    while((a//TABINT[i])!=0):
      s+=TABROMA[i]
      a-=TABINT[i]
    i+=1
  return s

print("自然数を入力して下さい:",end='')
a=int(input())
if (a>=4000):
  print("too big")
  sys.exit(1)
print(toroman(a))
print("ローマ数字を入力して下さい:",end='')
s=input()
i=toint(s)
if (i==-1):
  print("Invalid Roman Number")
  sys.exit(1)
print(i)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?