Hello everyone, 👋, let's delve into the world of problem-solving. Thank you for stopping by the channel.
I will take you in my own journey to solve today's problem using python.
this case was one of the tasks in ALX project 0x04-python-more_data_structures
Introduction
- the problem statement: create a function that convert Roman numeral to an integer.
Understanding Roman numeral:
Roman numerals are a numeral system that originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the Late Middle Ages. Numbers are written with combinations of letters from the Latin alphabet, each letter with a fixed integer value. Modern style uses only these seven:
roman_numeral = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
Approach to Problem Solving
Before starting its very important to whiteboard the design of the solution.
Introduce the problem-solving approach: understanding requirements, designing algorithms, and implementing solutions.
Desired output:
guillaume@ubuntu:~/0x04$ cat 12-main.py
#!/usr/bin/python3
""" Roman to Integer test file
"""
roman_to_int = __import__('12-roman_to_int').roman_to_int
roman_number = "X"
print("{} = {}".format(roman_number, roman_to_int(roman_number)))
roman_number = "VII"
print("{} = {}".format(roman_number, roman_to_int(roman_number)))
roman_number = "IX"
print("{} = {}".format(roman_number, roman_to_int(roman_number)))
roman_number = "LXXXVII"
print("{} = {}".format(roman_number, roman_to_int(roman_number)))
roman_number = "DCCVII"
print("{} = {}".format(roman_number, roman_to_int(roman_number)))
guillaume@ubuntu:~/0x04$ ./12-main.py
X = 10
VII = 7
IX = 9
LXXXVII = 87
DCCVII = 707
guillaume@ubuntu:~/0x04$
Solution one (Failed)
- firstly, i try to design algorithm that map each item in the
roman_string
in order to get the value related from theroman_numeral
which is the map for Coverting the numeral string characters into integers.
res = [roman_numeral.get(i) for i in roman_string]
- The previous task done successfully without any error but there is a challenge where each time the letter
I
exists before the letterX
it must be converted to-1
not1
.
In order to solve this challenge I try to iterate over the list res
in order to take every two items next to each other and compare them so if the first char is I
and the second is X
treat the I
as -1
and append its value to anew list res2
.
res = [roman_numeral.get(i) for i in roman_string]
res2[]
for a, b in zip(res,res[1:]):
print(f"a ={a}, b={b}")
if a == 1 & a < b :
res2.append(a*-1)
res2.append(b)
else:
res2.append(a)
- unfortunately, this solution will fail as the iteration will go out of index range if the characters count of the input roman string is even number.
Solution two (succeeded)
- firstly, i try to design algorithm that map each item in the
roman_string
in order to get the value related from theroman_numeral
which is the map for Coverting the numeral string characters into integers.
res = [roman_numeral.get(i) for i in roman_string]
The previous task done successfully without any error but there is a challenge where each time the letter
I
exists before the letterX
it must be converted to-1
not1
.I think that if i can loop forward in the
roman_string
until i found the charX
then reverse the list to loop backward and if i face anyI
char i convert it to -1 not 1.
res = [roman_n.get(i) for i in roman_string]
i = 0
res2 = []
for i in res:
res2.append(i)
if (i == 10) or (i == 5):
for x in reversed(res2):
if x == 1:
res2.remove(x)
res2.append(-1)
- So we almost reach to solve the problem but still one final challenge which is this case
XCIX -> must be 99
and with that code the result will be 109 so we will replay the same logic mentioned before to solve that case as following:
res = [roman_n.get(i) for i in roman_string]
i = 0
res2 = []
for i in res:
res2.append(i)
if (i == 10) or (i == 5):
for x in reversed(res2):
if x == 1:
res2.remove(x)
res2.append(-1)
if (i == 100):
for x in reversed(res2):
if x == 10:
res2.remove(x)
res2.append(-10)
- Fortunately, this solution succeeded 💪✌
- Here is the whole code snippet for the solution:
🌹🌹We appreciate you, our dear reader, for diving into this article on software development problem-solving. Your interest and engagement are invaluable to us. We hope you found the insights here helpful and motivating. Remember, problem-solving is more than a skill; it's a mindset that helps us tackle challenges and innovate. Keep honing your abilities and join us on this path of learning and progress. Thank you for being part of our community! 🌹🌹