Skip to content

Instantly share code, notes, and snippets.

@cjmcgraw
Last active August 29, 2015 14:02
Show Gist options
  • Save cjmcgraw/e5e20cf11675067c3b46 to your computer and use it in GitHub Desktop.
Save cjmcgraw/e5e20cf11675067c3b46 to your computer and use it in GitHub Desktop.
import java.awt.Point;
public class LoopFill {
public static void main(String[] args) {
LoopFill loopFiller = new LoopFill();
int[] result;
try {
result = parseArgs(args);
} catch (Exception e) {
throw new IllegalArgumentException("There was an error with your arguments. Ensure they are only int values! No comma's separating!'");
}
for(int i = 0; i < result.length; i+= 2) {
Point[] data = loopFiller.fillArray(result[i], result[i + 1]);
printArray(data);
System.out.println();
}
}
public static int[] parseArgs(String[] args) {
int[] result = new int[args.length - args.length % 2];
for(int i = 0; i < result.length; i+= 2) {
result[i] = Integer.parseInt(args[i]);
result[i + 1] = Integer.parseInt(args[i + 1]);
}
return result;
}
public static void printArray(Point[] array) {
System.out.print("[");
if (array.length >= 1) {
System.out.print(array[0]);
for(int i = 1; i < array.length; i++) {
System.out.print(", ");
System.out.print(array[i]);
}
}
System.out.print("]");
System.out.println();
}
public Point[] fillArray(int startIndex, int endIndex) {
int diffRange = getDiffRange(startIndex, endIndex);
Point[] result = new Point[diffRange + 1];
int c1 = getNormalizedCoefficient(endIndex - startIndex);
int c2 = getNormalizedCoefficient(startIndex - endIndex);
for (int i = 0; i <= diffRange; i++) {
result[i] = this.new MyPoint(startIndex + c1 * i, endIndex + c2 * i);
}
return result;
}
public int getNormalizedCoefficient(int value) {
return (int) value / Math.abs(value);
}
public int getDiffRange(int firstValue, int secondValue) {
return Math.abs(firstValue - secondValue);
}
private class MyPoint extends Point {
public MyPoint(int x, int y) {
super(x, y);
}
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
}
}
@cjmcgraw
Copy link
Author

cjmcgraw commented Jun 1, 2014

This is a small amount of code to display the results of solving a particular problem.

The problem specifically is:

Given the input of two numbers x and y: Form an array of points such that x -> y, and y -> x where each value is incremented/decremented for each index.

The challenge here was to begin with a simple list:

1, 10

x: 1 -> 10
y: 10 -> 1

Thus the output of our array would be:

[(1, 10), (2, 9), (3, 8), (4, 7), (5, 6), (6, 5), (7, 4), (8, 3), (9, 2), (10, 1)]

Initially this was easily solved with the simple loop. However when the initial values were swapped the requirement was that the list perform exactly, only now decrementing towards the sought after value:

10, 1

x: 10 -> 1
y: 1 -> 10

Thus the output of our array would be:

[(10, 1), (9, 2), (8, 3), (7, 4), (6, 5), (5, 6), (4, 7), (3, 8), (2, 9), (1, 10)]

This became more complicated. A simple solution with a for loop that incremented one value and decremented the other would not suffice.

Instead I noticed that the values themselves (given a swap of numbers) were swapped.

This meant I could easily solve by trading the values of x and y before performing my operation.

However this would require an additional if statement which I would rather not use.

Instead I realized that simply taking the coefficients, normalizing them (to 1) and then using their respective differences would cause the swap to happen this is show below:

1, 10

x: 1 -> 10 (which is a shift of 10 -1, or + 9)
y: 10 -> 1 (which is a shift of 1 - 10, or -9)

10, 1

x: 10 -> 1 (-9)
y: 1 -> 10 (+9)

This initially explains the trend I noticed before.

Now how can I use this to my advantage?

Well if its a shift of z we know that we can increment by 1/z each time we need to increment it, it will be updated.

But how many times do we need to increment it?

Well z times.

So (+/-)(1/z) * z = (+/-)1

Let c = (+/-)

So the question is how to we determine the coefficient?

Well simply enough, just like we did before by subtracting one number from the other.

Thus we reach the solution of normalizing the coefficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment