Comment On Roman Enumeration

"At my company, the powers-that-be determined that, because we rejected a lot of job candidates, my group was ineffective at hiring new employees,"Kendall writes, "thus, the responsibility of hiring new developers was shifted to a group much more proficient at hiring: human resources." [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Re: Roman Enumeration

2012-04-02 09:03 • by yoda (unregistered)
Ist, yeah!!!

Re: Roman Enumeration

2012-04-02 09:09 • by P. Almonius (unregistered)
378178 in reply to 378176
That should be "prmium"

Re: Roman Enumeration

2012-04-02 09:10 • by RandomGuy42 (unregistered)
if(r=="III") return "tertium";

Re: Roman Enumeration

2012-04-02 09:14 • by qbolec (unregistered)
Obviously, he should have used switch.

Re: Roman Enumeration

2012-04-02 09:14 • by Nagesh (unregistered)
Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.

Re: Roman Enumeration

2012-04-02 09:15 • by meme (unregistered)
Nice implementation of Y2k12.

Re: Roman Enumeration

2012-04-02 09:19 • by anon (unregistered)
378183 in reply to 378181
Nagesh:
Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.


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

Re: Roman Enumeration

2012-04-02 09:44 • by Peyote `Simon` Joints (unregistered)
One line in Haskell.

Re: Roman Enumeration

2012-04-02 09:52 • by LimitedMemory (unregistered)
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

Re: Roman Enumeration

2012-04-02 09:55 • by PoPSiCLe (unregistered)
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)
378190 in reply to 378181
Nagesh:
Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.


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

Re: Roman Enumeration

2012-04-02 10:01 • by Anon (unregistered)
TRWTF is this assignment, since there is no single standard for Roman numerals.

Re: Roman Enumeration

2012-04-02 10:13 • by Coyne
...and the best part of the routine is carefully hidden deep in the list: If you input 'MCMLXXXIX', it returns 'FILE NOT FOUND'.

Re: Roman Enumeration

2012-04-02 10:17 • by NSCoder
378193 in reply to 378189
PoPSiCLe:
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.

Re: Roman Enumeration

2012-04-02 10:20 • by ubersoldat
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)
378196 in reply to 378195
Code fail on
IIX

Re: Roman Enumeration

2012-04-02 10:29 • by Black Bart (unregistered)
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)
378199 in reply to 378196
Toodle:
Code fail on
IIX


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.. ]

Re: Roman Enumeration

2012-04-02 10:41 • by frits
378201 in reply to 378180
qbolec:
Obviously, he should have used switch.
A series of "cleverly" nested switches would be even better.

Re: Roman Enumeration

2012-04-02 10:48 • by PiisAWheeL
378202 in reply to 378196
Toodle:
Code fail on
IIX
8 being written as VIII aside, wouldn't it also fail on IV first?

Black Bart:
The new hire was obviously an Aztec, since he knew from hist calendar that there was no need for years beyond 2012.
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
378203 in reply to 378186
Peyote `Simon` Joints:
One line in Haskell.
I think you're bluffing. Code or it didn't happen.

Re: Roman Enumeration

2012-04-02 10:59 • by Zolo (unregistered)
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)
378205 in reply to 378202
PiisAWheeL:
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.

CAPTCHA: "validus": indeed

Re: Roman Enumeration

2012-04-02 11:14 • by the beholder (unregistered)
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

Re: Roman Enumeration

2012-04-02 11:21 • by Loren Pechtel
378207 in reply to 378206
the beholder:
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)
378208 in reply to 378206
the beholder:
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

There is such a function indeed. He just lost that hard-earned +1

Re: Roman Enumeration

2012-04-02 11:24 • by iToad (unregistered)
378209 in reply to 378204
Zolo:
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


...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.

Re: Roman Enumeration

2012-04-02 11:26 • by KattMan
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.

Re: Roman Enumeration

2012-04-02 11:34 • by Kivi
378212 in reply to 378208
the beholder:
the beholder:
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

There is such a function indeed. He just lost that hard-earned +1

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.

