Well, on embedded systems with limited processing power, a lookup table is still the best way ... Memory in GB's is cheap offcourse. And, the error return is TRWTF, it should be FILE_NOT_FOUND

Why would you call it a Roman Numerals to decimal numbers converter? A Roman Numeral can, by definition, not be a decimal (the Romans didn't have "0"), and neither did they use decimals (only whole numbers).

A better title for the "project" would be Roman Numerals to Integers (although this would also be wrong, as "integers" both incorporates negative numbers and zero), so the absolute correct naming would be Roman Numerals to Natural numbers (the numbers we learn when we learn to count, which are normally 1,2,3,4,5,6,7,8,9,10 etc)

Why would you call it a Roman Numerals to decimal numbers converter?

Because it returns the decimal (base 10) representation of the number. Strictly-speaking, with this name, it's correct to return it as a string (or a decimal datatype, if there is one in whichever language is being used), since returning it as an integer would make it a roman numerals to integer-without-any-particular-representation-or-base (or roman numerals to binary, depending on how you look at it) convertor, rather than specifically roman numerals to decimal.

So you're asking the right question, but for the wrong reasons. Roman Numerals to Natural Numbers doesn't make much sense, really, because all Roman numerals already do represent natural numbers; no conversion necessary. The function just changes the notation, in this case to Hindu-Arabic numerals (which are decimal, although it might not be what Kendall wanted.) If Kendall didn't want the result as a string, he should have either called it a roman numerals to integer convertor, or told the programmer whether he wanted the decimal number as a string, binary-coded decimal, or other decimal data type.

Hmmm... this is a pretty interesting example... and complex too:

public String toNormal(final String roman){
List<integer> ints = new ArrayList<integer>();
char[] cArray = roman.toCharArray();
for(int i = 0; i < cArray.length; i++){
switch(cArray[i]){
case 'I': ints.add(1); break;
case 'V': ints.add(5); break;
case 'X': ints.add(10); break;
case 'L': ints.add(50); break;
case 'C': ints.add(100); break;
case 'D': ints.add(500); break;
case 'M': ints.add(1000); break;
default: throw new IllegalArgumentException();
}
}
Integer total = ints.get(ints.size()-1);
for(int i = ints.size()-1; i > 0; i--){
if(ints.get(i) > ints.get(i-1))
total = total - ints.get(i-1);
else
total = total + ints.get(i-1);
}
return String.valueOf(total);
}<p>
</integer></integer>

8 being written as VIII aside, wouldn't it also fail on IV first?

Yes, doubly so: it should both remove twice the ints.get(i-1) number (since an instance of it was added in its own loop, and an extra should be removed) AND add ints.get(i).

So "IV" (and IX/XL/XC/LC/CD/CM/DM wherever they occur in a number) would return 0 in the current state.

...and this is why we will never see a Haskell WTF here. Mere mortals such as us are simply not qualified to tell the difference between god-mode Haskell and WTF Haskell.

The better way in my mind is to use the interpreter pattern and let it build any decimal representation of any given roman numeral representation you give it.

Bonus points for the fact there this is an example found with an easy google search on the interpreter pattern that does this exact same thing.

Why would you call it a Roman Numerals to decimal numbers converter? A Roman Numeral can, by definition, not be a decimal (the Romans didn't have "0"), and neither did they use decimals (only whole numbers).

A better title for the "project" would be Roman Numerals to Integers (although this would also be wrong, as "integers" both incorporates negative numbers and zero), so the absolute correct naming would be Roman Numerals to Natural numbers (the numbers we learn when we learn to count, which are normally 1,2,3,4,5,6,7,8,9,10 etc)

I can always count on TDWTF for my daily dose of pedantic dickweedery.

Here's my single line (assuming correctly formed romans):

let roman = "MDIX"; nums = map (\r -> lookup r (zip "IVXLCDM" [1,5,10,50,100,500,1000])) roman in sum $ zipWith (\xx yy -> case xx of Just x -> case yy of Just y -> if (x<y) then="" -x="" else="" x)="" nums="" ((tail="" nums)++[just="" 0])=""></y)>

...and this is why we will never see a Haskell WTF here. Mere mortals such as us are simply not qualified to tell the difference between god-mode Haskell and WTF Python.

Hmmm... this is a pretty interesting example... and complex too:

public String toNormal(final String roman){
List<integer> ints = new ArrayList<integer>();
char[] cArray = roman.toCharArray();
for(int i = 0; i < cArray.length; i++){
switch(cArray[i]){
case 'I': ints.add(1); break;
case 'V': ints.add(5); break;
case 'X': ints.add(10); break;
case 'L': ints.add(50); break;
case 'C': ints.add(100); break;
case 'D': ints.add(500); break;
case 'M': ints.add(1000); break;
default: throw new IllegalArgumentException();
}
}
Integer total = ints.get(ints.size()-1);
for(int i = ints.size()-1; i > 0; i--){
if(ints.get(i) > ints.get(i-1))
total = total - ints.get(i-1);
else
total = total + ints.get(i-1);
}
return String.valueOf(total);
}</integer></integer>

A list of integers. Really? I mean ... where's the XML?

Hmmm... this is a pretty interesting example... and complex too:

public String toNormal(final String roman){
List<integer> ints = new ArrayList<integer>();
char[] cArray = roman.toCharArray();
for(int i = 0; i < cArray.length; i++){
switch(cArray[i]){
case 'I': ints.add(1); break;
case 'V': ints.add(5); break;
case 'X': ints.add(10); break;
case 'L': ints.add(50); break;
case 'C': ints.add(100); break;
case 'D': ints.add(500); break;
case 'M': ints.add(1000); break;
default: throw new IllegalArgumentException();
}
}
Integer total = ints.get(ints.size()-1);
for(int i = ints.size()-1; i > 0; i--){
if(ints.get(i) > ints.get(i-1))
total = total - ints.get(i-1);
else
total = total + ints.get(i-1);
}
return String.valueOf(total);
}</integer></integer>

A list of integers. Really? I mean ... where's the XML?

Mere mortals such as us are simply not qualified to tell the difference between god-mode Haskell and WTF Haskell.

Some would say that there is no difference.

The average Haskell WTF is something about a guy who, during an interview, forgot to make his Monad an Applicative. Can you imagine such foolishness? Also, missing a perfectly good opportunity to use a zipWith or a liftM is cause for certain derision.

I think you're bluffing. Code or it didn't happen.

rom 'I' = 1
rom 'V' = 5
rom 'X' = 10
...

All of which can be translated into a single case statement, rom x = case x of { 'I' -> 1; 'V' -> 5; 'X' -> 10 ... }

Since a string is simply a list of characters:

raw_decode nums = map rom nums

Or as a comprehension: raw_decode nums = [ rom num | num <- nums ]

So raw_decode "IXX" yields [1, 10, 10]

Next we want to group up all the IX type cases. Our numerals should always be in descending order, except for those cases, so we can simply group using the < operator.

rollup_prefixes raw = groupBy (<) raw

That gives us a list of lists that may be one or two elements long, by our invariants.

sub_prefix [ num ] = num
sub_prefix [ pre num ] = num - pre

Or as a case statement:

sub_prefix roll = case roll of { [ num ] -> num; [ pre num ] -> num - pre; }

And we then map that out:

sub_prefixes rolled_up = map sub_prefix rolled_up

Then we simply sum the result:

final subbed = sum subbed

Put it all together:

rom2num roman = sum [ case roll of { [ num ] -> num; [ pre num ] -> num - pre; } | roll <-
groupBy (<) [ case num of { 'I' -> 1; 'V' -> 2; ... } | letter <- roman ] ]

Using Excel to auto-generate code is delightfully perverse. That's one of those innovations that is just crazy enough to be crazy.

So the best solution to this is to create an excel macro to do it. That is Brillant! Then again I have been known to use excel to create 100+ statements (that contain things like counters) for random things like database inserts and xml input files.

Mere mortals such as us are simply not qualified to tell the difference between god-mode Haskell and WTF Haskell.

Some would say that there is no difference.

The average Haskell WTF is something about a guy who, during an interview, forgot to make his Monad an Applicative. Can you imagine such foolishness? Also, missing a perfectly good opportunity to use a zipWith or a liftM is cause for certain derision.

Here's my single line (assuming correctly formed romans):

let roman = "MDIX"; nums = map (\r -> lookup r (zip "IVXLCDM" [1,5,10,50,100,500,1000])) roman in sum $ zipWith (\xx yy -> case xx of Just x -> case yy of Just y -> if (x<y) then="" -x="" else="" x)="" nums="" ((tail="" nums)++[just="" 0])=""></y)>

Two comments.

First, any time I see code that might perhaps be more easily understood if represented in cyrilic, or maybe a right to left language, I figure "wtf?" Then again, I'm not building device drivers or something else that requires the most succinct code - I'm solving business problems - and you'd better believe that maintainability is a so-close-it's-immaterial second to whether or not it produces the right result. As sure as God made little green apples, today's correct solution will be incorrect tomorrow. All of that leads to...

Second, the technical debt in our instance would be prohibitively high.

Why would you call it a Roman Numerals to decimal numbers converter? A Roman Numeral can, by definition, not be a decimal (the Romans didn't have "0"), and neither did they use decimals (only whole numbers).

A better title for the "project" would be Roman Numerals to Integers (although this would also be wrong, as "integers" both incorporates negative numbers and zero), so the absolute correct naming would be Roman Numerals to Natural numbers (the numbers we learn when we learn to count, which are normally 1,2,3,4,5,6,7,8,9,10 etc)

The word "decimal" means "base 10". He is converting from Roman numberals to Hindu-Arabic numerals in base 10. The name is perfectly appropriate.

He is not converting Roman Numerals to natural numbers. Roman Numerals already are natural numbers. Natural numbers exist independent of the notation used to represent them.

Even if we understood "integer" to mean "Hindu-Arabic represenstation of integers" and "natural numbers" to mean "Hindu-Arabic representation of natural numbers", your comment makes no sense. Even if the function can never produce zero or negative numbers, so what? If someone says that he has written a "French to English translation dictionary", would it be fair to say he was lying because you found an English word for which there is no corresponding word in French, and which the dictionary did not therefore have an entry for? Or for a closer analogy, would it really be fair to say that there cannot possibly be any such thing as a function to convert short integers to long integers, because many long integers exist which could never be output by this function? So the range of possible outputs is a subset of the domain. So what?

Why doesnt it return a "end of source file not found" error?
If it has a prefixed "e" it can be tested for being an error string too or maybe garbled floating point numbers.

Admin

Ist, yeah!!!

Admin

That should be "prmium"

Admin

if(r=="III") return "tertium";

Admin

Obviously, he should have used switch.

Admin

Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.

Admin

Nice implementation of Y2k12.

Admin

Cool, I'll thank Gray Eagle next time I see him.

Admin

One line in Haskell.

Admin

Well, on embedded systems with limited processing power, a lookup table is still the best way ... Memory in GB's is cheap offcourse. And, the error return is TRWTF, it should be FILE_NOT_FOUND

Admin

Why would you call it a Roman Numerals to decimal numbers converter? A Roman Numeral can, by definition, not be a decimal (the Romans didn't have "0"), and neither did they use decimals (only whole numbers).

A better title for the "project" would be Roman Numerals to Integers (although this would also be wrong, as "integers" both incorporates negative numbers and zero), so the absolute correct naming would be Roman Numerals to Natural numbers (the numbers we learn when we learn to count, which are normally 1,2,3,4,5,6,7,8,9,10 etc)

Admin

(Fake) Nagesh makes a valid point?!? Must be a late April Fools day joke.

Admin

TRWTF is this assignment, since there is no single standard for Roman numerals.

Admin

...and the best part of the routine is carefully hidden deep in the list: If you input 'MCMLXXXIX', it returns 'FILE NOT FOUND'.

Admin

So you're asking the right question, but for the wrong reasons. Roman Numerals to Natural Numbers doesn't make much sense, really, because all Roman numerals already do represent natural numbers; no conversion necessary. The function just changes the notation, in this case to Hindu-Arabic numerals (which are decimal, although it might not be what Kendall wanted.) If Kendall didn't want the result as a string, he should have either called it a roman numerals to integer convertor, or told the programmer whether he wanted the decimal number as a string, binary-coded decimal, or other decimal data type.

Admin

Hmmm... this is a pretty interesting example... and complex too:

public String toNormal(final String roman){ List<integer> ints = new ArrayList<integer>(); char[] cArray = roman.toCharArray(); for(int i = 0; i < cArray.length; i++){ switch(cArray[i]){ case 'I': ints.add(1); break; case 'V': ints.add(5); break; case 'X': ints.add(10); break; case 'L': ints.add(50); break; case 'C': ints.add(100); break; case 'D': ints.add(500); break; case 'M': ints.add(1000); break; default: throw new IllegalArgumentException(); } } Integer total = ints.get(ints.size()-1); for(int i = ints.size()-1; i > 0; i--){ if(ints.get(i) > ints.get(i-1)) total = total - ints.get(i-1); else total = total + ints.get(i-1); } return String.valueOf(total); }<p> </integer></integer>

Admin

Code fail on IIX

Admin

The new hire was obviously an Aztec, since he knew from hist calendar that there was no need for years beyond 2012.

Admin

IIX is invalid, 8 is written VIII as any fule no.

[ As an aside, I'm a bit thick, but I just realised that the real WTF is that the captcha answer is encoded in the viewstate on this site.. ]

Admin

Admin

Admin

Admin

r = { "I" : 1, "V" : 5, "X" : 10, "L" : 50, "C" : 100, "D" : 500, "M" : 1000 } def r2d(m, p="I"): return 0 if m=="" else r2d(m[:-1],m[-1])+r[m[-1]]*(1 if r[m[-1]]>=r[p] else -1)

print r2d("") # 0 print r2d("IX") # 9 print r2d("XI") # 11 print r2d("MDCCCCX") # 1910 print r2d("MCMLIV") # 1954 print r2d("MCMXC") # 1990 print r2d("MCMXCIX") # 1999

Admin

So "IV" (and IX/XL/XC/LC/CD/CM/DM wherever they occur in a number) would return 0 in the current state.

CAPTCHA: "validus": indeed

Admin

Nobody commented yet, but this new-hire MUST have typed

Every. Single. Number.In roman numbers.That's not exactly an easy thing to do unless there's a feature in Excel that I'm not aware of.

+1 for dedication -100 for stupidity

Admin

It wouldn't be that hard with a bit of cut & paste.

Admin

Admin

...and this is why we will never see a Haskell WTF here. Mere mortals such as us are simply not qualified to tell the difference between god-mode Haskell and WTF Haskell.

Admin

The better way in my mind is to use the interpreter pattern and let it build any decimal representation of any given roman numeral representation you give it.

Bonus points for the fact there this is an example found with an easy google search on the interpreter pattern that does this exact same thing.

Admin

The function, if anyone's interested, is here: http://office.microsoft.com/en-us/excel-help/convert-arabic-to-roman-numerals-HA010007443.aspx

Using Excel to auto-generate code is delightfully perverse. That's one of those innovations that is just crazy enough to be crazy.

Admin

Admin

Here's my single line (assuming correctly formed romans):

Admin

easy peasy.

Admin

Admin

That's not Haskell, that's Python. It's using a language feature that I was previously unaware of, oops. ([value] if [condition] else [alternative])

Admin

Fake nagesh has stole words from my key-board. It is "modern numbers", you idiot.

Also Alegbra invented here.

Admin

Admin

Admin

Admin

The average Haskell WTF is something about a guy who, during an interview, forgot to make his Monad an Applicative. Can you imagine such foolishness? Also, missing a perfectly good opportunity to use a zipWith or a liftM is cause for certain derision.

Admin

Etymology suggests it's from them Ay-rabs.

Admin

rom 'I' = 1 rom 'V' = 5 rom 'X' = 10 ...

All of which can be translated into a single case statement, rom x = case x of { 'I' -> 1; 'V' -> 5; 'X' -> 10 ... }

Since a string is simply a list of characters:

raw_decode nums = map rom nums

Or as a comprehension: raw_decode nums = [ rom num | num <- nums ]

So raw_decode "IXX" yields [1, 10, 10]

Next we want to group up all the IX type cases. Our numerals should always be in descending order, except for those cases, so we can simply group using the < operator.

rollup_prefixes raw = groupBy (<) raw

That gives us a list of lists that may be one or two elements long, by our invariants.

sub_prefix [ num ] = num sub_prefix [ pre num ] = num - pre

Or as a case statement:

sub_prefix roll = case roll of { [ num ] -> num; [ pre num ] -> num - pre; }

And we then map that out:

sub_prefixes rolled_up = map sub_prefix rolled_up

Then we simply sum the result:

final subbed = sum subbed

Put it all together:

rom2num roman = sum [ case roll of { [ num ] -> num; [ pre num ] -> num - pre; } | roll <- groupBy (<) [ case num of { 'I' -> 1; 'V' -> 2; ... } | letter <- roman ] ]

Admin

If I were more efficient at commenting, this would be first. Perhaps they will assign commenting duties to the marketing department.

Admin

Admin

Crap, you're right. To add insult to injury, I use Python. This is what happens when you post on a Monday morning.

Admin

Clearly this problem is screaming for a Perl REGEX!

Admin

Who's this Aleg chick? And what're you doin' inventin' underwear for her?

Admin

Admin

Two comments.

First, any time I see code that might perhaps be more easily understood if represented in cyrilic, or maybe a right to left language, I figure "wtf?" Then again, I'm not building device drivers or something else that requires the most succinct code - I'm solving business problems - and you'd better believe that maintainability is a so-close-it's-immaterial second to whether or not it produces the right result. As sure as God made little green apples, today's correct solution will be incorrect tomorrow. All of that leads to...

Second, the technical debt in our instance would be prohibitively high.

Admin

The word "decimal" means "base 10". He is converting from Roman numberals to Hindu-Arabic numerals in base 10. The name is perfectly appropriate.

He is not converting Roman Numerals to natural numbers. Roman Numerals already are natural numbers. Natural numbers exist independent of the notation used to represent them.

Even if we understood "integer" to mean "Hindu-Arabic represenstation of integers" and "natural numbers" to mean "Hindu-Arabic representation of natural numbers", your comment makes no sense. Even if the function can never produce zero or negative numbers, so what? If someone says that he has written a "French to English translation dictionary", would it be fair to say he was lying because you found an English word for which there is no corresponding word in French, and which the dictionary did not therefore have an entry for? Or for a closer analogy, would it really be fair to say that there cannot possibly be any such thing as a function to convert short integers to long integers, because many long integers exist which could never be output by this function? So the range of possible outputs is a subset of the domain. So what?

Admin

Why doesnt it return a "end of source file not found" error? If it has a prefixed "e" it can be tested for being an error string too or maybe garbled floating point numbers.