Roman Numerals Helper

kata programming

مساله:

کلاس RomanNumerals را ایجاد کنید که بتواند از یک عدد فرمت Roman Numeral را ایجاد کند و همچنین روند برعکس را انجام دهد.

برای مثال:

RomanNumerals.toRoman(1000) // should return 'M'
RomanNumerals.fromRoman("M") // should return 1000

Description:

Create a RomanNumerals class that can convert a roman numeral to and from an integer value. It should follow the API demonstrated in the examples below. Multiple roman numeral values will be tested for each helper method.

Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI.

Examples

RomanNumerals.toRoman(1000) // should return 'M'
RomanNumerals.fromRoman("M") // should return 1000

Help

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

import java.util.LinkedHashMap;
import java.util.Map;

public class RomanNumerals {
 
  public static LinkedHashMap<String, Integer> getRomanNumerals() {
    LinkedHashMap<String, Integer> roman_numerals = new LinkedHashMap<String, Integer>();
    roman_numerals.put("M", 1000);
    roman_numerals.put("CM", 900);
    roman_numerals.put("D", 500);
    roman_numerals.put("CD", 400);
    roman_numerals.put("C", 100);
    roman_numerals.put("XC", 90);
    roman_numerals.put("L", 50);
    roman_numerals.put("XL", 40);
    roman_numerals.put("X", 10);
    roman_numerals.put("IX", 9);
    roman_numerals.put("V", 5);
    roman_numerals.put("IV", 4);
    roman_numerals.put("I", 1);
    return roman_numerals;
  }
  
  public static int fromRoman(String romanNumeral) {
    LinkedHashMap<String, Integer> roman_numerals = getRomanNumerals();
    String[] arr = romanNumeral.split("");
    int result = 0;
    for (int i=0; i < romanNumeral.length(); i++) {
      if (i+1 < romanNumeral.length() && roman_numerals.containsKey(arr[i] + arr[i+1])) {
        result += roman_numerals.get(arr[i] + arr[i+1]);
        i++;
      } else {
        result += roman_numerals.get(arr[i]);
      }
    }
    
    return result;
  }

  public static String toRoman(int n) {
    LinkedHashMap<String, Integer> roman_numerals = getRomanNumerals();
    String res = "";
    for (Map.Entry<String, Integer> entry : roman_numerals.entrySet()) {
      int matches = n / entry.getValue();
      res += repeat(entry.getKey(), matches);
      n = n % entry.getValue();
    }
    return res;
  }

  public static String repeat(String s, int n) {
    if (s == null) {
      return null;
    }
    final StringBuilder sb = new StringBuilder();
    for (int i = 0; i < n; i++) {
      sb.append(s);
    }
    return sb.toString();
  }
}
public class RomanNumerals {
    private static final String[] ROMAN_NUMBERS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    private static final int[] ARABIC_NUMBERS = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

    public static String toRoman(int n) {
        int remainingValue = n;
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < ARABIC_NUMBERS.length; i++) {
            while (remainingValue >= ARABIC_NUMBERS[i]) {
                remainingValue -= ARABIC_NUMBERS[i];
                result.append(ROMAN_NUMBERS[i]);
            }
        }

