CodingBat: Java. Map-1


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.


7 thoughts on “CodingBat: Java. Map-1

  1. Emi

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

    Reply
  2. Dominik

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

    Reply
    1. Gregor Ulm Post author

      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.

      Reply

Leave a Reply to Dominik Cancel reply

Your email address will not be published. Required fields are marked *

Spammer prevention; the answer is an integer: * Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.