• Caveman@lemmy.world
    link
    fedilink
    arrow-up
    5
    ·
    13 小时前

    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.

  • mkwt@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    13 小时前

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

  • CookieOfFortune@lemmy.world
    link
    fedilink
    arrow-up
    55
    arrow-down
    1
    ·
    1 天前

    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
      11
      ·
      19 小时前

      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.

  • theunknownmuncher@lemmy.world
    link
    fedilink
    arrow-up
    99
    arrow-down
    1
    ·
    edit-2
    1 天前

    Whenever you sit back and smile proudly to yourself about how clever the block of code you just wrote is, your next move should be to delete and rewrite it.

    This is a clever block of code! Great job, now rewrite it to be sane 😂

    • balsoft@lemmy.ml
      link
      fedilink
      arrow-up
      27
      ·
      1 天前

      I think it depends; some smart code is good actually, think 0x5f3759df. As long as you properly document it and leave plenty of comments. This one is not smart though, at best it’s what I would call witty.

      • Cethin@lemmy.zip
        link
        fedilink
        English
        arrow-up
        4
        ·
        19 小时前

        This isn’t smart. This is clever. It’s a way to solve a problem in a novel way. It isn’t the best, or even most obvious, way to solve the problem. It’s just interesting.

    • dfyx@lemmy.helios42.de
      link
      fedilink
      arrow-up
      36
      ·
      1 天前

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

        • Gonzako@lemmy.world
          link
          fedilink
          arrow-up
          6
          arrow-down
          1
          ·
          1 天前

          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
            9
            ·
            19 小时前

            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
              3
              ·
              16 小时前

              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
              ·
              18 小时前

              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…

            • kamstrup@programming.dev
              link
              fedilink
              arrow-up
              1
              ·
              16 小时前

              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

  • TootSweet@lemmy.world
    link
    fedilink
    English
    arrow-up
    27
    ·
    edit-2
    1 天前

    My first thought was something along the lines of a “zip bomb”. For every “M” in the input string, it’d use more than a KiB of memory. But still, it’d take a string of millions of "M"s to exhaust memory on even a low-end modern server. Still probably not a good idea to expose to untrusted input on a public networked server, though. And it could easily peg a CPU core for a good while. Very good leveraged target for DDOSing.

  • rooroo@feddit.org
    link
    fedilink
    arrow-up
    21
    arrow-down
    1
    ·
    1 天前

    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
      6
      ·
      1 天前

      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

    • qaz@lemmy.worldOP
      link
      fedilink
      English
      arrow-up
      2
      ·
      14 小时前

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

    • grue@lemmy.world
      link
      fedilink
      arrow-up
      25
      ·
      edit-2
      1 天前

      Code gulf, you say?

      public static String
      convertRomanNumeral(String numeral) {
          numeral = numeral.replace("America", "Mexico");
          return numeral;
      } 
      
    • TheLazyNerd@europe.pub
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      15 小时前

      I’m not too good with java, but it should be something like this:

      public static int convertRomanNumeral(string n){Map.of("M","DD","CD","CCCC","D","CCCCC","C","LL","XL","XXXX","L","XXXXX","X","VV","IV","IIII","V","IIIII");.forEach((k,v)->{n=n.replace(k,v);});return n.length();}

    • ray@sh.itjust.works
      link
      fedilink
      arrow-up
      2
      ·
      24 小时前
      public static int convertRomanNumeral(String numeral)
      {
        numeral = numeral.replace("M", "DD")
          .replace("CD", "CCCC")
          .replace("D", "CCCCC")
          .replace("C", "LL")
          .replace("XL", "XXXX")
          .replace("L", "XXXXX")
          .replace("X", "VV")
          .replace("IV", "IIII")
          .replace("V", "IIIII");
        return numeral.length();
      }
      
      • qaz@lemmy.worldOP
        link
        fedilink
        English
        arrow-up
        6
        ·
        20 小时前
        public static int convertRomanNumeral(String numeral)
        {
          return numeral.replace("M", "DD")
            .replace("CD", "CCCC")
            .replace("D", "CCCCC")
            .replace("C", "LL")
            .replace("XL", "XXXX")
            .replace("L", "XXXXX")
            .replace("X", "VV")
            .replace("IV", "IIII")
            .replace("V", "IIIII")
            .length();
        }