Avatar of HukkaHua
HukkaHua

asked on 

Roman Numeral to Decimal : Interview Question

Folks,
         I need some code or the algorithm to convert a roman numerical to the
corresponding decimal number ?

This is an interview question and not any homework.

Thanks,
H
AlgorithmsProgrammingC

Avatar of undefined
Last Comment
Venabili
Avatar of josgood
josgood
Flag of United States of America image

Avatar of josgood
josgood
Flag of United States of America image

This C project from Sourceforge looks like it may help you
Avatar of HukkaHua
HukkaHua

ASKER

Which C project from Sourceforge are you talking about ?

Thanks,
H
ASKER CERTIFIED SOLUTION
Avatar of harfang
harfang
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of shanikawm
shanikawm
Flag of Sri Lanka image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of josgood
josgood
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi harfang,

Great start, but it's not that simple.

VIM is 6,000
IVM is 4,000

   :)


Kent
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> VIM is 6,000
>> IVM is 4,000

That depends on the notation you use. The Romans themselves used MMMMMM resp. MMMM.
In the Middle Ages, A horizontal line was placed above VI resp. IV to denote times 1000.
I've never seen your notation though ... It's quite ambiguous, as it could mean 994 resp. 996.
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


Roman Numeral notation had its oddities.  Hence the shift to the decimal system.  (If people didn't have 10 fingers, I wonder what mathematics would look like today?)

I recall different instructors having different ideas on so-called corner cases.

One swore that IL was proper for 49, another swore that that was shorthand and the additive nature of Roman Numbers dictated that XLIX was the "true" notation.

I don't remember ever seeing 49 represented as XLIX, except in theory.  On the other hand, that does support the claim that VIM is RN for 6,000.


Kent
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> that does support the claim that VIM is RN for 6,000.

That would require multiplication, no ? 6 * 1000. That's what I've never seen in Roman numerals.
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> (And to math geeks, it's kind of interesting reading.)

It is indeed. Nice link.
SOLUTION
Avatar of harfang
harfang
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of harfang
harfang
Flag of Switzerland image

Kent,

Sorry, I left the browser open for too long, so my post was written before I saw your link (thank you for it, it is interesting!). I'm glad I wrote "I'm ready to believe [...]", in retrospect. Also, I was happy to read: "it's more likely that someone writing MXMI intends 1991, rather than 1010001", because that fits my experience: books written after 1753, erm mdccliii...

(°v°)
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi harfang,

That has to be just about the most lucid, well thought out, and articulate follow-up post I've seen on this forum.

Can we bronze it?   :)

Avatar of harfang
harfang
Flag of Switzerland image

Thank you for your kind words! -- (^v°)
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

"If people didn't have 10 fingers, I wonder what mathematics would look like today?"

If we only had 2 fingers, the everything would be in BINARY.

There is nothing special about base 10.  After all, many computers work in Hexa-decimal (base 16, 0123456789ABCDEF), or Binary (base 2, 01).

Just remember,

There are precisely 10 kinds of people in this world:

     Those who understand Binary Numbers, and those who do not.

;-)

AW
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Hi Arthur,

There's really two things at work here.  If people had 4 fingers instead of 5, our mathematics would likely be base 8, not decimal.  (Of course, base 10 would be just another abstraction so we'd likely call base 8, decimal.)

The other is that computers aren't really hexadecimal.  They're a collection of some number of binary digits.  A hex machine collects 4 bits.  An octal machine collects 3 bits.  And hex vs. octal is really a display function, based on how best to split a byte.

Now think about this.  If the early computer designers had discovered a 3-state entity instead of standardizing on the 2-state (binary) on/off, they'd have achieved a 50% performance gain by using the 3-state object.  If so, computers today would be a derivative of base 3.  A byte would be base 9, and a word would be 81 bits.

Instead, the biggest arguments were big-endian or little endian, and 1's complement or 2's complement.

Of course, they could still have argued both of them in addition to an 81 bit word, huh?  :)


Kent
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Actually, we would (and do) call base 8 Octal (Decimal is from the Latin word for 10, as Octal is from the Latin word for 8)

Of course, I know that computers only operate on bits (1s and 0s), I was just making a point about the use of different bases.

Of course, there is always the issue of the common use of base 12 numbers (eggs in a Dozen, hours of Day vs night), or base 60 - minutes in an hour, seconds in a minute, basis of the number of degrees in a circle.  So the argument could be made that the use of base 10, and the fact that we have 10 fingers is purely coincidental.

