all 51 comments

sorted by: hot top controversial new old
[–] 115 points 1 year ago* (4 children)

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 😂

  • source
  • hideshow 4 child comments
  • [–] 32 points 1 year ago (3 children)

    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.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 40 points 1 year ago (1 child)

    Why don't you just ask Chat-GPT o3 every time? Works like a charm!

  • source
  • hideshow 1 child comment
  • [–] 67 points 1 year ago (11 children)

    Still linear time at least, could always be much MUCH worse

  • source
  • hideshow 11 child comments
  • [–] 43 points 1 year ago (2 children)

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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 16 points 1 year ago (7 children)

    True. Lost opportunity to blow things up with useless recursivity

  • source
  • parent
  • hideshow 7 child comments
  • [–] 16 points 1 year ago (6 children)

    The word you’re looking for is recursion (see recursion).

  • source
  • parent
  • hideshow 6 child comments
  • [–] 6 points 1 year ago (4 children)

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

  • source
  • parent
  • hideshow 4 child comments
  • [–] 10 points 1 year ago (3 children)

    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.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 4 points 1 year ago (1 child)

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

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 1 year 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

  • source
  • parent
  • [–] 63 points 1 year ago (1 child)

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

  • source
  • hideshow 1 child comment
  • [–] 15 points 1 year 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.

  • source
  • parent
  • [–] 30 points 1 year ago*

    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.

  • source
  • [–] 18 points 1 year ago

    Since Roman numerals have an upper bound, the time complexity is always O(1).

  • source
  • [–] 8 points 1 year ago* (last edited 1 year ago) (6 children)

    It's got some code duplication. Who can code gulf golf this?

  • source
  • hideshow 6 child comments
  • [–] 3 points 1 year ago (1 child)
    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();
    }
    
  • source
  • parent
  • hideshow 1 child comment
  • [–] [S] 7 points 1 year ago
    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();
    }
    
  • source
  • parent
  • [–] 2 points 1 year ago

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

  • source
  • parent
  • [–] 6 points 1 year ago

    Alternatively pip install roman.

  • source
  • [–] 6 points 1 year ago (2 children)

    Should do a regex find all then iterate over each chunk recursively until unchanged.

  • source
  • hideshow 2 child comments
  • [–] 5 points 1 year ago (1 child)

    I just wrote something similar for decoding binary asm instructions.

  • source
  • hideshow 1 child comment
  • [–] 9 points 1 year ago (1 child)

    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.

  • source
  • hideshow 1 child comment
  • [–] 15 points 1 year ago (1 child)

    They forgot "CM" so this doesn't work for any number that ends in 900s

  • source
  • hideshow 1 child comment
  • [–] 3 points 1 year ago
    [–] 3 points 1 year ago (1 child)

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

  • source
  • hideshow 1 child comment
  • [–] 5 points 1 year ago

    until(original=new) { run convertOriginal }

  • source
  • [–] 6 points 1 year ago (3 children)
  • [–] 32 points 1 year ago* (2 children)

    IIV would never be used. In Roman numerals at most one smaller unit can come in front of a larger one. The code doesn't do any validation though.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 3 points 1 year ago (2 children)

    Depending on the language, you may be mutating the input value, which isn't great.

  • source
  • hideshow 2 child comments