Results 1 to 15 of 15

Thread: Railroad array

  1. #1
    roooseon is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Railroad array

    Define an array to be a railroad-tie array if the following three conditions hold
    a. The array contains at least one non-zero element
    b. Every non-zero element has exactly one non-zero neighbor
    c. Every zero element has two non-zero neighbors.
    For example, {1, 2, 0, 3, -18, 0, 2, 2} is a railroad-tie array because
    a[0] = 1 has exactly one non-zero neighbor (a[1])
    a[1] = 2 has exactly one non-zero neighbor (a[0])
    a[2] = 0 has two non-zero neighbors (a[1] and a[3])
    a[3] = 3 has exactly one non-zero neighbor (a[4])
    a[4] = -18 has exactly one non-zero neighbor (a[3])
    a[5] = 0 has two non-zero neighbors (a[4] and a[6])
    a[6] = 2 has exactly one non-zero neighbor (a[7])
    a[7] = 2 has exactly one non-zero neighbor (a[6])

    The following are not railroad-tie arrays
    {1, 2, 3, 0, 2, 2}, because a[1]=2 has two non-zero neighbors.
    {0, 1, 2, 0, 3, 4}, because a[0]=0 has only one non-zero neighbor (it has no left neighbor)
    {1, 2, 0, 0, 3, 4}, because a[2]=0 has only one non-zero neighbor (a[1])
    {1}, because a[0]=1 does not have any non-zero neighbors.
    {}, because the array must have at least one non-zero element
    {0}, because the array must have at lease one non-zero element.

    Write a function named isRailroadTie which returns 1 if its array argument is a railroad-tie array; otherwise it returns 0.

    Correct output
    1
    0

    Output I'm getting
    0
    0


    Java Code:
    public class isRailroadTie {
    public static void main(String[] args) {
    System.out.println(isRailroadTie(new int[]{1, 2, 0, 3, -18, 0, 2, 2}));
    System.out.println(isRailroadTie(new int[]{1, 2, 0, 3, -18, 0, 2, 0}));
    }
    
    public static int isRailroadTie(int[] a) {
    int count = 0;
    if (a.length > 3) {
    
    for (int i = 0; i < a.length - 1; i++) {
    if (i < 1 && a[i] == 0) {
    return 0;
    } else if (i < 1 && a[i] != 0 && a[i + 1] != 0) {
    count++;
    } else if (i >= 1) {
    if (a[i] == 0 && ((a[i - 1] != 0) && a[i + 1] != 0)) {
    count++;
    } else if (a[i] != 0 && (a[i + 1] == 0 && a[i] != 0 && a[i - 1] == 0) || (a[i - 1] != 0 && a[i + 1] == 0)) {
    count++;
    }
    }
    if (a[i + 1] == a.length && a[i + 1] == 0) {
    return 0;
    } else if (a[i + 1] == a.length && (a[i + 1] != 0 && a[i] != 0)) {
    count++;
    }
    }
    }
    if (count == a.length)
    return 1;
    return 0;
    }
    }
    Last edited by roooseon; 07-25-2016 at 12:00 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Railroad array

    What have you tried?

    How is this a java programming question?
    Last edited by Norm; 07-24-2016 at 09:27 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Railroad array

    This sounds a lot like homework.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    roooseon is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Re: Railroad array

    Quote Originally Posted by jim829 View Post
    This sounds a lot like homework.

    Regards,
    Jim
    Hi Jim,
    It is not a homework. I'm trying to prepare for an entrance exam for a university.
    Last edited by roooseon; 07-24-2016 at 10:28 PM.

  5. #5
    roooseon is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Re: Railroad array

    Hi Norm,
    I'm new to programming. I wanted to solve this problem on Java. Below is my code that I have tried but I'm not getting a correct result.
    Java Code:
    public class isRailroadTie {
        public static void main(String[] args) {
            System.out.println(isRailroadTie(new int[]{1, 2, 0, 3, -18, 0, 2, 2}));
            System.out.println(isRailroadTie(new int[]{1, 2, 0, 3, -18, 0, 2, 0}));
        }
    
        public static int isRailroadTie(int[] a) {
            int count = 0;
            if (a.length > 3) {
    
                for (int i = 0; i < a.length - 1; i++) {
                    if (i < 1 && a[i] == 0) {
                        return 0;
                    } else if (i < 1 && a[i] != 0 && a[i + 1] != 0) {
                        count++;
                    } else if (i >= 1) {
                        if (a[i] == 0 && ((a[i - 1] != 0) && a[i + 1] != 0)) {
                            count++;
                        } else if (a[i] != 0 && (a[i + 1] == 0 && a[i] != 0 && a[i - 1] == 0) || (a[i - 1] != 0 && a[i + 1] == 0)) {
                            count++;
                        }
                    }
                    if (a[i + 1] == a.length && a[i + 1] == 0) {
                        return 0;
                    } else if (a[i + 1] == a.length && (a[i + 1] != 0 && a[i] != 0)) {
                        count++;
                    }
                }
            }
            if (count == a.length)
                return 1;
            return 0;
        }
    }
    Last edited by Norm; 07-25-2016 at 12:14 AM. Reason: Added code tags to improve readability

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Railroad array

    I'm not getting a correct result.
    Can you post the current output and add some comments showing what the output should be?

    One suggestion: Wrap the expressions in ()s to show the evaluation order and to make it easier to read and understand.

    Also Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    Last edited by Norm; 07-24-2016 at 11:44 PM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Railroad array

    I am confused by the assignment. Are you supposed to create an arbitrary length array that meets the requirements, evaluate a given array to see
    if it meets the requirements, or both.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    roooseon is offline Member
    Join Date
    Jul 2016
    Posts
    4
    Rep Power
    0

    Default Re: Railroad array

    We are supposed to evaluate a given array to see if it meets the requirements and return 1 if it meets them otherwise return 0.
    Thank you for helping.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Railroad array

    Another problem I see is there is no description in the code for what it is trying to solve. There needs to be a definition of what the code is trying to do as comments so that anyone reading the code will know.

    Also the ()s need work to make it easier to understand the logic in the long boolean expressions. For example:
    Java Code:
       if ((a[i] != 0 && (a[i + 1] == 0) && (a[i] != 0) && (a[i - 1] == 0)))
          || ( (a[i - 1] != 0 && (a[i + 1] == 0)) )  ) {
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Railroad array

    Yeah! If I had read your first post more carefully I would have realized that.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Railroad array

    Instead of coding, think a bit: those three rules a), b) and c) imply that the array should look like this: -,-,0,-,-,0,-,-,0,-,- ... 0,-- where '-' represents any non-zero number; those rules also imply that the array cannot have arbitrary lengh, but has to have length 3n+2 where n is the number of zeros in the array. Testing whether or not an array obeys to the rules is a breeze:

    Java Code:
    boolean isRailroadArray(int[] a) {
       if (a.length%3 != 2) return false;
       for (int i= 0; i < a.length; i++)
          if (i%3 == 2) {
             if (a[i] != 0) return false;
          }
          else
             if (a[i] == 0) return false;
       return true;
    }
    Only start coding after thinking the problem over and over again; until it's no fun anymore ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Railroad array

    Yep. There are actually, a number of way to do this as long as one recognizes the pattern. For instance, build up a binary
    value based on the array (1's for non-zero, and zero). The resulting string in octal will be all threes for a valid rr array. The only
    special test in that case is to ensure that the first element is not 0.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

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

    Default Re: Railroad array

    Quote Originally Posted by jim829 View Post
    Yep. There are actually, a number of way to do this as long as one recognizes the pattern. For instance, build up a binary
    value based on the array (1's for non-zero, and zero). The resulting string in octal will be all threes for a valid rr array. The only
    special test in that case is to ensure that the first element is not 0.
    Nor should the last element be zero (for similar reasons).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Railroad array

    Yes. Except that if the end of the array is 0,1,1,0,1,1,0 or 1,1,0,1,1,0, the resulting octal strings will be 66 rather than 33.
    I started to do it like you did using mod 3 but got distracted with pattern matching. I like your way better because it is
    fast fail.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Railroad array

    I was curious if this was a standard problem so I did a search. The OP also posted it at Railroad array.

    @OP. If you are going to cross-post to N sites, let each of the other N-1 sites know about the other posts. This prevents
    folks wasting time providing duplicate answers.

    Regards,
    Jim
    Last edited by jim829; 07-25-2016 at 04:23 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 3
    Last Post: 12-28-2013, 09:39 PM
  2. Replies: 15
    Last Post: 05-14-2012, 12:42 PM
  3. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  4. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  5. Replies: 2
    Last Post: 09-06-2010, 01:03 AM

Tags for this Thread

Posting Permissions

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