Re: Roman Enumeration

2012-04-02 11:35 • by D-Coder
378213 in reply to 378189
PoPSiCLe:
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.

Re: Roman Enumeration

2012-04-02 11:43 • by JAHH (Just Another Haskell Hobo) (unregistered)
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])

Re: Roman Enumeration

2012-04-02 11:44 • by PaulaBean (unregistered)
easy peasy.

private int toInt(string roman) {
int lastnum = 0;
int total = 0;
for (int c = roman.Length - 1; c >= 0; c--) {
string number = roman.Substring(c, 1);
int value = singleRomanToInt(number);
if (value < lastnum) total -= value;
else {
lastnum = value;
total += value;
}
}
return total;
}

private int singleRomanToInt(string romanchar) {
switch (romanchar.ToUpper()) {
case "I": return 1;
case "V": return 5;
case "X": return 10;
case "L": return 50;
case "C": return 100;
case "M": return 1000;
default:
return Convert.ToInt32("PaulaBean!");
}
}

Re: Roman Enumeration

2012-04-02 12:09 • by PedanticCurmudgeon
378217 in reply to 378209
iToad:
Zolo:
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


...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.
FTFY

Re: Roman Enumeration

2012-04-02 12:09 • by mwchase
378218 in reply to 378209
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])

Re: Roman Enumeration

2012-04-02 12:12 • by Nagesh
378220 in reply to 378181
Nagesh (fake):
Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.


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
378221 in reply to 378209
iToad:
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.

Re: Roman Enumeration

2012-04-02 12:17 • by foo (unregistered)
378222 in reply to 378195
ubersoldat:
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);
}
A list of integers. Really? I mean ... where's the XML?

Re: Roman Enumeration

2012-04-02 12:20 • by fjf (unregistered)
378223 in reply to 378222
foo:
ubersoldat:
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);
}
A list of integers. Really? I mean ... where's the XML?
XML == 1040, according to this function

Re: Roman Enumeration

2012-04-02 12:31 • by briverymouse
378224 in reply to 378221
PedanticCurmudgeon:
iToad:
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.

Re: Roman Enumeration

2012-04-02 12:33 • by TheJonB (unregistered)
378225 in reply to 378220
Nagesh:

Also Alegbra invented here.


Etymology suggests it's from them Ay-rabs.

Re: Roman Enumeration

2012-04-02 12:38 • by Meep (unregistered)
378227 in reply to 378203
PedanticCurmudgeon:
Peyote `Simon` Joints:
One line in Haskell.
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 ] ]

Re: Roman Enumeration

2012-04-02 12:39 • by Sir Twist
If I were more efficient at commenting, this would be first. Perhaps they will assign commenting duties to the marketing department.

Re: Roman Enumeration

2012-04-02 12:42 • by Anketam
378229 in reply to 378212
Kivi:
the beholder:
the beholder:
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

There is such a function indeed. He just lost that hard-earned +1

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.
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)
378230 in reply to 378218
mwchase:
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])


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

Re: Roman Enumeration

2012-04-02 12:43 • by tundog (unregistered)
Clearly this problem is screaming for a Perl REGEX!

Re: Roman Enumeration

2012-04-02 12:48 • by Ol' Bob (unregistered)
378232 in reply to 378220
Nagesh:
Nagesh (fake):
Roman numeral system is being flawed system. The West can be thanking Indian for invention of modurn numerals.


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

Also Alegbra invented here.


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

Re: Roman Enumeration

2012-04-02 12:49 • by PiisAWheeL
378233 in reply to 378224
briverymouse:
PedanticCurmudgeon:
iToad:
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.
Haskell people are wierd... and they talk funny.

Re: Roman Enumeration

2012-04-02 12:50 • by LANMind (unregistered)
378234 in reply to 378215
JAHH (Just Another Haskell Hobo):
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])


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)
378237 in reply to 378189
PoPSiCLe:
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?

Re: Roman Enumeration

2012-04-02 12:54 • by visualbasucks (unregistered)
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.
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Add Comment