• yoda (unregistered)

    Ist, yeah!!!

  • P. Almonius (unregistered) in reply to yoda

    That should be "prmium"

  • RandomGuy42 (unregistered)

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

  • qbolec (unregistered)

    Obviously, he should have used switch.

  • Nagesh (unregistered)

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

  • meme (unregistered)

    Nice implementation of Y2k12.

  • anon (unregistered) in reply to Nagesh
    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.

  • Peyote `Simon` Joints (unregistered)

    One line in Haskell.

  • 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

  • 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)

  • Anon (unregistered) in reply to Nagesh
    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.

  • Anon (unregistered)

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

  • (cs)

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

  • (cs) in reply to PoPSiCLe
    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.

  • (cs)

    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); }

  • Toodle (unregistered) in reply to ubersoldat

    Code fail on IIX

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

  • TheJonB (unregistered) in reply to Toodle
    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.. ]

  • (cs) in reply to qbolec
    qbolec:
    Obviously, he should have used switch.
    A series of "cleverly" nested switches would be even better.
  • (cs) in reply to Toodle
    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.
  • (cs) in reply to Peyote `Simon` Joints
    Peyote `Simon` Joints:
    One line in Haskell.
    I think you're bluffing. Code or it didn't happen.
  • 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

  • Daniel (unregistered) in reply to PiisAWheeL
    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

  • 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

  • (cs) in reply to 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

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

  • the beholder (unregistered) in reply to 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

  • iToad (unregistered) in reply to Zolo
    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.

  • (cs)

    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.

  • (cs) in reply to the beholder
    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.

  • (cs) in reply to PoPSiCLe
    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.

  • 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])
    </pre>
    
  • 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!");
                }
            }
    
  • (cs) in reply to iToad
    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
  • (cs) in reply to iToad

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

  • (cs) in reply to 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.

  • (cs) in reply to iToad
    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.
  • foo (unregistered) in reply to ubersoldat
    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?

  • fjf (unregistered) in reply to foo
    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

  • (cs) in reply to PedanticCurmudgeon
    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.

  • TheJonB (unregistered) in reply to Nagesh
    Nagesh:
    Also Alegbra invented here.

    Etymology suggests it's from them Ay-rabs.

  • Meep (unregistered) in reply to PedanticCurmudgeon
    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 ] ]

  • (cs)

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

  • (cs) in reply to Kivi
    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.

  • iToad (unregistered) in reply to mwchase
    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.

  • tundog (unregistered)

    Clearly this problem is screaming for a Perl REGEX!

  • Ol' Bob (unregistered) in reply to Nagesh
    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?

  • (cs) in reply to briverymouse
    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.

  • LANMind (unregistered) in reply to JAHH (Just Another Haskell Hobo)
    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])
    </pre>

    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.

  • Jay (unregistered) in reply to PoPSiCLe
    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?

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

Leave a comment on “Roman Enumeration”

Log In or post as a guest

Replying to comment #378225:

« Return to Article