Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Convert number to roman numerals & convert back

Here's my situation: I have been doing a python programming course for 10 weeks and for the final week the teacher has set the hardest challenge yet and I have no idea how to do it. Any help is appreciated! So is spoon feeding!

Here is exactly what the teacher has set:

Roman Numerals: Roman numbers are defined by the letter I, V, X, L, C, D, M
representing 1, 5, 10, 50, 100, 500, 1000. Any number can be represented as sequence
of those letters. The position matter as IX is 9 or XI is 11 or XL is 40 or LX is 60.
The problem requires you to write two functions:
1) convertToRoman(nr) is to get a number as argument and convert it to a string
representing the Roman numer.
2) convertFromRoman(str) gets a roman number as a string and returns the
number.
How about you give it a go, then we guide you ;)
It's alot funner that way and wouldn't harm in trying! I certainly have ideas on how to do it :)
(Jan 29, 2015 01:41 PM)whitewoolyhat Wrote:  How about you give it a go, then we guide you ;)
It's alot funner that way and wouldn't harm in trying! I certainly have ideas on how to do it :)

I have tried it myself, and as far as I have got is associating each letter with its corresponding number, also have it using .isdigit() to see if its an int or str, and then go to the correct converter
Ok, when I can get on my laptop, I'll write out how I would do it :)
Just split apart the string and analyze what position each numeral is in, check if there's a higher numeral to the right or left to add or subtract.
Edit: Take a look at this: http://svn.python.org/projects/python/br...s/roman.py
according to wikipedia, there are actually only 6 subtractive cases, so you could easily do a simple find and replace for both methods. Additionally, in roman numerals you are supposed to treat each digit individually, so like 1954 would be seperated into M(1,000), CM(900), L(50), and IX(4), making MCMLIX. The subtractitive cases can be found here: http://en.wikipedia.org/wiki/Roman_numer...n_numerals.
Thanks for all the help guys, got it working in the end!


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Fivepoint0Oct 23, 2018 05:24 PM 3 193 Oct 23, 2018 05:24 PM
Last Post: Fivepoint0
[.NET] by mailen.doonMar 28, 2017 10:16 AM 3 377 Mar 28, 2017 10:16 AM
Last Post: mailen.doon
[Java] by TahhOct 17, 2016 11:24 AM 0 136 Oct 17, 2016 11:24 AM
Last Post: Tahh