Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default 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");
            }
        }
    }
    Thanks in advance

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    Java Code:
            if ( status = true )
    This assigns the value true to variable status and the if-clause also evaluates to true; you probably meant '==' instead of '='.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Great :D
    Thnx very much, I didnt notice this

  4. #4
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    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");
                }
            }
        }
    }
    My input and output should be the following

    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
    but im getting is

    Complete Sequence
    Not Complete Sequence
    Not Complete Sequence
    Complete Sequence
    Complete Sequence
    Not Complete Sequence
    what is the problem with my code?
    Last edited by aizen92; 12-28-2010 at 01:38 PM.

  5. #5
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    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.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    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
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    I don't understand the problem

    You're not allowed a difference of 10-10=0.

  8. #8
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    yeh about that sorry, but i forgot to mention that when the sequence consist of 1 integer its automatically considered a complete sequence

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by pbrockway2 View Post
    You're not allowed a difference of 10-10=0.
    Ah, yes, got it, but why is the previous series "1 10" a 'complete' series? the max difference has to be 2-1 == 1 ...

    kind regards,

    Jos (<--- extremely stupid)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Quote Originally Posted by JosAH View Post
    Ah, yes, got it, but why is the previous series "1 10" a 'complete' series? the max difference has to be 2-1 == 1 ...

    kind regards,

    Jos (<--- extremely stupid)
    I said, this is a special case, when a sequence is of only 1 integer its directly considered a complete sequence, so "1 10" 1 is for the length of the sequence and 10 is the element in this sequence, and since its 1 element only, its complete

  11. #11
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    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. #12
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    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;
    and it worked for the single sequence

    Now all thats left is the 10 element sequence
    Last edited by aizen92; 12-28-2010 at 04:49 PM.

  13. #13
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    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.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    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
    For the more stupid people among us (i.e. me) could you please give a complete definition of a (in)complete sequence before we discuss matters further?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    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

  16. #16
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,568
    Rep Power
    12

    Default

    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.

  17. #17
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    Quote Originally Posted by pbrockway2 View Post
    My guess: a sequence if complete iff the differences form a permutation of the first n-1 natural numbers. (no special case required)
    well yeh in a sense it is

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    well yeh in a sense it is
    I don't trust that "in a sense" part. Can't you give a complete definition?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    aizen92 is offline Senior Member
    Join Date
    Nov 2010
    Posts
    155
    Rep Power
    5

    Default

    what do you mean by a complete definition, im lost here lol

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by aizen92 View Post
    what do you mean by a complete definition, im lost here lol
    Pbrockway2 "guessed" a definition and you replied "in a sense". I hate it when there is no definition and the developer has to guess what to do. You said you want to decide when a series of numbers is (in)complete. You must have a definition for it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 1 of 2 12 LastLast

Similar Threads

  1. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 07:59 AM
  2. Linked list sequence and array sequence
    By Predz in forum New To Java
    Replies: 1
    Last Post: 12-31-2009, 02:30 AM
  3. calling sequence
    By rocky in forum Web Frameworks
    Replies: 0
    Last Post: 04-27-2009, 09:35 PM
  4. Fetching sequence
    By Shivraj in forum New To Java
    Replies: 1
    Last Post: 03-19-2009, 03:30 PM
  5. Escape Sequence
    By Punter in forum New To Java
    Replies: 4
    Last Post: 02-10-2009, 08:04 AM

Posting Permissions

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