Category Archives: CodingBat: Java

CodingBat: Java. Map-2

Here are my solutions to the Map-2 section on CodingBat.

word0:

public Map<String, Integer> word0(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
  
  for (int i = 0; i < strings.length; i++) {
    map.put(strings[i], 0);
  }
  
  return map;
}

wordLen:

public Map<String, Integer> wordLen(String[] strings) {
    
  Map<String, Integer> map = new HashMap<String, Integer>();
  
  for (int i = 0; i < strings.length; i++) {
    String tmp = strings[i];
    map.put(tmp, tmp.length());
  }
  
  return map;
}

pairs:

public Map<String, String> pairs(String[] strings) {
  
  Map<String, String> map = new HashMap<String, String>();
  
  for (int i = 0; i < strings.length; i++) {
    String tmp   = strings[i];
    String first = String.valueOf(tmp.charAt(0));
    String last  = String.valueOf(tmp.charAt(tmp.length() - 1));
    map.put(first, last);
  }
  
  return map;
}

wordCount:

public Map<String, Integer> wordCount(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
  
  for (int i = 0; i < strings.length; i++) {
    
    String tmp = strings[i];
    
    if (map.containsKey(tmp)) {
      int count = map.get(tmp);
      map.put(tmp, count + 1);
    } else {
      map.put(tmp, 1);
    }
    
  }
  return map;
}

firstChar:

public Map<String, String> firstChar(String[] strings) {
  
  Map<String, String> map = new HashMap<String, String>();
  
  for (int i = 0; i < strings.length; i++) {
    
    String key = String.valueOf(strings[i].charAt(0));
    
    if (map.containsKey(key)) {
      String val = map.get(key) + strings[i];
      map.put(key, val);
    } else {
      map.put(key, strings[i]);
    }
    
  }
  return map;
}

wordAppend:

public String wordAppend(String[] strings) {
 
 Map<String, Integer> map    = new HashMap<String, Integer>();
 String               result = "";
 
 for (int i = 0; i < strings.length; i++) {
   
   String key = strings[i];
   
   if (map.containsKey(key)) {
     int val = map.get(key);
     val++;
     if (val % 2 == 0) {
      result += key;
     }
     map.put(key, val);
   } else {
     map.put(key, 1);
   }
   
 }
 
 return result;
}

wordMultiple:

public Map<String, Boolean> wordMultiple(String[] strings) {
  
  Map<String, Integer> counts = new HashMap<String, Integer>();
  Map<String, Boolean> result = new HashMap<String, Boolean>();
  
  for (int i = 0; i < strings.length; i++) {
    String key = strings[i];
    
    if (counts.containsKey(key)) {
      int val = counts.get(key);
      val++;
      counts.put(key, val);
    } else {
      counts.put(key, 1);
    }
    
    result.put(key, counts.get(key) >= 2);
  }
  
  return result;
}

allSwap:

public String[] allSwap(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
    
  for (int i = 0; i < strings.length; i++) {
    
    String key = String.valueOf(strings[i].charAt(0));
    
    if (map.containsKey(key)) {
      
      // swap
      int    pos   = map.get(key); 
      String tmp   = strings[pos];
      strings[pos] = strings[i];
      strings[i]   = tmp ;
      
      // delete
      map.remove(key);
      
    } else {
      map.put(key, i);
    }
    
  }
  
  return strings;
}

firstSwap:

public String[] firstSwap(String[] strings) {
  
  Map<String, Integer> map = new HashMap<String, Integer>();
    
  for (int i = 0; i < strings.length; i++) {
    
    String key = String.valueOf(strings[i].charAt(0));
    
    if (map.containsKey(key)) {
      
      int val = map.get(key);
      if (val == -1) {
        continue;
      }
      
      // swap
      int    pos   = map.get(key); 
      String tmp   = strings[pos];
      strings[pos] = strings[i];
      strings[i]   = tmp ;
      
      // set a flag
      map.put(key, -1);
      
    } else {
      map.put(key, i);
    }
    
  }
  
  return strings;
}

CodingBat: Java. Map-1

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

CodingBat: Java. Update 2013.2

The Array-2 section on Coding Bat saw an update as well. Those four exercises gradually build up to a variation of the well-known FizzBuzz problem.

All solutions were successfully tested on 29 March 2013.

fizzArray:

public int[] fizzArray(int n) {
	int[] result = new int[n];
	for (int i = 0; i < n; i++)
		result[i] = i;
	return result;
}

fizzArray2:

public String[] fizzArray2(int n) {
	String[] result = new String[n];
	for (int i = 0; i < n; i++)
		result[i] = String.valueOf(i);
	return result;
}

fizzArray3:

public int[] fizzArray3(int start, int end) {
	int n = end - start;
	int[] result = new int[n];

	for (int i = 0; i < n; i++)
		result[i] = start++;
	return result;
}

fizzBuzz:

public String[] fizzBuzz(int start, int end) {
	int n = end - start;
	String[] result = new String[n];

	int pos = 0;
	for (int i = start; i < end; i++) {
		boolean fizz = i % 3 == 0;
		boolean buzz = i % 5 == 0;

		if (fizz && buzz) result[pos] = "FizzBuzz";
		else if (fizz) result[pos] = "Fizz";
		else if (buzz) result[pos] = "Buzz";
		else result[pos] = String.valueOf(i);
		pos++;
	}
	return result;
}

