Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Name: Mark Jason T. Galang
- #School: FEU-TECH
- #Machine Problem number - 3
- class RomanNumerals:
- def __init__(self):
- # constructor
- self.res1 = ""
- self.res2 = 0
- def convert_to_roman(self, num):
- """
- this function converts an integer to roman numerals
- """
- # roman numeral symbols
- thousands = ["M", "MM", "MMM", "MMMM", "V̅"]
- hundreds = ["C", "CC", "CCC", "CD", "D" ,"DC", "DCC", "DCCC", "CM"]
- tens = ["X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
- ones = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
- # conversion using recursion
- # process: divide the number by its place value
- # and store the corresponding roman numeral symbols
- # using the (leading digit - 1) (0-based)
- if num >= 1000:
- self.res1 += thousands[(num // 1000) - 1]
- if num % 1000 != 0:
- return self.convert_to_roman(num % 1000)
- else:
- return self.res1
- elif num >= 100:
- self.res1 += hundreds[(num // 100) - 1]
- if num % 100 != 0:
- return self.convert_to_roman(num % 100)
- else:
- return self.res1
- elif num >= 10:
- self.res1 += tens[(num // 10) - 1]
- if num % 10 != 0:
- return self.convert_to_roman(num % 10)
- else:
- return self.res1
- else:
- self.res1 += ones[num - 1]
- return self.res1
- def convert_to_number(self, rom):
- """
- this function converts roman numerals to an integer
- """
- # roman numeral symbols and their equivalents
- thousands = {"M":1000, "MM":2000, "MMM":3000, "MMMM":4000, "V̅":5000}
- hundreds = {"C":100, "CC":200, "CCC":300, "CD":400, "D":500, "DC":600, "DCC":700, "DCCC":800, "CM":900}
- tens = {"X":10, "XX":20, "XXX":30, "XL":40, "L":50, "LX":60, "LXX":70, "LXXX":80, "XC":90}
- ones = {"I":1, "II":2, "III":3, "IV":4, "V":5, "VI":6, "VII":7, "VIII":8, "IX":9}
- # reverse the dictionaries (using list technique)
- thousands = dict(reversed(list(thousands.items())))
- hundreds = dict(reversed(list(hundreds.items())))
- tens = dict(reversed(list(tens.items())))
- ones = dict(reversed(list(ones.items())))
- # conversion (linear-search)
- # for thousands place
- if len(rom) > 0:
- if rom[0] == "M":
- for th in thousands:
- if th in rom:
- self.res2 += thousands[th]
- rom = rom[len(th):]
- break
- # for hundreds place
- if len(rom) > 0:
- if rom[0] == "C" or rom[0] == "D":
- for h in hundreds:
- if h in rom and h != "D":
- self.res2 += hundreds[h]
- rom = rom[len(h):]
- break
- if len(rom) > 0:
- if "D" in rom:
- self.res2 += hundreds["D"]
- rom = rom[1:]
- # for tens place
- if len(rom) > 0:
- if rom[0] == "X" or rom[0] == "L":
- for t in tens:
- if t in rom and t != "L":
- self.res2 += tens[t]
- rom = rom[len(t):]
- break
- if len(rom) > 0:
- if "L" in rom:
- self.res2 += tens["L"]
- rom = rom[1:]
- # for ones place
- if len(rom) > 0:
- if rom[0] == "I" or rom[0] == "V":
- for o in ones:
- if o in rom and o != "V":
- self.res2 += ones[o]
- rom = rom[len(o):]
- break
- if len(rom) > 0:
- if "V" in rom:
- self.res2 += ones["V"]
- rom = rom[1:]
- return (self.res2 if len(rom) == 0 else 0)
- def main():
- while True:
- # menu
- print("MENU\n")
- print("1. convert an integer to a roman numeral")
- print("2. convert a roman numeral to an integer")
- print("3. exit\n")
- # menu - input with error-handling
- ch = eval(input("Enter your choice:"))
- if ch < 1 or ch > 3:
- print("INVALID INPUT: Select from 1 to 3 only.\n")
- continue
- elif type(ch) != int:
- print("INVALID INPUT: Whole number only.\n")
- continue
- if ch == 1:
- # two (2) cases for both input and output
- inp1 = "Enter Integer - "
- inp2 = "EnterInteger - "
- output1 = "Output in Roman numerals is: {0}\n"
- output2 = "Output in Roman Numberals is {0}\n"
- for i in range(2):
- # choice 1 - input with error handling
- n = 0
- while True:
- n = eval(input(inp1 if (i + 1) & 1 else inp2))
- if n <= 0 or n > 5000:
- print("INVALID INPUT: Select from 1 to 5000 only.\n")
- continue
- elif type(n) != int:
- print("INVALID INPUT: Whole number only.\n")
- continue
- else:
- break
- # object instantiation
- rn = RomanNumerals()
- # output
- ans = rn.convert_to_roman(n)
- print(output1.format(ans) if (i + 1) & 1 else output2.format(ans))
- elif ch == 2:
- # two (2) cases for both input and output
- inp1 = "Enter roman numeral - "
- inp2 = "Enter roman numberals - "
- output1 = "Output in Integer is - {0}\n"
- output2 = "Output in Integer is - {0}\n"
- for i in range(2):
- rom = ""
- while True:
- # choice 2 - input with error handling
- rom = str(input(inp1 if (i + 1) & 1 else inp2))
- rom = rom.upper()
- # object instantiation
- rn = RomanNumerals()
- ans = rn.convert_to_number(rom)
- # output with error-handling
- if ans != 0:
- print(output1.format(ans) if (i + 1) & 1 else output2.format(ans))
- break
- else:
- print("INVALID INPUT: Please try again.\n")
- continue
- else:
- print("Program Exiting...")
- break
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement