 Feature Articles
 CodeSOD
 Error'd
 Forums

Other Articles
 Random Article
 Other Series
 Alex's Soapbox
 Announcements
 Best ofâ€¦
 Best of Email
 Best of the Sidebar
 Bring Your Own Code
 Coded Smorgasbord
 Mandatory Fun Day
 Off Topic
 Representative Line
 News Roundup
 Editor's Soapbox
 Software on the Rocks
 Souvenir Potpourri
 Sponsor Post
 Tales from the Interview
 The Daily WTF: Live
 Virtudyne
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
...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 HinduArabic 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, binarycoded 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(i1)) total = total  ints.get(i1); else total = total + ints.get(i1); } return String.valueOf(total); }
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 newhire 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 godmode 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/enus/excelhelp/convertarabictoromannumeralsHA010007443.aspx
Using Excel to autogenerate 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 keyboard. 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 Ayrabs.
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 socloseit'simmaterial 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 HinduArabic 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 "HinduArabic represenstation of integers" and "natural numbers" to mean "HinduArabic 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.