fr

Daily Challenge 🗓️

Your daily dose of technical intelligence to shine in (developer) society.

Discover a software development quiz per day with its detailed correction.

Frontend, Backend, Mobile, Data, Web, AI, Infra... Everything that can be coded!

Friday 31 July 2026

-

Java and Craftsmanship

Java and Craftsmanship #11/24

This code

public class Numerals {

  private static final NavigableMap<Integer, String> CONVERSIONS = buildConversions();

  private Numerals() {}

  public static String toRoman(int arabic) {
    return highestKnownConversion(arabic).map(toRomanRepresentation(arabic)).orElse("");
  }

  private static Optional<Entry<Integer, String>> highestKnownConversion(int arabic) {
    return Optional.ofNullable(CONVERSIONS.floorEntry(arabic));
  }

  private static Function<Entry<Integer, String>, String> toRomanRepresentation(int arabic) {
    return conversion -> conversion.getValue() + toRoman(arabic - conversion.getKey());
  }

  private static NavigableMap<Integer, String> buildConversions() {
    NavigableMap<Integer, String> conversions = new TreeMap<>();

    conversions.put(1, "I");
    conversions.put(4, "IV");
    conversions.put(5, "V");
    conversions.put(9, "IX");
    conversions.put(10, "X");

    return conversions;
  }
}
Author: W3D TeamStatus: PublishedQuestion passed 484 times
1
Reviews from developersNo one has reviewed this question yet, be the first!

About Daily Challenge 🗓️

From Monday to Friday, discover each day a quiz question with its detailed answer to enrich your tech knowledge. The questions are proposed by the WeLoveDevs.com community: experienced developers, trainers and freelancers.WeLoveDevs.com helps developers to find their dream job. We also provide over 50 free technical tests in different technologies for those of you who would like to test your knowledge.

History

    ⚠️
    Your browser is badly|not supported!
    We recommend you to use a more modern browser such as Edge, Chrome or Firefox
    Know More