And how (or why) did the romans devise the I, V, X, L, C, D, M system in the first place?

AW

Avatar of HukkaHua
HukkaHua

ASKER

Ok Guys may you please throw in some code as well which would work or give some
pseudo code /algorithm ...

Thanks
H
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of Jose Parrot
Jose Parrot
Flag of Brazil image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
SOLUTION
Avatar of harfang
harfang
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of HukkaHua
HukkaHua

ASKER

I guess the solution posted by harfang is the one :)
But I do not know VB :(, but I am trying to follow the logic.

A C solution would have been best :)

Thanks for all the discussion and help.

H
Avatar of harfang
harfang
Flag of Switzerland image

Kdo found some c code samples in {http:#22069004} and said there's more where those came from. I find VB slightly annoying, but quite readable. Simply treat it as pseudo-code (the goto's are really just used to fake try/catch and for error handling; the returned error code is of course specific to Excel).

If you don't need to enforce some restrictions on "valid" numbers and don't expect medieval notations, the first post {http:#22065794} is all you need anyway. To obtain the "strict" version, set that flag to true and medieval to false and simplify.

Again, do look at some look-ahead parsers, just to make sure. You *were* looking for algorithms and not for a ready-made implementation, weren't you?

(°v°)
Avatar of Infinity08
Infinity08
Flag of Belgium image

The information and following discussion is good, and should be kept in the PAQ db imo.

code/algorithms as asked in the question :

http:#22065794 (harfang)
http:#22065798 (shanikawm)
http:#22065867 (josgood)
http:#22085106 (harfang)


some posts of the discussion that followed that are directly related to the question (hence also interesting) :

http:#22069004 (Kdo)
http:#22069554 (harfang)
http:#22075374 (Kdo)
http:#22082514 (JoseParrot)
Avatar of Venabili
Venabili
Flag of Bulgaria image

One answer per person - the rule in Cleanup is equal number of points per user in the splits.
Avatar of Infinity08
Infinity08
Flag of Belgium image

There were 5 persons in my proposal, so each would get 100, for example :

http:#22065794 (harfang)             33
http:#22065798 (shanikawm)       100
http:#22065867 (josgood)            100
http:#22085106 (harfang)             33

http:#22069004 (Kdo)                   50
http:#22069554 (harfang)             34
http:#22075374 (Kdo)                   50
http:#22082514 (JoseParrot)        100

It was just an idea ;)
Avatar of Venabili
Venabili
Flag of Bulgaria image

:) Yes but it cannot be done automatically. Every answer in the split will get the equal number of points so 1 per person is what we do. And opening the door for awarding uneven splits will make all CVs work even worse.

Venabili
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

I really enjoyed this thread and believe that it definitely belongs in the PAQ.

Split the points among the contributors.

harfang       - C (only average work?)  :)
josgood      - CV (Bonus points because I'm in a good mood.)
JoseParrot  - XCIX (He loses one point for writing VB.)
shanikawm - CI (He gets JoseParrot's lost point.)
Kdo             - VIM (what else?)  :)
Infinity08    - C*$!@ (Sorry Infinity -- I ran out of cute sayings.....)


  :)
Avatar of harfang
harfang
Flag of Switzerland image

Same here: it was good, and I learned a few things. If HukkaHua doesn't close, an even Split(Strict:=True, Medieval:=True, Rule:=eeStandardCV) among V participants is fine: D-point question, C assist points, but A grade nonetheless (CD points for us and CV points for Venabili...)!

(^v°)
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> Infinity08    - C*$!@ (Sorry Infinity -- I ran out of cute sayings.....)

I'm not sure if my comments measure up to the others enough to warrant points, but thanks for the thought :)

C*$!@ is modern Roman for 25 million I'm sure ? ;)
Avatar of Infinity08
Infinity08
Flag of Belgium image

@harfang : how can one top that witty reply heh
Avatar of Venabili
Venabili
Flag of Bulgaria image

>>I really enjoyed this thread
This particular thread was quite enjoyable read compared with most of the things I read today. Although I just run the cleanup in Math and Science and the threads there are usually a pleasure to deal with. Thanks everyone for responding. I wish all experts were so ready to help when I ping :)
Programming
Programming

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.

55K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo