you are viewing a single comment's thread
view the rest of the comments
[–] 8 points 1 year ago* (last edited 1 year ago) (6 children)

It's got some code duplication. Who can code gulf golf this?

  • source
  • hideshow 6 child comments
  • [–] 3 points 1 year ago (1 child)
    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();
    }
    
  • source
  • parent
  • hideshow 1 child comment
  • [–] [S] 7 points 1 year ago
    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();
    }
    
  • source
  • parent
  • [–] 2 points 1 year ago

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

  • source
  • parent