1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(solutions): add roman-numerals/python/function

This commit is contained in:
Divlo 2021-06-30 15:03:59 +02:00
parent 213ae08fb3
commit ede3e7526b
No known key found for this signature in database
GPG Key ID: 185ED2F15F104E52
2 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# roman-numerals/python/function
Created by [@Divlo](https://github.com/Divlo) on 30 June 2021.

View File

@ -0,0 +1,53 @@
from typing import List
import sys
matches_roman_arabic = [
{'arabic': 1000, 'roman': 'M'},
{'arabic': 900, 'roman': 'CM'},
{'arabic': 500, 'roman': 'D'},
{'arabic': 400, 'roman': 'CD'},
{'arabic': 100, 'roman': 'C'},
{'arabic': 90, 'roman': 'XC'},
{'arabic': 50, 'roman': 'L'},
{'arabic': 40, 'roman': 'XL'},
{'arabic': 10, 'roman': 'X'},
{'arabic': 9, 'roman': 'IX'},
{'arabic': 5, 'roman': 'V'},
{'arabic': 4, 'roman': 'IV'},
{'arabic': 1, 'roman': 'I'}
]
def convert_arabic_to_roman(arabic_number: int) -> str:
roman_number: str = ''
for match in matches_roman_arabic:
while arabic_number >= match['arabic']:
roman_number = roman_number + match['roman']
arabic_number = arabic_number - match['arabic']
return roman_number
def convert_roman_to_arabic(roman_number: str) -> int:
arabic_number: int = 0
for match in matches_roman_arabic:
while True:
try:
if roman_number.index(match['roman']) == 0:
arabic_number = arabic_number + match['arabic']
roman_number = roman_number.replace(match['roman'], '', 1)
else:
break
except ValueError:
break
return arabic_number
input_values: List[str] = []
for value in sys.stdin:
input_values.append(value.rstrip('\n'))
convert_type = input_values[0]
if convert_type == 'arabic to roman':
print(convert_arabic_to_roman(int(input_values[1])))
else:
print(convert_roman_to_arabic(input_values[1]))