391
top 50 comments
sorted by: hot top controversial new old
[-] Wynnstan@lemmy.world 1 points 7 hours ago

Alternatively pip install roman.

[-] TheLazyNerd@europe.pub 13 points 17 hours ago

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

[-] olafurp@lemmy.world 5 points 15 hours 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.

[-] nik9000@programming.dev 1 points 14 hours ago

My first thought was that it'd be a great oracle for randomized testing.

[-] CookieOfFortune@lemmy.world 54 points 1 day 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 10 points 21 hours 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.

[-] mkwt@lemmy.world 2 points 14 hours ago

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

[-] webadict@lemmy.world 3 points 7 hours ago

No, they didn't.

CM becomes CDD, which becomes CCCCD which becomes CCCCCCCCC.

[-] Zyansheep@programming.dev 2 points 15 hours ago
[-] theunknownmuncher@lemmy.world 98 points 1 day ago* (last edited 1 day ago)

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 27 points 1 day ago

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 4 points 21 hours ago

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.

[-] theunknownmuncher@lemmy.world 20 points 1 day ago

I'd accept that "smart code" and "clever code" are 2 different things

[-] Valmond@lemmy.world 9 points 1 day ago

Fast inverse square root eh?

[-] douglasg14b@lemmy.world 55 points 1 day ago

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

[-] dfyx@lemmy.helios42.de 36 points 1 day ago

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

[-] Jerkface@lemmy.world 34 points 1 day ago
[-] aaaaaaaaargh@feddit.org 8 points 23 hours ago

This is the spirit

[-] kogasa@programming.dev 4 points 21 hours ago

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

[-] lugal@lemmy.dbzer0.com 16 points 1 day ago

True. Lost opportunity to blow things up with useless recursivity

[-] bleistift2@sopuli.xyz 15 points 1 day ago

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

[-] lugal@lemmy.dbzer0.com 5 points 20 hours ago

Thanks. I knew something was off

[-] Gonzako@lemmy.world 5 points 1 day ago

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

[-] Cethin@lemmy.zip 9 points 21 hours 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 3 points 18 hours 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 4 points 19 hours 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...

[-] embed_me@programming.dev 2 points 14 hours ago

Linked lists are encountered somewhat frequently in low level systems programming.

[-] kamstrup@programming.dev 1 points 18 hours 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

[-] anugeshtu@lemmy.world 37 points 1 day ago

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

[-] lugal@lemmy.dbzer0.com 44 points 1 day ago

Because there are better random generators

[-] TootSweet@lemmy.world 27 points 1 day ago* (last edited 1 day 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.

[-] rooroo@feddit.org 20 points 1 day 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.

load more comments (4 replies)
[-] BlackEco@lemmy.blackeco.com 2 points 19 hours ago

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

[-] qaz@lemmy.world 2 points 15 hours ago

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

[-] BlackEco@lemmy.blackeco.com 2 points 12 hours ago* (last edited 12 hours ago)

Oh right, my Java is a bit rusty. But if it was Javascript, that would have been a problem.

[-] Zangoose@lemmy.world 13 points 1 day ago

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

[-] trxxruraxvr@lemmy.world 48 points 1 day ago* (last edited 1 day 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
  5. ......
[-] itsraining@lemmygrad.ml 6 points 1 day ago

According to this code, "CEREAL" is a valid Roman numeral which equals 154. Great job!

load more comments
view more: next ›
this post was submitted on 11 Oct 2025
391 points (100.0% liked)

Programmer Humor

26827 readers
1659 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS