![]() |
|
#1
|
|||
|
|||
Roman numeral to decimalI have a project in which I have to convert roman numerals to arabic characters. I can only get it to read the numeral on the right and it won't loop anyone have any way of helping me fix this.
Python Code:
Thanks Last edited by admin : 03-Apr-2009 at 17:32.
Reason: Please insert your example Python codes between [PY] and [/PY] tags
|
|||
|
#2
|
||||
|
||||
Re: Roman numeral to decimalIt's not looping because you don't have any loop statements in it. All you have are ifs, which are only processed one time unless enclosed within a loop.
You also have length and i being created and assigned values twice. Did you mean to do this on purpose? I haven't ran the code but fixing the loop is the biggest thing I found. Without a loop, the program will run once then quit. This is probably why it only does the rightmost numeral. You may want to slice the Roman number and add up the values that way. Looping through the number, getting a different slice value each time, may be easier to work with. Just a suggestion. __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#3
|
|||
|
|||
Re: Roman numeral to decimalI hadn't put the while loop bacause I'm trying to get it to work correctly first before putting the loop in it. Yes I did mean to have the lenght twice, should I not have it, would there be any problems in getting the code to run properly like that. How do I do the slicing part and how do I get it to add the old value ot the new value like:
IV <--- how do I get it to subtract 1 from 5 VI <--- how do I get it to add 1 to 5 Thanks, I'm pretty new and still trying to learn how to program |
|
#4
|
||||
|
||||
Re: Roman numeral to decimalUnless you have to write your program using if statements, I would recommend using regular expressions. You can write a reasonably short regex expression that will parse through the Roman number and give you the values of each number.
Take a look at Dive Into Python; it has a chapter talking about regular expressions and it also talks about Roman numeral conversion. __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#5
|
|||
|
|||
Re: Roman numeral to decimalWell this is what I've done so far. I looked at he dive into python book but I got lost and have no clue what to do. I tried indexing it the comments are in there but i couldn't get it work. With the way it is now it at least gives me some basic values like 1 , 5 etc but that's about it.
These are the rules I must follow: The valid letters and rules for creating and reading valid Roman Numerals (up to 3999) are as follows: I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000 1) A letter repeats its value that many times (XXX = 30, CC = 200, etc.). A letter can only be repeated three times. 2) If one or more letters are placed after another letter of greater value, add that amount. Examples: a. VI = 6 (5 + 1 = 6) b. LXX = 70 (50 + 10 + 10 = 70) c. MCC = 1200 (1000 + 100 + 100 = 1200) 3) If a letter is placed before a letter of greater values, subtract that amount. Further Rules: a. Subtract only powers of ten, such as I, X, or C. Writing VL for 45 is not allowed: write XLV instead. b. Subtract only a single letter from a single numeral. Write VIII for 8, not IIX; 19 is XIX, not IXX. c. Don't subtract a letter from another letter more than ten times greater. This means that you can only subtract I from V or X, and X from L or C, so MIM is illegal. Python Code:
Thanks Last edited by LuciWiz : 05-Apr-2009 at 15:11.
Reason: Please insert your Python code between [py] & [/py] tags
|
|
#6
|
||||
|
||||
Re: Roman numeral to decimalWell, you need to read in the whole value, instead of just looking at the rightmost. If you just start with the rightmost number, you won't know if it's part of a larger value, e.g. VI instead of just I.
Also, starting from the left would make more sense because Roman numbers subtract from the left, e.g. IV =4. Starting from the right won't catch those cases. You should consider making a mapping of valid roman numerals to their respective decimal values, like this: Python Code:
I have a suspicion that, continuing on with your methodology, it would devolve into creating an if statement for every single possible combination. __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another Roman numeral conversion problem. | hoodedpython | C++ Forum | 19 | 10-Dec-2007 02:18 |
| Roman numerals to decimal | ShamanKingCpp | C++ Forum | 8 | 14-Dec-2006 07:25 |
| help writing a roman numeral to decimal program | Ichigo | C++ Forum | 3 | 23-Mar-2006 15:05 |
| Frustration...Roman Numeral Program | SpyD3r | C++ Forum | 14 | 13-Nov-2005 21:02 |
| Roman to decimal to roman | SpudNuts | C++ Forum | 2 | 16-Feb-2005 20:44 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The