| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Next » |
Re: Roman Enumeration
2012-04-02 09:09
•
by
P. Almonius
(unregistered)
|
|
That should be "prmium"
|
|
if(r=="III") return "tertium";
|
|
Obviously, he should have used switch.
|
|
Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.
|
|
Nice implementation of Y2k12.
|
Re: Roman Enumeration
2012-04-02 09:19
•
by
anon
(unregistered)
|
Cool, I'll thank Gray Eagle next time I see him. |
|
One line in Haskell.
|
|
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) |
Re: Roman Enumeration
2012-04-02 09:56
•
by
Anon
(unregistered)
|
(Fake) Nagesh makes a valid point?!? Must be a late April Fools day joke. |
|
TRWTF is this assignment, since there is no single standard for Roman numerals.
|
|
...and the best part of the routine is carefully hidden deep in the list: If you input 'MCMLXXXIX', it returns 'FILE NOT FOUND'.
|
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); } |
Re: Roman Enumeration
2012-04-02 10:26
•
by
Toodle
(unregistered)
|
|
Code fail on
IIX |
|
The new hire was obviously an Aztec, since he knew from hist calendar that there was no need for years beyond 2012.
|
Re: Roman Enumeration
2012-04-02 10:37
•
by
TheJonB
(unregistered)
|
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.. ] |
A series of "cleverly" nested switches would be even better. |
8 being written as VIII aside, wouldn't it also fail on IV first? Wrong. That is a reason not to include 2013. You still need to include the last year that will exist. December 20th is most of the year. |
Re: Roman Enumeration
2012-04-02 10:52
•
by
PedanticCurmudgeon
|
I think you're bluffing. Code or it didn't happen. |
|
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 |
Re: Roman Enumeration
2012-04-02 10:59
•
by
Daniel
(unregistered)
|
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. CAPTCHA: "validus": indeed |
|
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 |
It wouldn't be that hard with a bit of cut & paste. |
Re: Roman Enumeration
2012-04-02 11:22
•
by
the beholder
(unregistered)
|
There is such a function indeed. He just lost that hard-earned +1 |
Re: Roman Enumeration
2012-04-02 11:24
•
by
iToad
(unregistered)
|
...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. |
And, to be fair, the -100 as well. 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. |
I can always count on TDWTF for my daily dose of pedantic dickweedery. |
Re: Roman Enumeration
2012-04-02 11:43
•
by
JAHH (Just Another Haskell Hobo)
(unregistered)
|
|
Here's my single line (assuming correctly formed romans):
|
|
easy peasy.
|
Re: Roman Enumeration
2012-04-02 12:09
•
by
PedanticCurmudgeon
|
FTFY |
|
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])
|
Fake nagesh has stole words from my key-board. It is "modern numbers", you idiot. Also Alegbra invented here. |
Re: Roman Enumeration
2012-04-02 12:13
•
by
PedanticCurmudgeon
|
Some would say that there is no difference. |
Re: Roman Enumeration
2012-04-02 12:17
•
by
foo
(unregistered)
|
A list of integers. Really? I mean ... where's the XML? |
Re: Roman Enumeration
2012-04-02 12:20
•
by
fjf
(unregistered)
|
XML == 1040, according to this function |
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. |
Re: Roman Enumeration
2012-04-02 12:33
•
by
TheJonB
(unregistered)
|
Etymology suggests it's from them Ay-rabs. |
Re: Roman Enumeration
2012-04-02 12:38
•
by
Meep
(unregistered)
|
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 ] ] |
|
If I were more efficient at commenting, this would be first. Perhaps they will assign commenting duties to the marketing department.
|
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. |
Re: Roman Enumeration
2012-04-02 12:42
•
by
iToad
(unregistered)
|
Crap, you're right. To add insult to injury, I use Python. This is what happens when you post on a Monday morning. |
|
Clearly this problem is screaming for a Perl REGEX!
|
Re: Roman Enumeration
2012-04-02 12:48
•
by
Ol' Bob
(unregistered)
|
Who's this Aleg chick? And what're you doin' inventin' underwear for her? |
Haskell people are wierd... and they talk funny. |
Re: Roman Enumeration
2012-04-02 12:50
•
by
LANMind
(unregistered)
|
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. |
Re: Roman Enumeration
2012-04-02 12:53
•
by
Jay
(unregistered)
|
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. |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Next » |