• (cs) in reply to da Doctah
    da Doctah:
    Somewhat serious followup: Give me a regex that will match only movie titles that end in Roman numerals. Take into account that sometimes there will be a secondary title, as in "Death Warriors IV: the Deaths Continue".

    I tried to do this a few years ago on IMDb to figure out what the highest numbered "sequel" was. Ran into the problem that "mi", "di", "xi" and others are actual words in some languages.

    Was this before or after In the Mix?

  • Alex (unregistered)

    Just my one-liner:

    echo "MCMLXVII" | sed "s/IX/+9/g; s/IV/+4/g; s/I/+1/g; s/V/+5/g; s/XL/+40/g; s/XC/+40/g; s/X/+10/g; s/L/+50/g; s/CD/+400/g; s/CM/+900/g; s/C/+100/g; s/M/+1000/g; s/^/echo $\[ /; s/$/ ] /" | bash
    

    captcha: saluto. I saluto to this code.

  • CXIIX (unregistered) in reply to yoda

    http://lmgtfy.com/?q=roman+numeral+converter

  • (cs) in reply to Brendan
    Brendan:
    Meep:
    rom2num roman = sum [ case roll of { [ num ] -> num; [ pre num ] -> num - pre; } | roll <- groupBy (<) [ case letter of { 'I' -> 1; 'V' -> 5; ... } | letter <- roman ] ]

    That's about 25 lines of code (including the comments that are necessary to make Haskell readable). It would've been less typing in something like COBOL (and probably faster too, as there's no need to depend on the misuse of recursion)!

    I hope you're trolling. It's one line, not 25. The fact that you would need 24 lines of comments to understand one line of code, says nothing about Haskell, and everything about you. I only read the line of code, not the rest of his post, and I understand perfectly what it does, and how. (It's quite succinct and clever by the way, kudos Meep.) So either I am much smarter than you, or you have never read one book on Haskell, and for some reason find it strange that this implies you don't understand the language. Probably both.

    You clearly don't even understand how a processor works. There is no direct recursion in his code, but some of the library functions he uses are implemented as tail recursive functions. Modern compilers (you know, like the one used to compile Haskell, but also most modern C compilers) will output the exact same processor instructions for a tail-recursive function as a C compiler would for a while loop.

  • (cs) in reply to Yoh
    Yoh:
    That code in question did in fact fail on IX though. As will most of the examples people have shared.
    Do they fail on VIIII or MIM?
  • Yoh (unregistered) in reply to havokk

    They wouldn't fail PROPERLY on VIIII, as that is not a valid roman numeral.

    http://mathworld.wolfram.com/RomanNumerals.html http://chart.copyrightdata.com/ch02.html

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

    You know, I wish I used a language that allowed us to put pointlessly long lines together in the name of stupidity.

    Oh wait... I do:

    (from fluff in " " let n = (from c in Console.ReadLine().ToUpper() where "IVXLCDM".Contains(c) select c == 'I' ? 1 : c == 'V' ? 5 : c == 'X' ? 10 : c == 'L' ? 50 : c == 'C' ? 100 : c == 'D' ? 500 : 1000).ToList() let r = from i in Enumerable.Range(0, n.Count) let l = i > 0 && n[i] > n[i - 1] ? -n[i - 1] * 2 : 0 select n[i] + l select r.Sum()).ToList().ForEach(Console.WriteLine);

    With some spacing, for mere mortals:

    http://pastebin.com/usUTLr42

  • (cs) in reply to Yoh
    Yoh:
    They wouldn't fail PROPERLY on VIIII, as that is not a valid roman numeral.

    http://mathworld.wolfram.com/RomanNumerals.html http://chart.copyrightdata.com/ch02.html

    From your first link:

    "the practice of placing smaller digits before large ones to indicate subtraction of value was hardly ever used by Romans and came into popularity in Europe after the invention of the printing press"

    So VIIII is a perfectly valid Roman numeral, though the canonical form is probably IX (VIV is definitely incorrect though.)

    It might be worth pointing out though IL is ambiguously 49, the more usually seen way to write that is XLIX. The general rule is that you would only write subtraction BA if B was a power of ten (I, X, C) and A was either 5 * B or 10 * B.

  • Dave (unregistered) in reply to Peyote `Simon` Joints
    Peyote `Simon` Joints:
    One line in Haskell.

    Yawn. I can do it in one statement in Intercal:

    READ OUT ,1

    (possibly 2 if you need to add a PLEASE before it, and depending on whether you interpret the PLEASE as a separate statement or not).

  • (cs) in reply to CarnivorousHippie
    CarnivorousHippie:
    da Doctah:
    Somewhat serious followup: Give me a regex that will match only movie titles that end in Roman numerals. Take into account that sometimes there will be a secondary title, as in "Death Warriors IV: the Deaths Continue".

    I tried to do this a few years ago on IMDb to figure out what the highest numbered "sequel" was. Ran into the problem that "mi", "di", "xi" and others are actual words in some languages.

    Was this before or after In the Mix?

    Before, but there's probably some old western starring Tom Mix that would produce another false hit.

  • (cs) in reply to PiisAWheeL
    PiisAWheeL:
    KattMan:
    CD/CXXVII = PI
    I. P is not a roman numeral.
    Better notation would have been CD ÷ CXXVII = π, yes. However:
    PiisAWheeL:
    II. Roman numerals didn't have decimal points.
    Who says they need to? If you work with traditional fractions, you don't need decimal points (or commas, or whatever). CD ÷ CXXVII is a close enough approximation of the value of π for day to day use.
  • Karl (unregistered) in reply to iToad

    Sorry, dude. That is Python.

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

    Eee ya plonker. Alegbra is for people with saggy legs.

  • Sarcastic kebab reaper (unregistered) in reply to briverymouse
    briverymouse:
    Brendan:
    Meep:
    rom2num roman = sum [ case roll of { [ num ] -> num; [ pre num ] -> num - pre; } | roll <- groupBy (<) [ case letter of { 'I' -> 1; 'V' -> 5; ... } | letter <- roman ] ]

    That's about 25 lines of code (including the comments that are necessary to make Haskell readable). It would've been less typing in something like COBOL (and probably faster too, as there's no need to depend on the misuse of recursion)!

    I hope you're trolling. It's one line, not 25. The fact that you would need 24 lines of comments to understand one line of code, says nothing about Haskell, and everything about you. I only read the line of code, not the rest of his post, and I understand perfectly what it does, and how. (It's quite succinct and clever by the way, kudos Meep.) So either I am much smarter than you, or you have never read one book on Haskell, and for some reason find it strange that this implies you don't understand the language. Probably both.

    You clearly don't even understand how a processor works. There is no direct recursion in his code, but some of the library functions he uses are implemented as tail recursive functions. Modern compilers (you know, like the one used to compile Haskell, but also most modern C compilers) will output the exact same processor instructions for a tail-recursive function as a C compiler would for a while loop.

    I hope you are trolling. There's nothing like a complicated "one liner" that's mostly unreadable and uncommented. You clearly aren't a coder, and if you are, you aren't a good coder. You don't give two thoughts to the fact that the maintainability of code has become so important that there are huge books about how to write a readable code. Oh wait, whole languages have been invented with readability and maintainability in mind. And yet you will write your "one liner" (which doesn't count as one, having several consecutive instructions slapped together and pretending to be just one) and no comments.

  • Brendan (unregistered) in reply to briverymouse

    [quote user="briverymouse"][quote user="Brendan"]That's about 25 lines of code (including the comments that are necessary to make Haskell readable). It would've been less typing in something like COBOL (and probably faster too, as there's no need to depend on the misuse of recursion)![/quote]

    I hope you're trolling. It's one line, not 25. The fact that you would need 24 lines of comments to understand one line of code, says nothing about Haskell, and everything about you. I only read the line of code, not the rest of his post, and I understand perfectly what it does, and how. (It's quite succinct and clever by the way, kudos Meep.) So either I am much smarter than you, or you have never read one book on Haskell, and for some reason find it strange that this implies you don't understand the language. Probably both.

    To be honest, it was a mixture of trolling and joking. However, they say behind every joke there's an element of truth, and obviously this element of truth has made you react in the way you have.

    I've never learnt Pascal, or COBOL, or Objective C, or MIPS assembly; but have no trouble reading code written in any of these (and many other) languages. Some languages are so good that a person with no programming experience at all can get a good idea of what the code is doing. Some languages are so bad that even after learning the language you can't easily follow source code written in it. Are you suggesting that reading a book on Haskell is a necessary step just to understand Haskell source code? If so, what does that tell you about Haskell?

    [quote user="briverymouse"]You clearly don't even understand how a processor works. There is no direct recursion in his code, but some of the library functions he uses are implemented as tail recursive functions. Modern compilers (you know, like the one used to compile Haskell, but also most modern C compilers) will output the exact same processor instructions for a tail-recursive function as a C compiler would for a while loop.[/quote]

    The best way to tell what the compiler generates is to have a look at what the compiler generates. Either post a disassembly so I can laugh at it, or use silence to admit defeat.

  • scoeri (unregistered)

    code obfuscation for the win

    here is a ruby version:

    $h = { "I" => 1, "V" => 5, "X" => 10, "L" => 50, "C" => 100, "D" => 500, "M" => 1000 }

    def r2d(r, p=0);r.empty? ? 0 : r2d(r[0..-2], v=$h[r[-1]]) + (v < p ? -v : v);end

  • (cs) in reply to Matt Westwood
    Matt Westwood:
    From this they derive postmodern literary dogma such as: 'Mathematics is portrayed as a woman whose nature desires to be the conquered Other.'

    And here I thought most natural women desired to be the conquered other. Take the terms we use in every day society, "A man takes a wife", "A woman gives herself to her man." On top of that look at the desire for women to be attractive, to display themselves to attract a good mate. It seems biologically wired that women should be seen this way, but not as weak, one can conquer and take a strong woman, and trust me if she remain strong even when she is yours it is so much better. :)

  • (cs) in reply to Brendan
    Brendan:
    Some languages are so good that a person with no programming experience at all can get a good idea of what the code is doing. Some languages are so bad that even after learning the language you can't easily follow source code written in it. Are you suggesting that reading a book on Haskell is a necessary step just to understand Haskell source code? If so, what does that tell you about Haskell?
    I was just about to post something to this effect as a joke. The worst part is that so many programmers actually take this position seriously.
  • Anon (unregistered) in reply to Alex
    Alex:
    Just my one-liner:
    echo "MCMLXVII" | sed "s/IX/+9/g; s/IV/+4/g; s/I/+1/g; s/V/+5/g; s/XL/+40/g; s/XC/+40/g; s/X/+10/g; s/L/+50/g; s/CD/+400/g; s/CM/+900/g; s/C/+100/g; s/M/+1000/g; s/^/echo $\[ /; s/$/ ] /" | bash
    

    Some people look at a problem and think "I know, I'll use regular expressions." Now they have II problems.

  • (cs) in reply to PedanticCurmudgeon
    PedanticCurmudgeon:
    Brendan:
    Some languages are so good that a person with no programming experience at all can get a good idea of what the code is doing. Some languages are so bad that even after learning the language you can't easily follow source code written in it. Are you suggesting that reading a book on Haskell is a necessary step just to understand Haskell source code? If so, what does that tell you about Haskell?
    I was just about to post something to this effect as a joke. The worst part is that so many programmers actually take this position seriously.
    Hey, the same argument can be applied to French, and everybody knows French is stupide.
  • the beholder (unregistered) in reply to Zylon
    Zylon:
    the beholder:
    Nobody commented yet, but this new-hire MUST have typed Every. Single. Number. In roman numbers.
    Uh, yeah. Nobody's commented on this because it's implicitly stated right in the article:
    // // Snipped LOTS of "code" here //
    This is, in fact, the entire focus of the WTFery of his solution.
    You clearly didn't understand what I meant. Yes, it's lenghty, but what I really pointed out is the TYPING of roman numbers. It's by far worse than "ha-ha, this guy have typed two thousand consecutive numbers".

    With roman numbers you don't just copy last line, hit backspace and add a char one position higher than the one you erased. If it were that simple it could be done on an "auto-pilot" mode (albeit it would still be a minor WTF)

    So, how does your foot taste?

  • Zunesize Me! (unregistered) in reply to KattMan
    KattMan:
    Matt Westwood:
    From this they derive postmodern literary dogma such as: 'Mathematics is portrayed as a woman whose nature desires to be the conquered Other.'
    And here I thought most natural women desired to be the conquered other. Take the terms we use in every day society, "A man takes a wife", "A woman gives herself to her man." On top of that look at the desire for women to be attractive, to display themselves to attract a good mate. It seems biologically wired that women should be seen this way, but not as weak, one can conquer and take a strong woman, and trust me if she remain strong even when she is yours it is so much better. :)
    I'm not sure where I stand with respect to female dominance/submissiveness, but what I do know is the kind I like are the kind with penises.
  • Zunesize Me! (unregistered) in reply to the beholder
    the beholder:
    Zylon:
    the beholder:
    Nobody commented yet, but this new-hire MUST have typed Every. Single. Number. In roman numbers.
    This is, in fact, the entire focus of the WTFery of his solution.
    You clearly didn't understand what I meant... ...So, how does your foot taste?
    Joke's on you, pal! I regularly taste my own feet, anyway! So there!
  • Qŭert (unregistered) in reply to radarbob
    radarbob:
    Don't laf.
    That awkward moment when a misspelling makes more sense than the "correct" spelling.

    Captcha: commoveo => Con los ojos.

  • (cs) in reply to Sarcastic kebab reaper
    Sarcastic kebab reaper:
    briverymouse:
    Brendan:
    Meep:
    rom2num roman = sum [ case roll of { [ num ] -> num; [ pre num ] -> num - pre; } | roll <- groupBy (<) [ case letter of { 'I' -> 1; 'V' -> 5; ... } | letter <- roman ] ]

    That's about 25 lines of code (including the comments that are necessary to make Haskell readable). It would've been less typing in something like COBOL (and probably faster too, as there's no need to depend on the misuse of recursion)!

    I hope you're trolling. It's one line, not 25. ...

    I hope you are trolling. There's nothing like a complicated "one liner" that's mostly unreadable and uncommented. You clearly aren't a coder, and if you are, you aren't a good coder. You don't give two thoughts to the fact that the maintainability of code has become so important that there are huge books about how to write a readable code. Oh wait, whole languages have been invented with readability and maintainability in mind. And yet you will write your "one liner" (which doesn't count as one, having several consecutive instructions slapped together and pretending to be just one) and no comments.

    I never said it was good production code. It obviously isn't. Nesting two list comprehensions is something I would probably never do. This is roughly what I would write in practice (based on Meep's code): http://pastebin.com/7KK7r8jW (I added error handling, as this is supposed to be production code.) As you can see, only ten lines, of which two are imports that you probably already have in any serious application, one is a blank line, and one is a type annotation that's only added for clarity.

    Still, Meep's code is very readable considering it's a one-liner, and every person that actually knows Haskell needs less than a few seconds to see what it does. And it is a one-liner: adding a line break anywhere in there would feel unnatural. I'm not sure what you mean by instruction, as there's no such thing in Haskell, but you probably mean the semicolons. They are part of the case statement, and could indeed be replaced by newlines, but having a small case statement like that on one line is not always considered bad style.

  • Jay (unregistered)

    What the hell, have some old school VB:

    Public Function Rom2Dec(Roman As String) As Long
    
        Dim Letter As String
        Dim Result As Long
        Dim MaxValue As Long
        Dim Position As Long
        
        Result = 0
        MaxValue = 0
    
        For Position = Len(Roman) To 1 Step -1
            Letter = Mid$(Roman, Position, 1)
            Value = Switch(Letter = "I", 1, Letter = "V", 5, Letter = "X", 10, Letter = "L", 50, Letter = "C", 100, Letter = "D", 500, Letter = "M", 1000)
            If Value < MaxValue Then
                Result = Result - Value
            Else
                Result = Result + Value
                MaxValue = Value
            End If
        Next Position
    
        Rom2Dec = Result
        
    End Function

    Deals with all the weird stuff like IIIIX, CCCCCCCCCCCCCCCCCCCIC etc, who knew 1999 can be written as MCMXCIX, MLMVLIV, MXMIX, MVMIV & MIM.

  • (cs) in reply to iToad
    iToad:
    There was actually a serious (maybe) proposal to add Roman numerals to Python, but it got rejected:

    PEP 313

    Too bad. I wanted to see whether I could calculate the square root of pi, using only Roman Numerals.

    Check the "Created" date on PEP 313...

  • (cs) in reply to Jay
    Jay:
    who knew 1999 can be written as MCMXCIX, MLMVLIV, MXMIX, MVMIV & MIM.

    MCMXCIX: yes. MLMVLIV: no, VL doesn't work because V is not a power of 10. MXMIX: no, XM doesn't work because M > 10 * X MVMIV: no, VM doesn't work because V is not a power of 10 and M > 10 * V MIM: no, IM doesn't work because M > 10 * I

    You could also write it as MDCCCCLXXXXVIIII which is how the Romans would have written it.

  • (cs) in reply to ObiWayneKenobi
    ObiWayneKenobi:
    ... There are sophisticated error handling in modern languages, you don't need to return ints or bools all over the place.
    There doesn't appear to be any exception handling in the most modern of all computer languages, Google's Go1. s/modern/good/
  • Jay (unregistered) in reply to Maurits
    Maurits:
    Jay:
    who knew 1999 can be written as MCMXCIX, MLMVLIV, MXMIX, MVMIV & MIM.

    MCMXCIX: yes. MLMVLIV: no, VL doesn't work because V is not a power of 10. MXMIX: no, XM doesn't work because M > 10 * X MVMIV: no, VM doesn't work because V is not a power of 10 and M > 10 * V MIM: no, IM doesn't work because M > 10 * I

    You could also write it as MDCCCCLXXXXVIIII which is how the Romans would have written it.

    Since there aren't any rules for Roman numbers (citation needed & all that), all those variations are equally valid.

    Those variations are from Excel; for whatever reason it gives you the option of specifying how long-winded you want to be.

  • (cs) in reply to Brendan
    Brendan:
    Are you suggesting that reading a book on Haskell is a necessary step just to understand Haskell source code?
    Definitely.
    Brendan:
    If so, what does that tell you about Haskell?
    Let's take two ways to write a sum of a list of ten numbers (disregarding the existence of the sum function):

    Haskell:

    foldl1 (+) [1 .. 10]

    Python:

    sum = 0
    for a in range(1, 11):
      sum += a

    Which one would you most easily understand? Obviously the Python one. Which one would you most easily understand once you know what foldl1 does? Obviously the Haskell one: it's all there on one line, it folds 1 to 10 using addition. Choose the right tool for the job: if you want your code to be understood by non-programmers, by all means use a language that's easy to read for non-programmers. If you want your code to be easily understood by programmers, use a language like Haskell.

    Brendan:
    The best way to tell what the compiler generates is to have a look at what the compiler generates. Either post a disassembly so I can laugh at it, or use silence to admit defeat.
    http://pastebin.com/2miyJFji
  • I live in hell (unregistered)

    Since we don't have a new WTF (yet?) today, I thought I'd share one.

    I sat down to dial in to my daily conference call this morning. You know, the one where 18 highly paid people waste an hour each listening while three people discuss the project's lack of progress? I don't typically say more than one sentence a week but there's no cost to the meeting organizer to invite everyone who might slightly be needed so they blast the world. And in our organization the culture is that you just can't decline a meeting invite without a pretty good reason, like being dead.

    So we spend five minutes waiting for the moderator, then having everyone connect and introduce themselves, and we're almost ready to get down to real not-work when the call suddenly hangs up on everyone. What to do, what to do? Start over of course.

    So five minutes later everyone is kicked off again.

    This happens a couple more times before we figure out that maybe we can live through the day without this call. You think?

    Later the word comes back: the problem was we exceeded our license for 120 simultaneous conference calls.

    WTF#1: Spending 5 hours a week waiting for that one moment to answer a question that could be done in 30 seconds by email.

    WTF#2: Multiplying WTF#1 by 18 highly paid people.

    WTF#3: Multiplying WtF#2 by 120 simultaneous calls. Even if we average only 3 people per call, that's 360 people in our organization all discussing at the same time why nobody is getting anything done. And that's this hour. Start over next hour?

    WTF#4: Could there possibly be a less graceful way to handle the error condition "you haven't paid us enough money to do that"?

    WTF#5: Me, I guess, for trying to tolerate all this.

  • Jay (unregistered) in reply to Cap'n Spanky
    Cap'n Spanky:
    Roman Enumeration:
    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.
    I heard that Hitler was a monad.

    Really? Hitler wandered around the wilderness tending sheep?

  • Jay (unregistered) in reply to da Doctah
    da Doctah:
    Somewhat serious followup: Give me a regex that will match only movie titles that end in Roman numerals. Take into account that sometimes there will be a secondary title, as in "Death Warriors IV: the Deaths Continue".

    I tried to do this a few years ago on IMDb to figure out what the highest numbered "sequel" was. Ran into the problem that "mi", "di", "xi" and others are actual words in some languages.

    Did you hear they made a sequel to "Malcolm X"? It is, of course, called "Malcom XI".

  • Jay (unregistered) in reply to Matt Westwood
    Matt Westwood:
    Instead they offer verbal examples culled from textbooks -- 'manipulate an algebraic expression', 'attack a problem', 'exploit a theorem' -- as evidence that mathematics is a nest of aggression, violence, domination and sexism. ... From this they derive postmodern literary dogma such as: 'Mathematics is portrayed as a woman whose nature desires to be the conquered Other.'

    Can we include this text in the description of Algebra II in our course catalog? I think it could really boost enrollment.

  • (cs) in reply to Jay
    Jay:
    Matt Westwood:
    Instead they offer verbal examples culled from textbooks -- 'manipulate an algebraic expression', 'attack a problem', 'exploit a theorem' -- as evidence that mathematics is a nest of aggression, violence, domination and sexism. ... From this they derive postmodern literary dogma such as: 'Mathematics is portrayed as a woman whose nature desires to be the conquered Other.'

    Can we include this text in the description of Algebra II in our course catalog? I think it could really boost enrollment.

    Sure. Let's just get rid of all the women.

  • (cs)

    What this thread really needs is a code competition...

  • Jay (unregistered)

    Side note: Roman numerals do have one obvious advantage: Addition is easy. At least, if you use "old style" Roman numerals where you don't do that "if the next symbol is higher subtract" stuff, so you write 4 as "IIII", etc.

    To add two numbers, just ram the two together and replace any counts that now "carry" with the higher digit, like 5 I's with a V. Sort them if you like, but it doesn't really matter.

    For example: LXVII + XXIII. LVXIIXXIII. IIIII=V so LVXVXX. VV = X so LXXXX. Done. It's probably easier for children to learn.

    Multiplication isn't that tough either. The "times table" is shorter as you only have to deal with 7 symbols -- I, V, X, L, C, D, and M -- instead of 10. I * I = I, I * V = V, ... V * V = XXV, V * X = L, ... X * V = L, X * X = C, X * L = D, ...

    Multiply each digit of one number by each digit of the other. Ram them all together. Then do the "carries". E.g. LXVII * XXIII = DCLXX DCLXX LXVII LXVII LXVII. The first two groups are when we multiply each digit by X, which we must do twice because there are two X's. The other three groups are when we multiply each digit by I, which we do 3 times. Then simplify:

    DCLXX DCLXX LXVII LXVII LXVII IIIII=V: DCLXX DCLXX LXVV LXV LXVI VV=X: DCLXX DCLXX LXX LXX LXI XXXXX=L: DCLL DCL LX LXX LXI LL=C: DCC DCC X CXX XI CCCCC=D: DD D X XX XI DD=M: MDXXXXI

    Of course a Roman would have done several roll-ups in each pass, maybe all in one pass if he was "good at arithmetic".

    I suspect that with some practice, it's easier to do addition, subtraction, and multiplication with Roman numerals than with Hindu-Arabic numerals.

    Division was probably tough. Of course writing arbitrarily large numbers, working with decimal places, and many other functions are more difficult. I'm not suggesting we use Roman numerals. But they're not totally dumb.

  • Jay (unregistered) in reply to Anon
    Anon:
    Jay:
    Anon: That's impossible.

    Nobody said it was impossible. Do you always put words in other people's mouths so you can argue with them?

    You said, "The real WTF is ..." What did that mean other than, "The problem is unsolvable"?

    If you simply meant, "The problem is inadequately defined", well, nobody said that the one sentence given in the article was the complete description or that the employee was not allowed to ask questions.

  • (cs)

    Nobody notice that this code can only go up to 2011. Did program candidate think world going to end after that or did he think nobody is going to remember Roman Number greater than 2011?

    I would like to know his think process.

  • Brendan (unregistered) in reply to briverymouse
    briverymouse:
    Let's take two ways to write a sum of a list of ten numbers (disregarding the existence of the sum function):

    Haskell:

    foldl1 (+) [1 .. 10]

    Python:

    sum = 0
    for a in range(1, 11):
      sum += a

    Which one would you most easily understand? Obviously the Python one.

    Agreed.

    briverymouse:
    Which one would you most easily understand once you know what foldl1 does? Obviously the Haskell one: it's all there on one line, it folds 1 to 10 using addition.

    Wrong. The most natural solution to the problem is a loop ("foreach"). Ask someone who's never programmed anything before how they would do it and I can guarantee their answer won't involve recursion. Once you know what fold1 does it's "clever".

    Note: I use "clever" in a derogatory way. It's like a man who sets up a system of pulleys and levers to push buttons on his remote control just so he can feel superior to everyone else, without realising everyone else thinks he's a moron for not shifting the remote control closer.

    This is the essential problem with Haskell - often the most natural solution isn't suitable in Haskell. You have to spend ages learning how to think "clever" before your mind is so screwed up that unnatural solutions seem acceptable; and only "clever" people are stupid enough to think that is a good idea.

    briverymouse:
    Choose the right tool for the job: if you want your code to be understood by non-programmers, by all means use a language that's easy to read for non-programmers. If you want your code to be easily understood by programmers, use a language like Haskell.

    The right tool for the job is the tool that allows you to express the most natural solution (and get good performance from that). Haskell is the right tool for doing research into compiler optimisations, and is almost never the right tool for any actual programming.

    briverymouse:
    Brendan:
    The best way to tell what the compiler generates is to have a look at what the compiler generates. Either post a disassembly so I can laugh at it, or use silence to admit defeat.
    http://pastebin.com/2miyJFji

    That isn't the piece of code we're talking about. It isn't even Haskell code. You've attempted to prove that apples are yellow and mushy by squashing a banana.

  • (cs) in reply to Brendan
    Brendan:
    briverymouse:
    Let's take two ways to write a sum of a list of ten numbers (disregarding the existence of the sum function):

    Haskell:

    foldl1 (+) [1 .. 10]

    Python:

    sum = 0
    for a in range(1, 11):
      sum += a

    Which one would you most easily understand? Obviously the Python one.

    Agreed.

    Amateur. Professional-level trolls know that the ideal language for comprehension by non-programmers is COBOL.

  • (cs) in reply to Brendan
    Brendan:
    Wrong. The most natural solution to the problem is a loop ("foreach"). Ask someone who's never programmed anything before how they would do it and I can guarantee their answer won't involve recursion. Once you know what fold1 does it's "clever".
    What do you think foldl1 does? It loops over a list from the left, combining the results using the provided function. It's a more specific kind of loop. The fact that it's implemented internally using recursion is completely irrelevant. Externally, it just behaves like a loop with all the plumbing code removed. You see foldl1, (+) and a list, and you know what it does, without having to read 3 lines of code to see what exactly this particular for loop is meant to accomplish.

    Ask someone who's never programmed anything before how they would do it and I can guarantee their answer won't involve a foreach loop either. They will probably say something like "add the first two, then add the next to it, and so on until the end", which is literally what foldl1 does, and not "make a temporary variable, set it to zero, then take every value from the list and add it to the temporary variable". That would be foldl by the way, which also exists.

    briverymouse:
    Brendan:
    The best way to tell what the compiler generates is to have a look at what the compiler generates. Either post a disassembly so I can laugh at it, or use silence to admit defeat.
    http://pastebin.com/2miyJFji

    That isn't the piece of code we're talking about. It isn't even Haskell code. You've attempted to prove that apples are yellow and mushy by squashing a banana.

    What I said was:

    Modern compilers (you know, like the one used to compile Haskell, but also most modern C compilers) will output the exact same processor instructions for a tail-recursive function as a C compiler would for a while loop.
    You asked me to prove it, and I did. What else do you want me to show? I can't show you that Haskell produces the same bytecode as C, because it obviously doesn't: Haskell has an entirely different execution model (lazy evaluation). And I will immediately admit that this sometimes slows Haskell programs down a bit. That's not what you were claiming though, according to you, Haskell's use of recursion slows it down. That's just bullshit.
  • (cs) in reply to briverymouse
    briverymouse:
    Brendan:
    Wrong. The most natural solution ...blah blah blah
    What do you think foldl1 does? It loops over ... blah blah blah
    That isn't the piece of code we're talking about. It isn't even ...blah blah blah
    You asked me to prove it, and I did. What else ... blah blah blah

    I love you guys and this forum!

  • (cs) in reply to Jay
    Jay:
    Since there aren't any rules for Roman numbers (citation needed & all that), all those variations are equally valid.
    I agree that there is no universally accepted set of rules, and the Romans themselves seen to have been pretty lax about these kinds of things. (Medieval folk didn't tend to spell works the same way all the time, either.)

    But part of implementing a feature is settling on a spec. I am proposing that the following set of rules from Wikipedia be adopted when converting Arabic to Roman. The advantage of this set of rules is that the resulting output is unique. (A different, more all-encompassing set of rules could be applied to converting Roman to Arabic; or the same strict set of rules could be used, depending on the scenario.)

    http://en.wikipedia.org/wiki/Roman_numerals The symbols "I", "X", "C", and "M" can be repeated three times in succession, but no more. (They may appear more than three times if they appear non-sequentially, such as XXXIX.) "D", "L", and "V" can never be repeated. "I" can be subtracted from "V" and "X" only. "X" can be subtracted from "L" and "C" only. "C" can be subtracted from "D" and "M" only. "V", "L", and "D" can never be subtracted Only one small-value symbol may be subtracted from any large-value symbol.
  • Nagesh (unregistered)

    Computer science should being more concurned with conversion of Arabian Numerals (but invention in India) -> to binary.

  • Franz Kafka (unregistered) in reply to CarnivorousHippie
    CarnivorousHippie:
    da Doctah:
    Somewhat serious followup: Give me a regex that will match only movie titles that end in Roman numerals. Take into account that sometimes there will be a secondary title, as in "Death Warriors IV: the Deaths Continue".

    I tried to do this a few years ago on IMDb to figure out what the highest numbered "sequel" was. Ran into the problem that "mi", "di", "xi" and others are actual words in some languages.

    Was this before or after In the Mix?

    simple solution: use a loose regex to get all the sequels, then eliminate entries where prior sequels aren't present - 'In the MVIII' isn't there so 'In the Mix' isn't a sequel.

  • apoc9 (unregistered)

    Java implementation, only valid roman numerals, throws stadnard exception, single function. It is not so trivial, because the rules for valid numerals.

    public static int decodeRomanNumeral(String numeral) {
         int result = 0;
         if (!numeral.matches("^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$")) {
               throw new NumberFormatException("For input string: \"" + numeral + '"');
         }
    		char[] chars = numeral.toCharArray();
    		int p = 0;
    		for (int i = chars.length - 1; i >= 0; i--) {
    			int ch = 0;
    			switch (chars[i]) {
    			case 'I':
    				ch = 1;
    				break;
    			case 'V':
    				ch = 5;
    				break;
    			case 'X':
    				ch = 10;
    				break;
    			case 'L':
    				ch = 50;
    				break;
    			case 'C':
    				ch = 100;
    				break;
    			case 'D':
    				ch = 500;
    				break;
    			case 'M':
    				ch = 1000;
    				break;
    			default:
    				throw new NumberFormatException("For input string: \"" + numeral + '"');
    			}			
    			if (ch >= p) {
    				p = ch;
    				result += ch;
    			} else {
    				result -= ch;
    			}
    		}
    		return result;
    	}
  • Remy Martin (unregistered)

    IMPORTANT ANNOUNCEMENT:

    Alex is in the hospital in an alcohol-poisoning-induced coma. He did not leave me with the access codes, so I cannot post a new article. I encourage you to try the "Random Article" link, or to post a few more implementations of the Roman Numeral conversion libraries in whatever esotaric language you would like.

  • Captain WTF (unregistered)

    And you call yourselves WTFers ...

    You're all reinventing the wheel. Not only is this wheel square it's made out of balsa wood!

    package com.thedailywtf;
    
    public class RomanNumbers {
    	private String number;
    
    	public RomanNumbers() {
    		setNumber(null);
    	}
    
    	public RomanNumbers(String number) {
    		setNumber(number);
    	}
    
    	public int toArabic() {
    		StringBuffer urlString = new StringBuffer();
    		urlString.append("http://www.google.com/search?q=");
    		urlString.append(this.number);
    		urlString.append("+in+decimal");
    
    		try {
    			java.net.URL url = new java.net.URL(urlString.toString());
    			java.net.URLConnection urlConnection = url.openConnection();
    			urlConnection.setRequestProperty("User-agent",
    					"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0");
    
    			java.io.BufferedInputStream bis = new java.io.BufferedInputStream(urlConnection.getInputStream());
    
    			StringBuffer googleHtml = new StringBuffer();
    			byte[] googleBits = new byte[1024];
    			int bitsRead = 0;
    			while ((bitsRead = bis.read(googleBits)) > -1) {
    				googleHtml.append(new String(googleBits, 0, bitsRead));
    			}
    
    			String myHtml = googleHtml.toString();
    
    			int pos = myHtml.indexOf("
    "); myHtml = myHtml.substring(pos); pos = myHtml.indexOf(""); int pos2 = myHtml.indexOf("", pos); myHtml = myHtml.substring(pos, pos2); pos = myHtml.lastIndexOf(" = "); String answer = myHtml.substring(pos + 3).trim(); return Integer.parseInt(answer); } catch (java.net.MalformedURLException e) { e.printStackTrace(); return 0; } catch (java.io.IOException e) { e.printStackTrace(); return 0; } } public String getNumber() { return this.number; } public void setNumber(String number) { if (number == null) this.number = null; else this.number = number.toUpperCase(); } }

Leave a comment on “Roman Enumeration”

Log In or post as a guest

Replying to comment #:

« Return to Article