Since Roman numerals have an upper bound, the time complexity is always O(1).
Programmer Humor
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
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
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.
My first thought was that it'd be a great oracle for randomized testing.
Alternatively pip install roman.
You missed "CM," which was common in copyright statements in the 20th century.
No, they didn't.
CM becomes CDD, which becomes CCCCD which becomes CCCCCCCCC.
unary!
Depending on the language, you may be mutating the input value, which isn't great.
I'm pretty sure it's Java (due to the syntax and Eclipse editor default color scheme), so that isn't an issue
Oh right, my Java is a bit rusty. But if it was Javascript, that would have been a problem.
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 😂
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.
I'd accept that "smart code" and "clever code" are 2 different things
Fast inverse square root eh?
Still linear time at least, could always be much MUCH worse
There could be a hidden quadratic cost because the string needs to be reallocated and copied multiple times.
Not if I don't see it.
This is the spirit
True. Lost opportunity to blow things up with useless recursivity
The word you’re looking for is recursion (see recursion).
Thanks. I knew something was off
This isn’t sufficiently enterprisey for Java. There should be a Roman numeral factory followed by relevant fromString and toInteger methods.
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.
Why don't you just ask Chat-GPT o3 every time? Works like a charm!
Because there are better random generators
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.
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.
They forgot "CM" so this doesn't work for any number that ends in 900s
No, M will be replaced by DD and then CD will be picked up, so it will go
- CM
- CDD
- CCCCD
- CCCCCCCCC
- ......