This isn’t sufficiently enterprisey for Java. There should be a Roman numeral factory followed by relevant fromString and toInteger methods.
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
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
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 quadratic in the length of the input. Assuming replace is linear this is also linear
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
Nah, I'd like to un-see recursion. It was way overblown on uni, I barely ever use it.
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.
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 😂
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.
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
Also does not handle 'IIIIIIIII' -> 'IX' properly
If the substitution went right to left it might work.
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.
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
- ......
IIV becomes IIIII, hm?
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.
While it doesn't say anything about IIV specifically, they sure got creative enough to sometimes subtract more than one of the smaller units from a larger one.
Yes, that does demonstrate my point.
It's got some code duplication. Who can code gulf this?
Code gulf, you say?
public static String
convertRomanNumeral(String numeral) {
numeral = numeral.replace("America", "Mexico");
return numeral;
}
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();
}
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();
}
public static int convertRomanNumeral(String numeral) {
return 4; // todo
}
until(original=new) { run convertOriginal }
Should do a regex find all then iterate over each chunk recursively until unchanged.
there was no regex in ancient rome
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.