codewars060 - Resistor Color Codes, Part 2

Instructions

https://www.codewars.com/kata/5855777bb45c01bada0002ac/train/java

Solution

import java.util.HashMap;
import java.util.Map;
public class EncodeResistorColors {
private static Map<String,String> map = new HashMap<String, String>();
	
	static {
		map.put("0", "black ");
		map.put("1", "brown ");
		map.put("2", "red ");
		map.put("3", "orange ");
		map.put("4", "yellow ");
		map.put("5", "green ");
		map.put("6", "blue ");
		map.put("7", "violet ");
		map.put("8", "gray ");
		map.put("9", "white ");

	}
    public static String encodeResistorColors(String ohmsString) {

		String[] arr = ohmsString.split(" ");
		String num = arr[0];
		if(num.contains("k")){
			//num = num.replace("k", "000");
			float temp = Float.valueOf(num.substring(0, num.length() - 1)) * 1000;
			num = String.valueOf((int)temp);
		}
		if(num.contains("M")){
			//num = num.replace("M", "000000");
			float temp = Float.valueOf(num.substring(0, num.length() - 1)) * 1000000;
			num = String.valueOf((int)temp);
		}
		StringBuilder sb = new StringBuilder();
		sb.append(map.get(String.valueOf(num.charAt(0))));
		sb.append(map.get(String.valueOf(num.charAt(1))));
		int len = num.length();
		sb.append(map.get(String.valueOf(len - 2)));
		sb.append("gold");

		return sb.toString();
	
    }
}

Sample Tests:

https://www.codewars.com/kata/5855777bb45c01bada0002ac/train/java
相關文章
相關標籤/搜索