ArrayTester free response answer 6

ArrayTester free response problem from the 2018 AP Computer Science A Exam.

ArrayTester is #4 from the from the 2018 AP Computer Science A Free Response problems.

https://secure-media.collegeboard.org/ap/pdf/ap18-frq-computer-science-a.pdf

Part (a) – getColumn method

public static int[] getColumn(int[][] arr2D, int c)
{
  int[] column = new int[arr2D.length];

  for(int i = 0; i < column.length; i++)
    column[i] = arr2D[i][c];

  return column;
}

Part (b) – isLatin method

public static boolean isLatin(int[][] square)
{
  if(containsDuplicates(square[0]))
    return false;

  for(int r = 1; r < square.length; r++)
    if( ! hasAllValues(square[0], square[r]) )
      return false;

  for(int c = 0; c < square[0].length; c++)
    if( ! hasAllValues(square[0], getColumn(square, c)))
      return false;

  return true;
}

hasAllValues method

This method was NOT required to be implemented on the Exam. It is provided in case anyone would like to test their code with it.

Note: The inner loop could be made more efficient by stopping at the first occurrence. I was targeting ease of implementation.

public static boolean hasAllValues(int[] arr1, int[] arr2)
{
  for(int arr1Index = 0; arr1Index < arr1.length; arr1Index++)
  {
    boolean contains = false;
    for(int arr2Index = 0; arr2Index < arr2.length; arr2Index++)
    {
      if(arr1[arr1Index] == arr2[arr2Index])
        contains = true;
    }

    if( ! contains )
      return false;
  }

  return true;
}

containsDuplicates method

This method was NOT required to be implemented on the Exam. It is provided in case anyone would like to test their code with it.

public static boolean containsDuplicates(int[] arr)
{
  for(int i = 0; i < arr.length; i++)
    for(int j = i + 1; j < arr.length; j++)
      if(arr[i] == arr[j])
        return true;

  return false;
}

6 thoughts on “ArrayTester free response answer

  1. Reply Josh Pudaloff May 17,2018 8:25 pm

    Nitpicking here but the parameter name for isLatin is “square” and not “squares

  2. Reply Mark Moylan May 18,2018 12:18 pm

    alt solution to isLatin only one loop is required

    public static boolean isLatin(int [][] square){
      for(int i = 0; i < square.length; i++){
        int[] col = getColumn();  
        if(containsDuplicates(square[0]) 
            || !haveAllValues(square[0], square[i]) 
    	|| !haveAllValues(square[0], col)){
          return false;
        }
      }
      
    return true;
    }
    
    • Reply Brandon Horn May 19,2018 12:02 pm

      The logic of a single loop is valid, although there is no reason to run containsDuplicates more than once.

      This solution calls getColumn incorrectly and uses the wrong name for hasAllValues.

  3. Reply Bryce May 18,2018 3:24 pm

    For the hasAllValues method, couldn’t you just have one for loop because square.length == square[0].length? (it’s a square)

    Then for the for loop you could have something like this?

    for(int i = 0; i < square.length; i++) {
      if(!hasAllValues(square[0], square[r]) || !hasAllValues(square[0], getColumn(square, i)))
        return false;
    }
    
    • Reply Brandon Horn May 19,2018 12:03 pm

      Yes. You could definitely write this with a single loop.

      The solution should use i instead of r.

Leave a Reply