CodingBat: Java. Update 2013.1

As I thought I was done with publishing my solutions to the entire Coding Bat: Java library of exercises, with a total of over 200, Nick Parlante updated his website and added some more exercises. Apparently, his aim was to thoroughly prepare students for “FizzBuzz” interview questions.

The first exercise was added to Warmup-1, the others to Logic-1.

All solutions were successfully tested on 28 March 2013.

or35:

public boolean or35(int n) {
	return n % 3 == 0 || n % 5 == 0;
}

more20:

public boolean more20(int n) {
	return n % 20 == 1 || n % 20 == 2;
}

old35:

public boolean old35(int n) {
	return n % 3 == 0 ^ n % 5 == 0;
}

specialEleven:

public boolean specialEleven(int n) {
	return n % 11 == 0 || n % 11 == 1;
}

less20:

public boolean less20(int n) {
	return (n + 1) % 20 == 0 || (n + 2) % 20 == 0;
}

fizzString:

public String fizzString(String str) {
	boolean fizz = str.charAt(0) == 'f';
	boolean buzz = str.charAt(str.length() - 1) == 'b';

	if (fizz && buzz) return "FizzBuzz";
	if (fizz) return "Fizz";
	if (buzz) return "Buzz";
	return str;
}

fizzString2:

public String fizzString2(int n) {
	boolean fizz = n % 3 == 0;
	boolean buzz = n % 5 == 0;

	if (fizz && buzz) return "FizzBuzz!";
	if (fizz) return "Fizz!";
	if (buzz) return "Buzz!";
	return n + "!";
}

CodingBat: Java. Recursion-2

Finally we’ve reached the last section of Coding Bat: Recursion-2. Those nine exercises are all very similar. Once you’ve figured out one you can probably solve the others immediately.

I’ll just walk you through the general strategy, using the first exercise as an example: The method “groupSum” takes three arguments. The first, “start”, is the index of an array of integers, the second is the array of integers in question, and the third, “target” is the target value. Now, “start” increases by one with each recursive call. If this value is greater than or equal to the length of the array, then the entire array has been processed. At this point, only the evaluation step has to be done, which consists of checking whether the target value is zero.

“Why zero?”, you may ask. If “target” equals zero, then it is possible to pick a group of integers from the array that sum up to the target value. The calculation is done by selecting each value of the array and in one case subtracting it from “target”, while in the other leaving “target” unchanged. Thus, all possible combinations will eventually be checked. Finally, the method returns “true” once one of the results is “true”, which is a property of short-circuit evaluation of logical disjunctions.

All solutions were successfully tested on 28 March 2013.

groupSum:

public boolean groupSum(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	return groupSum(start + 1, nums, target - nums[start])
			|| groupSum(start + 1, nums, target);
}

groupSum6:

public boolean groupSum6(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	if (nums[start] == 6)
		return groupSum6(start + 1, nums, target - nums[start]);
	return groupSum6(start + 1, nums, target - nums[start])
			|| groupSum6(start + 1, nums, target);
}

groupNoAdj:

public boolean groupNoAdj(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	return groupNoAdj(start + 2, nums, target - nums[start])
			|| groupNoAdj(start + 1, nums, target);
}

groupSum5:

public boolean groupSum5(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;
	if (nums[start] % 5 == 0) {
		if (start < nums.length - 1 && nums[start + 1] == 1)
			return groupSum5(start + 2, nums, target - nums[start]);
		return groupSum5(start + 1, nums, target - nums[start]);
	}
	return groupSum5(start + 1, nums, target - nums[start])
			|| groupSum5(start + 1, nums, target);
}

groupSumClump:

public boolean groupSumClump(int start, int[] nums, int target) {
	if (start >= nums.length) return target == 0;

	int sum = nums[start];
	int count = 1;
	for (int i = start + 1; i < nums.length; i++)
		if (nums[i] == nums[start]) {
			sum += nums[i];
			count++;
		}
	return groupSumClump(start + count, nums, target - sum)
			|| groupSumClump(start + count, nums, target);
}

splitArray:

public boolean splitArray(int[] nums) {
	return helper(0, nums, 0, 0);
}

private boolean helper(int start, int[] nums, int sum1, int sum2) {
	if (start >= nums.length) return sum1 == sum2;
	return helper(start + 1, nums, sum1 + nums[start], sum2)
			|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}

splitOdd10:

public boolean splitOdd10(int[] nums) {
	return helper(0, nums, 0, 0);
}

private boolean helper(int start, int[] nums, int sum1, int sum2) {
	if (start >= nums.length)
		return sum1 % 10 == 0 && sum2 % 2 == 1 || sum1 % 2 == 1
				&& sum2 % 10 == 0;
	return helper(start + 1, nums, sum1 + nums[start], sum2)
			|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}

split53:

public boolean split53(int[] nums) {
	return helper(0, nums, 0, 0);
}

private boolean helper(int start, int[] nums, int sum1, int sum2) {
	if (start >= nums.length) return sum1 == sum2;
	if (nums[start] % 5 == 0)
		return helper(start + 1, nums, sum1 + nums[start], sum2);
	if (nums[start] % 3 == 0)
		return helper(start + 1, nums, sum1, sum2 + nums[start]);

	return helper(start + 1, nums, sum1 + nums[start], sum2)
			|| helper(start + 1, nums, sum1, sum2 + nums[start]);
}