Golf this AP CS Java FRQ - Duplicate Sums of Arrays in 2D Array

5

In preparation for the AP Computer Science AP Test (Java), our teacher has been giving us FRQs from previous years for practice.

My friend and I were golfing this during class today and got it as short as we could, but I'd like to see if there are any shorter solutions any of you can come up with.

Rules:

  • You must use the predefined method headers, method names, and parameter names.
  • You do not have to specify imports.
  • All code is to be written in Java.
  • No external libraries

Information:

Part A

Given an array of type int, write a method that returns the sum of the elements of the array.

public int arraySum(int[] arr)
{
    /*Implementation to be written in part A*/
}

Now, write the implementation for the method arraySum.

Part B

Given a 2D array of type int, write a method that returns a 1-dimensional int array containing the sums of each row of the given 2-dimensional array. You must use arraySum but may assume it works properly regardless of what you put for part A.

public int[] array2dSum(int[] arr2d)
{
    /*Implementation to be written in part B*/
}

Now, write the impmenetation for the method array2dSum.

Part C

Given a 2D array of type int, write a method that returns whether or not the sums of the rows of the array are diverse, meaning the array contains no duplicate sums. You must use array2dSum but may assume it works properly regardless of what you put for part A.

public boolean isDiverse(int[] arr2d)
{
    /*Implementation to be written in part C*/
}

Now, Write the implementation for the method isDiverse.


Here's what I have:

A - 31 chars

return IntStream.of(arr).sum();

B - 59 chars

return Stream.of(arr2d).mapToInt(x->arraySum(x)).toArray();

C - 86 chars

int[]h=array2dSum(arr2d);return IntStream.of(h).distinct().toArray().length==h.length;

Test Inputs:

arraySum(new int[] {1, 2, 3}) returns 6

array2dSum(new int[][] {{1,2},{3,4}}) returns {3,7}

isDiverse(new int[][] {{1,2,3},{4,5,6},{7,8,9}}) returns true

isDiverse(new int[][] {{1,2,3},{4,5,6},{1,2,3},{7,8,9}}) returns false

Targz

Posted 2017-04-25T19:29:53.673

Reputation: 77

Question was closed 2017-04-26T00:10:27.960

4Welcome to the site but in my humble opinion this is too specific. Asking for answers in one language particularly Java which is not particularly golfable (again in my humble opinion) will not get a great response. Maybe try to make the question a bit more open? – ElPedro – 2017-04-25T19:35:36.230

1Shouldn't your test case for array2dSum() return {3,7} ? – Geobits – 2017-04-25T19:38:22.700

3@ElPedro Questions like this are on-topic. The goal is essentially tips, not a challenge. – mbomb007 – 2017-04-25T19:42:43.600

@mbomb007 - thanks for the clarification but that is not how I understood it. "Tips for golfing in XXXX language" I understand are on topic but not specific questions that specify a language. Apologies if I have misunderstood. – ElPedro – 2017-04-25T19:44:52.953

He already listed his shortest solutions, so it's essentially a tips question. Asking for tips for a specific golf is acceptable, though chat may be a better place to ask. He does not yet have enough rep to post a chat message, though. – mbomb007 – 2017-04-25T19:46:39.360

Point taken. Not my kind of question and it wasn't me that downvoted so I guess I'll leave it at that. Thanks again for the clarification. – ElPedro – 2017-04-25T19:48:32.470

This question is also a golf-course. Which are disallowed in most cases. This particular question seems like a golf course with little interaction and should probably be closed.

– Post Rock Garf Hunter – 2017-04-25T20:28:26.920

5

I'm voting to close this question as off-topic because this question is a golf-course with little interaction

– Post Rock Garf Hunter – 2017-04-25T20:30:46.830

@WheatWizard - I wasn't aware of golf-course but it pretty well sums up what I was thinking. – ElPedro – 2017-04-25T20:41:13.490

@WheatWizard I understand about golf courses but I didn't want to post a separate question for each part since I thought that would be spam and obnoxious. I also included my shortest solutions so people wouldn't think I was simply asking for someone to do a homework problem or something. – Targz – 2017-04-25T22:12:51.523

@WheatWizard also if it wasn't clear I'm not doing it like a golf course where you have to do all 3 parts for an answer, I just wanted to see if anybody could get this any shorter in any of the parts at all – Targz – 2017-04-25T22:38:51.487

@Targz - myself I wasn't meaning to be in any way insulting. It's just that questions here usually leave things open to Python/JavaScript/AnyOtherLanguages programmers to also submit an answer unless they are questions asking for tips in a specific language but not usually with a specific question/challenge. Perhaps if you posted this in the sandbox we could help improve the question. Maybe then you would get your Java answer and maybe some interesting answers in Ruby or Perl or other languages.

– ElPedro – 2017-04-25T22:45:15.427

"Given a 2D array of type int" I would expect to see int[][] in the signature. – Peter Taylor – 2017-04-25T22:58:58.563

@PeterTaylor I just copied the wording of the original problem – Targz – 2017-04-25T23:14:47.370

No answers