Results 1 to 20 of 26
Thread: complete sequence
- 12-28-2010, 11:40 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
complete sequence
Hello again,
I got a bit of a problem, i am supposed to write a code that check the difference between the numbers of the sequence, and check if they are between 1 and their number - 1
eg. if the sequence is of 8 elements, to check if the difference occupies all the numbers between 1 and 7 (8-1)
ive wrote the following code, and gave it a not complete sequence, but it is giving me a complete sequence, can someone check it for and and tell me where is my problem
Java Code:public class Problem1 { public static void main (String[] args) { int [] array = {1, 3, 4, 8, 11, 17, 22, 30}; int n = array.length; int end = n - 1; int [] diff = new int [n-1]; Boolean status = false; for ( int i = 0; i < n - 1; i++) { diff [i] = Math.abs(array[i] - array[i + 1]); } for ( int i = 0; i < n - 1; i++) { if ( diff [i] >= 1 && diff [i] <= end) { status = true; }else{ status = false; break; } } if ( status = true ) { System.out.println("Complete Sequence"); }else{ System.out.println("Not Complete Sequence"); } } }
- 12-28-2010, 11:54 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-28-2010, 11:57 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
Great :D
Thnx very much, I didnt notice this
- 12-28-2010, 01:35 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
Ok
after doing some more coding, I've got to a complete code for the whole problem which is the following
Java Code:import java.util.*; import java.io.*; public class test { public static void main (String[] args) throws FileNotFoundException { Scanner input = new Scanner (new File(args[0])); int n; // for number of elements int len; // for the length of the array int end; // for the checking of difference between 1 and end Boolean status = false; // true if complete, false if not complete while (input.hasNextLine()) // to check for each line { n = input.nextInt(); // number of element in each line end = n - 1; // used for the checking int [] array = new int [n]; // to put the numbers in an array for checking the sequence int [] diff = new int [n-1]; // to write down the difference between the elements of the array for ( int i = 0; i < n; i++) // to fill up the array with the inegers found in each line { array [i] = input.nextInt(); } for ( int j = 0; j < n - 1; j++) // to find out the difference between the elements and put them in new array { diff [j] = Math.abs(array[j] - array[j + 1]); } for ( int k = 0; k < n - 1; k++) // to check is the the elements in the array diff if they are between 1 and end { if ( diff [k] >= 1 && diff [k] <= end ) { status = true; }else{ status = false; break; } } if (status == true ) // to print the result { System.out.println("Complete Sequence"); }else{ System.out.println("Not Complete Sequence"); } } } }
input:
4 1 4 2 3
5 1 4 2 -1 6
1 10
10 10 9 8 7 6 5 4 3 2 1
9 1 3 4 8 11 17 22 30 37
8 1 3 4 8 11 17 22 30
output:
Complete
Not Complete
Complete
Not Complete
Complete
Not Complete
Complete Sequence
Not Complete Sequence
Not Complete Sequence
Complete Sequence
Complete Sequence
Not Complete SequenceLast edited by aizen92; 12-28-2010 at 01:38 PM.
- 12-28-2010, 03:52 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
A few things that could make your code easier to follow:
Use Java naming conventions and call your class Test.
Don't declare variables you don't use like len. Give other variables names that are descriptive enough that you can mostly dispense with the comments.
Java Code:if(someCondition) { status = true; } else { status = false; break; }
is better written as
Java Code:status = someCondition;
(and the "break" is quite redundant)
Finally,
Java Code:if()status == true) { doSomething(); } else { doSomethingElse(); }
is better written as
Java Code:if(status) { doSomething(); } else { doSomethingElse(); }
--------------------
As to the logic of what you have written, the final for loop is quite strange: you keep evaluating the value of status and doing nothing with that value. Only the last time around the loop will status aquire a value that is used for anything.
Try and express an algorithm (a recipe, a precise set of instructions) that will give status the value you want it to have before you begin to write code.
- 12-28-2010, 04:01 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
I don't understand the problem; according to your original post the difference between two consecutive numbers should be less than the total number of numbers, right? If so, why is the sequence starting with "10 10 ..." not complete? The length of the sequence is 11 so the maximum difference should be 10 or less ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 12-28-2010, 04:24 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
I don't understand the problem
You're not allowed a difference of 10-10=0.
- 12-28-2010, 04:27 PM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
- 12-28-2010, 04:33 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-28-2010, 04:36 PM #10
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
- 12-28-2010, 04:43 PM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
OK. If the first number is the sequence length and not part of the sequence, then we're back to Jos' question since the 10 element sequnce has a difference of 1 and so should be complete.
- 12-28-2010, 04:46 PM #12
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
but the thing is to be complete it should occupy all the numbers between the 1 and the length - 1, in the 10 element sequence the difference between them all is 1
EDIT: Ok i added an if statement that says
Java Code:if ( array.length == 1 ) status = true;
Now all thats left is the 10 element sequenceLast edited by aizen92; 12-28-2010 at 04:49 PM.
- 12-28-2010, 04:50 PM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Are you sure you aren't supposed to get each of the numbers 1->(len-1) exactly once as a difference?
[Edit] Ok, so it appears you are.
Adding a special case for 1 element sequences is not going to help. You need to figure out an algorithm that will work in general. How would you go about dooing this with pen and paper if you were given the numbers one at a time (and told how many there would be)? Start from there before you begin to write code or it will all be a bit random.Last edited by pbrockway2; 12-28-2010 at 04:53 PM.
- 12-28-2010, 04:52 PM #14
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-28-2010, 04:58 PM #15
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
LOL, sorry for the much trouble
lets say for example, the sequence "1 4 2 3",
the diff between 1 and 4 is 3
the diff between 4 and 2 is 2
the diff between 2 and 3 is 1
then length of the sequence is 4, and the differences occupy the numbers from 1 to the length of the sequence - 1, which is 3, which is true (note: not necessarily consecutive numbers in the difference, but it happened to be the case in this sequence)
another example, is the sequence of "1 4 2 -1 6"
the diff between 1 and 4 is 3
the diff between 4 and 2 is 2
the diff between 2 and -1 is 3
the diff between -1 and 6 is 7 (we use absolute value when negative number)
the length of the sequence is 5, and the difference do not occupy all the numbers from 1 to length of the sequence - 1, which is 4, and we have two numbers repeated so its not a complete sequence
I hope this is a good explanation of the question
- 12-28-2010, 05:01 PM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
My guess: a sequence is complete iff the differences form a permutation of the first n-1 natural numbers. (no special case required)
Last edited by pbrockway2; 12-28-2010 at 05:04 PM.
- 12-28-2010, 05:03 PM #17
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
- 12-28-2010, 05:19 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 12-28-2010, 05:21 PM #19
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 11
what do you mean by a complete definition, im lost here lol
- 12-28-2010, 05:26 PM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03-25-2010, 07:59 AM -
Linked list sequence and array sequence
By Predz in forum New To JavaReplies: 1Last Post: 12-31-2009, 02:30 AM -
calling sequence
By rocky in forum Web FrameworksReplies: 0Last Post: 04-27-2009, 09:35 PM -
Fetching sequence
By Shivraj in forum New To JavaReplies: 1Last Post: 03-19-2009, 03:30 PM -
Escape Sequence
By Punter in forum New To JavaReplies: 4Last Post: 02-10-2009, 08:04 AM
Bookmarks