Basic Language Features
Fundamentals
Control Flow
def convert_decimal_roman(number: int) -> str: try: number = int(number) except ValueError: print("Invalid Input, Value Error") return("Invalid Input") str = '' numDct = {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"} decimals = [] for key, value in numDct.items(): decimals.append(value * (number // key)) number %= key return "".join(decimals)
def convert_decimal_roman(number):- def convert_decimal_roman(number: int) -> str:
- try:
- number = int(number)
- except ValueError:
- print("Invalid Input, Value Error")
- return("Invalid Input")
- str = ''
- numDct = {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"}
- decimals = []
- for key, value in numDct.items():
str += value * (number // key)- decimals.append(value * (number // key))
- number %= key
return str- return "".join(decimals)
Basic Language Features
Fundamentals
Control Flow
Made the for-loop a little bit slimmer. Also += always creates new string. Join only ones and therefore better performance
def convert_decimal_roman(number): number = int(number) str = [] numDct = {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"} for key, value in numDct.items(): str.append(value * (number // key)) number = number % key return "".join(str)
- def convert_decimal_roman(number):
- number = int(number)
str = ""- str = []
- numDct = {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"}
for key in numDct:while number >= key:str += numDct[key]number -= keyreturn str- for key, value in numDct.items():
- str.append(value * (number // key))
- number = number % key
- return "".join(str)