It's got some code duplication. Who can code ~~gulf~~ golf this?
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
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();}
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();
}
According to this code, "CEREAL" is a valid Roman numeral which equals 154. Great job!
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
Should do a regex find all then iterate over each chunk recursively until unchanged.
I just wrote something similar for decoding binary asm instructions.
If you have the time it's a good solution!
until(original=new) { run convertOriginal }