For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
Nick Parlante updated CodingBat with two new sets of Java exercises, which both focus on maps. My solutions for Map-1 are below.
mapBully:
public Map<String, String> mapBully(Map<String, String> map) { if (map.containsKey("a")) { String tmp = map.get("a"); map.put("a", ""); map.put("b", tmp); } return map; }
mapShare:
public Map<String, String> mapShare(Map<String, String> map) { if (map.containsKey("a")) { String tmp = map.get("a"); map.put("b", tmp); } map.remove("c"); return map; }
mapAB:
public Map<String, String> mapAB(Map<String, String> map) { if (map.containsKey("a") && map.containsKey("b")) { String tmp = map.get("a") + map.get("b"); map.put("ab", tmp); } return map; }
topping1:
public Map<String, String> topping1(Map<String, String> map) { if (map.containsKey("ice cream")) { map.put("ice cream", "cherry"); } map.put("bread", "butter"); return map; }
topping2:
public Map<String, String> topping2(Map<String, String> map) { if (map.containsKey("ice cream")) { map.put("yogurt", map.get("ice cream")); } if (map.containsKey("spinach")) { map.put("spinach", "nuts"); } return map; }
topping3:
public Map<String, String> topping3(Map<String, String> map) { if (map.containsKey("potato")) { map.put("fries", map.get("potato")); } if (map.containsKey("salad")) { map.put("spinach", map.get("salad")); } return map; }
mapAB2:
public Map<String, String> mapAB2(Map<String, String> map) { if (map.containsKey("a") && map.containsKey("b")) { if (map.get("a").equals(map.get("b"))) { map.remove("a"); map.remove("b"); } } return map; }
mapAB3:
public Map<String, String> mapAB3(Map<String, String> map) { if (map.containsKey("a") && !map.containsKey("b")) { map.put("b", map.get("a")); } else if (!map.containsKey("a") && map.containsKey("b")) { map.put("a", map.get("b")); } return map; }
mapAB4:
public Map<String, String> mapAB4(Map<String, String> map) { if (map.containsKey("a") && map.containsKey("b")) { String a = map.get("a"); String b = map.get("b"); if (a.length() > b.length()) map.put("c", a); else if (b.length() > a.length()) map.put("c", b); else { map.put("a", ""); map.put("b", ""); } } return map; }
For further help with Coding Bat (Java), please check out my books. I am also available for tutoring.
What happened to mapAB2, mapAB3, and mapAB4?
Those are recent additions to the CodingBat exercise set. I have not had a look at these problems yet.
I have just added suggested solutions to those problems.
My solution for mapAB2:
public Map mapAB2(Map map) {
if (map.get(“a”) != null && map.get(“b”) != null && map.get(“b”).equals(map.get(“a”))) {
map.remove(“a”);
map.remove(“b”);
}
return map;
}
I prefer using the map method ‘containsKey’ instead of manually checking against ‘null’.
IMO slightly cleaner mapAB3 solution
public Map mapAB3(Map map) {
if (map.containsKey(“a”) ^ map.containsKey(“b”)) {
if (map.containsKey(“a”)) {map.put(“b”, map.get(“a”));}
if (map.containsKey(“b”)) {map.put(“a”, map.get(“b”));}
}
return map;
}
This is not cleaner at all. Instead, it’s a confusing solution. First, the XOR operator is obscure. Second, after the first if-statement, you need additional code to determine which case of the XOR was triggered, which is pretty ugly. Third, line 4 can be shortened to “map.put(“a”, map.get(“b”));” because at that point it is known that “b” is the map.
I’m very new with Java
Here is my solution on AB2,
public Map mapAB2(Map map) {
if ((map.containsKey(“a”) && map.containsKey(“b”)) && (map.get(“a”).equals(map.get(“b”)))) {
map.remove(“a”);
map.remove(“b”);
}
return map;
}
What is your opinion?