0
$\begingroup$

I request you to please read this before marking as Duplicate.

I have referred to below:

After understanding, I have designed following algorithm to convert roman to decimal.

  • Let Roman String be ......
  • Replace string contents of length = 4 with appropriate content

    • Length = 4, Roman numerals are VIII = 8
  • Replace string contents of length = 3 with appropriate content

    • Length = 3, Roman numerals are MMM = 3000, XXX = 30, VII = 7, III = 3
  • Replace string contents of length = 2 with appropriate content
    • Length = 2, Roman numerals are MM = 2000, XX = 20, XI = 11, IX = 9, VI = 6, IV = 4, II = 2
  • Once this is done, then follow below rules:
    • Subtract digits from each other
      • I can be subtracted (absolute) from V or X
      • X can be subtracted (absolute) from L or C
      • C can be subtracted (absolute) from D or M
    • Finally there will be a bunch of digits. Add all of them to get the answer

My questions are :

  • Is the algorithm correct ?
  • I am planning to optimize the algorithm. Does anyone have any tips ?
$\endgroup$
  • $\begingroup$ Well, don't u think this question should be on Stackoverflow or Computer ScienceSE or some other place? $\endgroup$ – Mayank M. Aug 27 '16 at 16:20
  • $\begingroup$ It is on this because it has to do with math than to do with computer science. I want people who have knowledge of math to verify this. Do u still think I shud move it ? $\endgroup$ – SimpleGuy Aug 27 '16 at 17:41
  • $\begingroup$ Well, when you used the word "Algorithm" I thought about that. I'll comment again, let me think about it for a while. Okay, I just wanna know, do you want coding of this algorithm or we have to just find out flaws? $\endgroup$ – Mayank M. Aug 30 '16 at 14:50
  • $\begingroup$ If you need the flaws then I can tell you but even then a tint of programming will come here. That's why I recommended you that. Also would you please elaborate your algo. with an example. Some of your steps are absurd. $\endgroup$ – Mayank M. Aug 30 '16 at 14:56
  • $\begingroup$ @MayankM. I don't want you to tell me to about the tint of programming. This isn't a programming site and neither I am expecting you to tell me how to code. The term algorithm by definition is not a programming term. So, just because algo is used, it does not mean that it relates to programming. In simple words, it means solving something step by step. Do remember this. All I want, is, people to tell me if this is functionally correct or not. $\endgroup$ – SimpleGuy Sep 1 '16 at 8:41
0
$\begingroup$

I know very well what is an algorithm, and neither I'm interested in giving the codes on Math.SE.

If I'm not wrong in understanding your algo., in those steps of replacing you must keep in mind the precedence of replacement, for example, take $XIX$, you'll first replace XI (as you've typed) with 11, then subtract 10($X$), from 11, thus answer comes 1.

But if you add $XIX$ in the $length=3$ list then you may get the perfect answer.

But, even then, you cannot just write all possible exceptions, 'cause exceptions come due to lack of knowledge or due to improper steps of algorithm. And, due to that "precedence" I said to you that it has a "tint" (only a tint) of programming.

And, just to remove this small flaw($XIX$), you've to take in mind of grouping or at least the correct precedence of replacement.

Well, I found this link of your's very useful. They have the correct precedence.

[EDIT]
Algorithm for it can be recursive in nature and can go like this:
1) Start reading the characters from left.
2) If there is any value you come upon which is less than the earlier value then divide the expression in two, one for left side and one for right side.
3) Find value of left expression using your replace conditions, and repeat the steps for the right side.
4) Finally add all the values.

I checked it for $XIX$ and it works, provided you take the precedence of replacement same as in that link.

[EDIT2]
Well, I found that $V, L, D$ cannot be used as subtracting elements from here and BTW, our algorithms nearly match, why didn't you took a reference from there?

$\endgroup$
  • $\begingroup$ Thanks ! I will add the precedence part when replacing strings with roman in a same length category. For that, I will form the strings by reading chars from right to left $\endgroup$ – SimpleGuy Sep 2 '16 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.