# Thread: Attempting to understand code. Homework question, mystery code.

1. Member Join Date
Feb 2012
Posts
6
Rep Power
0

## Attempting to understand code. Homework question, mystery code.

Hello everyone! So my teacher assigned us a block of Mystery code that we are suppose to try and figure out how it works. I have been looking at it for some time now here is the code:

Java Code:
```package assignment0;
import java.util.*;

public class mystery {

public static class Mystery{

private List<Integer> makeData(int n){

List<Integer> values = new ArrayList<>();
for(int i = 0; i < n; i++){

}
Collections.shuffle(values);
return values;
}

private int[] guessResult(List<Integer> values){

Random rand = new Random();
int len = values.size();
int[] guess = {

values.get(rand.nextInt(len)),
values.get(rand.nextInt(len)),
};
return guess;
};

private int[] mystery(List<Integer> values, int [] guess){

if(values.isEmpty()){

return guess;
}

int v = values.get(0);
int[] newGuess = {

(guess < v) ? guess : v,
(guess > v) ? guess : v,
};
return this.mystery(values.subList(1, values.size()), newGuess);
}

public int[] operation(int n){

List<Integer> data = this.makeData(n);
int[] guess = this.guessResult(data);
int[] result = this.mystery(data, guess);
return result;
}
}

public static void main(String[] args) {

Mystery mystery = new Mystery();
int n = 99;
int[] result = mystery.operation(n);
for(int i = 0; i<2; i++){

System.out.println(result[i]);
}
}

}```
So I punched it into my NetBeans and tested it. It returns two values 0 and 98. So what I have been able to ascertain so far is that the code will always return 0 and a number wish is 1 less than n, that much is obvious. However, it does this by printing the contents of the array result. Those values are found by some sort of comparison between the array guess and the array data in the mystery method. I know that data is an array of random integers of size n and that guess is an array of size 2 who's contents are random integers. I am not sure how the mystery class is comparing the contents of the arrays in order figure out what to print.

Is this a recursive method which uses a Boolean comparison to do some type of binary search on the array.. as you can see I am a bit lost.

Side question is there a way I can use the debugger to figure this out? Like it would be nice if I could see the contents of data and guess without printing them out. Is there a way to step through the program as it runs whilst displaying that content?  Reply With Quote

2. ## Re: Attempting to understand code. Homework question, mystery code.

One approach to understanding code is to add comments to it describing what each section does. Multiple passes over the code adding more comments as understanding improves should eventually help you understand what the code is doing.  Reply With Quote

3. Member Join Date
Jan 2017
Posts
2
Rep Power
0

## Re: Attempting to understand code. Homework question, mystery code.

This is recursive function. So you must analyse code rather than debug 99 functions call.
probably you can change n value to reduce number of steps.

Function is simple.

1. Base case empty list. function return 2 items guess array.

2. function call itself without  item. (this means iteration trough array)

*return this.mystery(values.subList(1, values.size()), newGuess);

3. each time first item is compared with guess and select max and min values and store values
in guess array
Java Code:
```    int[] newGuess = {

(guess < v) ? guess : v,
(guess > v) ? guess : v,
};```
Last edited by homeworkhelp4u; 01-23-2017 at 06:27 AM. Reason: added code tag  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Attempting to understand code. Homework question, mystery code.

The OP was supposed to figure this out for himself. I guarantee you that Norm could have provided the same answer. Please provide ways to help posters find answers without providing the details. As you are new to the forum you may also want to read the forum guidelines.

Regards,
Jim  Reply With Quote

5. Member Join Date
Jan 2017
Posts
2
Rep Power
0

## Re: Attempting to understand code. Homework question, mystery code. Originally Posted by jim829 The OP was supposed to figure this out for himself.
Jim
I apologize. It's my fault. It was not a complete solution.  Reply With Quote

6. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Attempting to understand code. Homework question, mystery code.

No apology necessary. Just wanted to let you know. And sometimes syntax and examples are provided. Usually it is a judgement call. And welcome to the forum!

Regards,
Jim  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•