patternjavaModerate
Checking three values are consecutive
Viewed 0 times
threearecheckingvaluesconsecutive
Problem
This question was recently asked: Determining if three numbers are consecutive
It intrigued me, even though it was broken. I have the following implementation I thought would be interesting, and I am looking for reviews on readability, usability, and for any edge cases it may miss, etc.
I put this together in a unit test. Here's the full file:
```
import org.junit.Assert;
import org.junit.Test;
public class ThreeInARow {
/**
* Determine whether three int values can be arranged in to an incrementing sequence.
*
* @param a the first value
* @param b the second value
* @param c the third value
* @return true if there is an order of the three inputs which makes them sequential
*/
public static final boolean isSequential(int a, int b, int c) {
final int x = Math.abs(a - b);
final int y = Math.abs(b - c);
final int z = Math.abs(a - c);
return x + y + z == 4 && x y z == 2;
}
private static final int[] FROM = {Integer.MIN_VALUE, Integer.MIN_VALUE + 3, -5, -4, -3, -2, -1,
0, 1, 2, 3, 100, Integer.MAX_VALUE - 2, Integer.MAX_VALUE};
@Test
public void testGoodBlocks() {
for (int f : FROM) {
int a = f;
int b = a + 1;
int c = b + 1;
Assert.assertTrue(isSequential(a, b, c));
Assert.assertTrue(isSequential(b, c, a));
Assert.assertTrue(isSequential(c, a, b));
Assert.assertTrue(isSequential(b, a
It intrigued me, even though it was broken. I have the following implementation I thought would be interesting, and I am looking for reviews on readability, usability, and for any edge cases it may miss, etc.
/**
* Determine whether three int values can be arranged in to an incrementing sequence.
*
* @param a the first value
* @param b the second value
* @param c the third value
* @return true if there is an order of the three inputs which makes them sequential
*/
public static final boolean isSequential(int a, int b, int c) {
final int x = Math.abs(a - b);
final int y = Math.abs(b - c);
final int z = Math.abs(a - c);
return x + y + z == 4 && x * y * z == 2;
}I put this together in a unit test. Here's the full file:
```
import org.junit.Assert;
import org.junit.Test;
public class ThreeInARow {
/**
* Determine whether three int values can be arranged in to an incrementing sequence.
*
* @param a the first value
* @param b the second value
* @param c the third value
* @return true if there is an order of the three inputs which makes them sequential
*/
public static final boolean isSequential(int a, int b, int c) {
final int x = Math.abs(a - b);
final int y = Math.abs(b - c);
final int z = Math.abs(a - c);
return x + y + z == 4 && x y z == 2;
}
private static final int[] FROM = {Integer.MIN_VALUE, Integer.MIN_VALUE + 3, -5, -4, -3, -2, -1,
0, 1, 2, 3, 100, Integer.MAX_VALUE - 2, Integer.MAX_VALUE};
@Test
public void testGoodBlocks() {
for (int f : FROM) {
int a = f;
int b = a + 1;
int c = b + 1;
Assert.assertTrue(isSequential(a, b, c));
Assert.assertTrue(isSequential(b, c, a));
Assert.assertTrue(isSequential(c, a, b));
Assert.assertTrue(isSequential(b, a
Solution
I don't like the idea of having to solve Diophantine equations just to be able to understand the code. I'll suggest the same solution here as I did for @cody.codes:
"The minimum and maximum differ by 2, and all three numbers are unique" is easier to understand.
public static boolean isSequential(int a, int b, int c) {
int min = Math.min(a, Math.min(b, c));
int max = Math.max(a, Math.max(b, c));
return max - min == 2 && a != b && a != c && b != c;
}"The minimum and maximum differ by 2, and all three numbers are unique" is easier to understand.
Code Snippets
public static boolean isSequential(int a, int b, int c) {
int min = Math.min(a, Math.min(b, c));
int max = Math.max(a, Math.max(b, c));
return max - min == 2 && a != b && a != c && b != c;
}Context
StackExchange Code Review Q#94826, answer score: 12
Revisions (0)
No revisions yet.