1. Member
Join Date
Jul 2016
Posts
4
Rep Power
0

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.

What have you tried?

How is this a java programming question?
Last edited by Norm; 07-24-2016 at 09:27 PM.

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

This sounds a lot like homework.

Regards,
Jim

4. Member
Join Date
Jul 2016
Posts
4
Rep Power
0

Originally Posted by jim829
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. Member
Join Date
Jul 2016
Posts
4
Rep Power
0

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

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.

[code]
[/code]

to get highlighting and preserve formatting.
Last edited by Norm; 07-24-2016 at 11:44 PM.

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

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

8. Member
Join Date
Jul 2016
Posts
4
Rep Power
0

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.

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)) )  ) {```

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

Regards,
Jim

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

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

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

Originally Posted by jim829
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

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

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

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

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.