Spyke

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 😂

120
balsoftreply
lemmy.ml

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.

32

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.

4
dfyxreply
lemmy.helios42.de

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

44

Not quadratic in the length of the input. Assuming replace is linear this is also linear

5
lugalreply
lemmy.dbzer0.com

True. Lost opportunity to blow things up with useless recursivity

17
bleistift2reply
sopuli.xyz

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

17
Gonzakoreply
lemmy.world

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

6
Cethinreply
lemmy.zip

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.

10
kamstrupreply
programming.dev

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

4
Epherareply
lemmy.ml

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

4

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

1

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

65

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.

15
lemmy.world

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

41

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.

31
feddit.org

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.

22
lugalreply
lemmy.dbzer0.com

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

7
Atlas_reply
lemmy.world

Also does not handle 'IIIIIIIII' -> 'IX' properly

6
roorooreply
feddit.org

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

2
lugalreply
lemmy.dbzer0.com

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

0
roorooreply
feddit.org

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

1
lugalreply
lemmy.dbzer0.com

Does 9 really work? Wouldn't it be:

IIIIIIIII
-> VIIII
-> VIV
1

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.

2
roorooreply
feddit.org

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.

2

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.

2

I noticed my “and so on” is literally a noop here so yeah.

1
lemmy.world

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

14

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
  5. ......
50
lemmy.world

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.

9
public static int convertRomanNumeral(String numeral) {
    return 4; // todo
}
48
gruereply
lemmy.world

Code gulf, you say?

public static String
convertRomanNumeral(String numeral) {
    numeral = numeral.replace("America", "Mexico");
    return numeral;
} 
27
rayreply
sh.itjust.works
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();
}
3
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();
}
7

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

2
lemmy.world

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.

33
lemmy.ca

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

6

Actually there were seven kings prior to the establishment of the republic, at which point they expelled the rulers... a reg-ex if you will.

6
lemmy.ml

I just wrote something similar for decoding binary asm instructions.

5
qazreply
lemmy.world

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

2
lemmy.world

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

3

No, they didn't.

CM becomes CDD, which becomes CCCCD which becomes CCCCCCCCC.

9