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))
return false;

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

for(int c = 0; c < square.length; c++)
if( ! hasAllValues(square, 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;
}
```

## 5 thoughts on “ArrayTester free response answer”

1. Josh Pudaloff

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

• Brandon Horn

You speak truth.

2. Mark Moylan

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)
|| !haveAllValues(square, square[i])
|| !haveAllValues(square, col)){
return false;
}
}

return true;
}
```
• Brandon Horn

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. Bryce

For the hasAllValues method, couldn’t you just have one for loop because square.length == square.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, square[r]) || !hasAllValues(square, getColumn(square, i)))
return false;
}
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.7414936,"math_prob":0.97524166,"size":2235,"snap":"2019-51-2020-05","text_gpt3_token_len":578,"char_repetition_ratio":0.12550426,"word_repetition_ratio":0.15697674,"special_character_ratio":0.27561522,"punctuation_ratio":0.15258215,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99018276,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-29T10:47:18Z\",\"WARC-Record-ID\":\"<urn:uuid:d4c55732-5471-4bdf-ae2e-53b89b3784e1>\",\"Content-Length\":\"24430\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a44c3c2e-298c-436a-8d0a-d7b24ad47c6d>\",\"WARC-Concurrent-To\":\"<urn:uuid:5d18614f-96bc-46b6-b0ac-8d5bc112130e>\",\"WARC-IP-Address\":\"199.192.23.6\",\"WARC-Target-URI\":\"https://apcomputersciencetutoring.com/exam-review/arraytester-free-response-answer/\",\"WARC-Payload-Digest\":\"sha1:VZUDDSJLJM3ZS7HG7L23KPEFNLHMQ6QR\",\"WARC-Block-Digest\":\"sha1:SJ6HDVZUIJDLZKISX2UOZKU3JZ63NADU\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579251796127.92_warc_CC-MAIN-20200129102701-20200129132701-00194.warc.gz\"}"}