        return result.toString();
    }

    public static int fromRoman(String romanNumeral) {
        String remainingValue = romanNumeral;
        int result = 0;

        for(int i = 0; i<ROMAN_NUMBERS.length; i++) {
            while(remainingValue.startsWith(ROMAN_NUMBERS[i])) {
                remainingValue = remainingValue.substring(ROMAN_NUMBERS[i].length(), remainingValue.length());
                result += ARABIC_NUMBERS[i];
            }
        }
        return result;
    }
}
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class RomanNumerals {

    private static final Map<Integer, String> CONVERSIONS;
    static {
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1000, "M");
        map.put(900, "CM");
        map.put(500, "D");
        map.put(400, "CD");
        map.put(100, "C");
        map.put(90, "XC");
        map.put(50, "L");
        map.put(40, "XL");
        map.put(10, "X");
        map.put(9, "IX");
        map.put(5, "V");
        map.put(4, "IV");
        map.put(1, "I");
        CONVERSIONS = Collections.unmodifiableMap(map.descendingMap());
    }

    public static String toRoman(int n) {
        StringBuilder result = new StringBuilder();
        for (Map.Entry<Integer, String> entry : CONVERSIONS.entrySet()) {
            while (n >= entry.getKey()) {
                result.append(entry.getValue());
                n -= entry.getKey();
            }
        }
        return result.toString();
    }

    public static int fromRoman(String romanNumeral) {
        int result = 0;
        for (Map.Entry<Integer, String> entry : CONVERSIONS.entrySet()) {
            while (romanNumeral.startsWith(entry.getValue())) {
                result += entry.getKey();
                romanNumeral = romanNumeral.substring(entry.getValue().length());
            }
        }
        return result;
    }
}
public class RomanNumerals {
 
  private static final Nums[] nums;
    static
    {
        nums = Nums.values();
    }

    public enum Nums
    {
        M(1000, "M"), CM(900, "CM"), D(500, "D"), CD(400, "CD"),
        C(100, "C"), XC(90, "XC"), L(50, "L"), XL(40, "XL"),
        X(10, "X"), IX(9, "IX"), V(5, "V"), IV(4, "IV"),
        I(1, "I");

        private final int num;
        private final String symbol;

        Nums(int num, String symbol)
        {
            this.num = num;
            this.symbol = symbol;
        }
    }


    public static String toRoman(int n)
    {
        StringBuilder result = new StringBuilder();

        for(Nums num: nums)
        {
            while (n >= num.num)
            {
                n -= num.num;
                result.append(num.symbol);
            }
        }

        return result.toString();
    }

    public static int fromRoman(String romanNumeral)
    {
        int result = 0;
        Nums[] nums = transform(romanNumeral);

        Nums bigger = nums[nums.length - 1];
        for(int i = nums.length - 1; i >= 0; --i)
        {
            if(bigger.num > nums[i].num)
                result -= nums[i].num;
            else
            {
                bigger = nums[i];
                result += nums[i].num;
            }
        }

        return result;
    }

    private static Nums[] transform(String romanNumeral)
    {
        Nums[] nums = new Nums[romanNumeral.length()];

        for(int i = 0; i < nums.length; i++)
            nums[i] = Nums.valueOf(Character.toString(romanNumeral.charAt(i)));

        return nums;
    }
}
import java.util.LinkedHashMap;
import java.util.Map;

public class RomanNumerals {
 
    private static LinkedHashMap<String, Integer> MAP;
    static {
        MAP = new LinkedHashMap<String, Integer>();
        MAP.put( "M"  , 1000 );
        MAP.put( "CM" , 900  );
        MAP.put( "D"  , 500  );
        MAP.put( "CD" , 400  );
        MAP.put( "C"  , 100  );
        MAP.put( "XC" , 90   );
        MAP.put( "L"  , 50   );
        MAP.put( "XL" , 40   );
        MAP.put( "X"  , 10   );
        MAP.put( "IX" , 9    );
        MAP.put( "V"  , 5    );
        MAP.put( "IV" , 4    );
        MAP.put( "I"  , 1    );
    }
  
    public static String toRoman(int n) {
        StringBuilder builder = new StringBuilder();
        for(Map.Entry<String, Integer> entry: MAP.entrySet() ){
            int i = entry.getValue();
            String s = entry.getKey();
            while( n>=i ){
                builder.append(s);
                n -= i;
            }
        }
        return builder.toString();
    }
    public static int fromRoman(String romanNumeral) {
        int tempAfter = 0;
        char[] cs = romanNumeral.toCharArray();
        int result = 0;
        for (char c : cs) {
            String roma = String.valueOf(c);
            int num = MAP.get(roma);
            if (num <= tempAfter || tempAfter == 0) {
                result += num;
                tempAfter = num;
            }else {
                result -= tempAfter;
                result += (num - tempAfter);
            }
        }
        return result;
    }
}

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد.