• trxxruraxvr@lemmy.world
      link
      fedilink
      arrow-up
      49
      ·
      edit-2
      1 month ago

      No, M will be replaced by DD and then CD will be picked up, so it will go

      1. CM
      2. CDD
      3. CCCCD
      4. CCCCCCCCC
        • Gonzako@lemmy.world
          link
          fedilink
          arrow-up
          7
          arrow-down
          1
          ·
          1 month ago

          Nah, I’d like to un-see recursion. It was way overblown on uni, I barely ever use it.

          • Cethin@lemmy.zip
            link
            fedilink
            English
            arrow-up
            10
            ·
            1 month ago

            Recursion is amazing for a small selection of problems. Most of the time you don’t need, or want, it. When it is useful though, it tends to be really useful.

            I don’t understand people’s issue with it. I always found it easy. Maybe that’s why I feel this way. Maybe if you find it challenging you want to avoid it, even when it’s a good solution.

            • kamstrup@programming.dev
              link
              fedilink
              arrow-up
              1
              ·
              1 month ago

              Most devs I know like recursion. Trouble is that many popular languages don’t support tail recursion, but throw a stackoverflow error after a few thousand levels. So you have to keep track of max recursion depth manually, and it starts to look like a complicated solution

            • kamstrup@programming.dev
              link
              fedilink
              arrow-up
              4
              ·
              1 month ago

              Most devs I know like recursion. Trouble is that many popular languages don’t support tail recursion, but throw a stackoverflow error after a few thousand levels. So you have to keep track of max recursion depth manually, and it starts to look like a complicated solution

            • Ephera@lemmy.ml
              link
              fedilink
              English
              arrow-up
              4
              ·
              1 month ago

              I think, their point (and also my experience) is that you get taught about it in university a lot more than about simple loops, so it feels more important even though you rarely use it in reality.

              Same thing goes for linked lists and inheritance…

    • dfyx@lemmy.helios42.de
      link
      fedilink
      arrow-up
      44
      ·
      1 month ago

      There could be a hidden quadratic cost because the string needs to be reallocated and copied multiple times.

  • mkwt@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    1 month ago

    You missed “CM,” which was common in copyright statements in the 20th century.

  • Caveman@lemmy.world
    link
    fedilink
    arrow-up
    10
    arrow-down
    1
    ·
    1 month ago

    It’s not too bad, it’s readable and easily optimised by adding intermediate sums and removing whatever power of 10 you’re working on.

  • CookieOfFortune@lemmy.world
    link
    fedilink
    arrow-up
    66
    arrow-down
    1
    ·
    1 month ago

    This isn’t sufficiently enterprisey for Java. There should be a Roman numeral factory followed by relevant fromString and toInteger methods.

    • vithigar@lemmy.ca
      link
      fedilink
      arrow-up
      16
      arrow-down
      1
      ·
      1 month ago

      Ugh. Literally refactored multiple factories into straightforward functions in the most recent sprint where I work.

      Someone saw a public factory method which was a factory for a reason and just cargo culted multiple private methods using the same pattern.

  • rooroo@feddit.org
    link
    fedilink
    arrow-up
    23
    arrow-down
    1
    ·
    1 month ago

    It also works the other way round: wanna convert Arabic n to Roman? Just write n times ‘I’ and revert these replacement in inverse order.

    • lugal@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      7
      ·
      1 month ago

      I don’t know what happens when the substring overlaps. Like for the number 6, will it replace the first 5 I’s with V and end up correctly with VI or the last ones and come to IV? I would guess the former and maybe you know but I never thought about it before

      • rooroo@feddit.org
        link
        fedilink
        arrow-up
        2
        ·
        1 month ago

        I’ve written it that way and it works, as in it will replace left to right and you replace iiii to iv after iiiii to v

        • lugal@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          0
          ·
          1 month ago

          Makes sense but it will fail at 9 (VIV) it would only work for 9 if the replace went from right to left or the V and IV statements were exchanged but in both cases, 6 would fail

          • rooroo@feddit.org
            link
            fedilink
            arrow-up
            1
            ·
            30 days ago

            9 is IX though, and that works.

            6 works fine, as it replaces the first set of 5 I with V and then there’s nothing to replace.

            I’d written it in typescript for all it’s worth; go ahead and try it yourself :)

              • rooroo@feddit.org
                link
                fedilink
                arrow-up
                2
                ·
                30 days ago

                I like your questions about this and they all seem fair but I kinda wanna encourage you to go ahead and write it yourself; it’s a fun way to convert into Roman numerals that both is and isn’t intuitive at the same time.

              • rooroo@feddit.org
                link
                fedilink
                arrow-up
                2
                ·
                30 days ago

                No, cause you do the replacement from large to small, I.e. you’d first check for 10 I to replace with X (none found); then replace 9 with IX (check), then check for 5, 4 and so on.

                • lugal@lemmy.dbzer0.com
                  link
                  fedilink
                  arrow-up
                  2
                  ·
                  30 days ago

                  The original doesn’t have an extra check for 9 and it works for Roman->Indioarabic because it’s:

                  IX
                  ->IVV
                  ->IIIIV
                  ->IIIIIIIII
                  

                  But the other way around, you need an extra step for 9. That’s where our misunderstanding comes from.

    • qaz@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 month ago

      I’m pretty sure it’s Java (due to the syntax and Eclipse editor default color scheme), so that isn’t an issue