Advertisements
Advertisements
प्रश्न
Create a dictionary having decimal equivalent of roman numerals. Store it in a binary file. Write a function to convert roman number to decimal equivalent using the binary file data.
कोड लेखन
Advertisements
उत्तर
import pickle
roman_values = {
"I": 1, "V": 5, "X": 10, "L": 50,
"C": 100, "D": 500, "M": 1000
}
with open("roman.dat", "wb") as file:
pickle.dump(roman_values, file)
def roman_to_decimal(roman_number):
with open("roman.dat", "rb") as file:
values = pickle.load(file)
roman_number = roman_number.upper()
total = 0
previous = 0
for symbol in reversed(roman_number):
value = values[symbol]
if value < previous:
total -= value
else:
total += value
previous = value
return total
print(roman_to_decimal("XIV"))
Output: 14. The subtraction rule handles numerals such as IV and IX.
shaalaa.com
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
