Incrementing an array of unique numbers in raising order
So i have this array which only contains unique numbers and where the number at index 0 is the lowest and the one at the end of the array is the highest.
E.g. [1,2,3,4]
Now i increment each time the number at the back with 1. But when any of the numer reaches a certain height, it should increment the number on the left.
E.g. Let's say the max height would be 8.
[1,2,3,8] -> [1,2,4,5]
Now until here my code works. But when the 2 last numbers have reached maximum height it won't increment the third last anymore.
E.G. [1,2,7,8] -> [1,3,4,5]
The code i wrote is recursive.
//Position is the index in the array of which element should be incremented by 1
public int[] increaseArray(int[] index, int maxIndex, int position) {
int tmp = index[position];
if (tmp < maxIndex) {
index[position] = tmp + 1;
return index;
} else {
if (positie != 0 && index[position - 1] + 2 <= maxIndex) {
index[position] = index[position - 1] + 2;
return increaseArray(index, maxIndex, position - 1);
} else {
return null;
}
}
}
EDIT 1:
The resulting array only contains unique numbers, so yes int[2] is maxed out to 7 here.
Also i edited the code. I'm feeling i'm nearly there although the last number is still bugged...
public int[] increaseIndex(int[] index, int maxIndex, int position) {
int tmp = index[position];
if (tmp < maxIndex + position - 2) {
index[position] = tmp + 1;
return index;
} else {
if (position > 0) {
//The following line of code is the problem...
index[position] = index[pos开发者_运维技巧ition - 1] + 2;
return increaseIndex(index, maxIndex, position - 1);
} else {
return null;
}
}
}
EDIT 2:
Really close right now. I fixed the maxIndex like said. Now there is some small bug when more than 2 numbers should be incremented.
The code
public int[] increaseIndex(int[] index, int maxIndex, int position) {
int size = index.length;
int tmp = index[position];
if (tmp < maxIndex - (size-position-1)) {
index[position] = tmp + 1;
return index;
} else {
if (position > 0) {
//The following line is the problem i think...
index[position] = index[position - 1] + 2;
return increaseIndex(index, maxIndex, position - 1);
} else {
return null;
}
}
}
This would give me the following output for example with maxIndex 8 when i use following executing code
int[] index = new int[] {1,2,3,4};
index = increaseIndex(index, row.length - 1, k - 2);
while (index != null) {
printArray(index);
index = increaseIndex(index, row.length - 1, k - 2);
}
[1, 2, 3, 4]
[1, 2, 3, 5]
[1, 2, 3, 6]
[1, 2, 3, 7]
[1, 2, 3, 8]
[1, 2, 4, 5]
[1, 2, 4, 6]
[1, 2, 4, 7]
[1, 2, 4, 8]
[1, 2, 5, 6]
[1, 2, 5, 7]
[1, 2, 5, 8]
[1, 2, 6, 7]
[1, 2, 6, 8]
[1, 2, 7, 8]
[1, 3, 4, 9] //wrong
[1, 3, 5, 6]
[1, 3, 5, 7]
[1, 3, 5, 8]
[1, 3, 6, 7]
[1, 3, 6, 8]
[1, 3, 7, 8]
[1, 4, 5, 9] //wrong
[1, 4, 6, 7]
[1, 4, 6, 8]
[1, 4, 7, 8]
[1, 5, 6, 9] //wrong
[1, 5, 7, 8]
[1, 6, 7, 9] //wrong
[2, 3, 8, 9] //wrong
[2, 4, 5, 10]//wrong
[2, 4, 6, 7]
[2, 4, 6, 8]
[2, 4, 7, 8]
[2, 5, 6, 9] //wrong
[2, 5, 7, 8]
[2, 6, 7, 9] //wrong
[3, 4, 8, 9] //wrong
[3, 5, 6, 10]//wrong
[3, 5, 7, 8]
[3, 6, 7, 9] //wrong
[4, 5, 8, 9] //wrong
[4, 6, 7, 10]//wrong
[5, 6, 8, 9] //wrong
Here is a different approach without recursion. It tries all combinations and filters out those which are not unique.
This approach can be easily adapted to a range of requirements.
public static void main(String... args) {
uniqueCombinations(4, 8);
}
private static void uniqueCombinations(int depth, int maxValue) {
int[] ints = new int[depth];
long combinations = (long) Math.pow(maxValue, depth);
LOOP:
for (long l = 0; l < combinations; l++) {
long l2 = l;
// create a combination.
for (int i = ints.length - 1; i >= 0; i--) {
ints[i] = (int) (l2 % maxValue + 1);
l2 /= maxValue;
}
// check the combination.
for (int i = 0; i < ints.length; i++)
for (int j = i + 1; j < ints.length; j++)
if (ints[i] == ints[j]) continue LOOP;
// print a result.
System.out.println(Arrays.toString(ints));
}
}
prints
[1, 2, 3, 4]
[1, 2, 3, 5]
[1, 2, 3, 6]
[1, 2, 3, 7]
[1, 2, 3, 8]
[1, 2, 4, 3]
.....
[8, 7, 5, 6]
[8, 7, 6, 1]
[8, 7, 6, 2]
[8, 7, 6, 3]
[8, 7, 6, 4]
[8, 7, 6, 5]
Here it is in python:
def choose_iter(elements, length):
for i in xrange(len(elements)):
if length == 1:
yield (elements[i],)
else:
for next in choose_iter(elements[i+1:len(elements)], length-1):
yield (elements[i],) + next
output:
>>> for res in choose_iter([1, 2, 3, 4, 5, 6, 7, 8], 4):
print res
(1, 2, 3, 4)
(1, 2, 3, 5)
(1, 2, 3, 6)
(1, 2, 3, 7)
(1, 2, 3, 8)
(1, 2, 4, 5)
(1, 2, 4, 6)
(1, 2, 4, 7)
(1, 2, 4, 8)
(1, 2, 5, 6)
(1, 2, 5, 7)
(1, 2, 5, 8)
(1, 2, 6, 7)
(1, 2, 6, 8)
(1, 2, 7, 8)
(1, 3, 4, 5)
(1, 3, 4, 6)
(1, 3, 4, 7)
(1, 3, 4, 8)
(1, 3, 5, 6)
(1, 3, 5, 7)
(1, 3, 5, 8)
(1, 3, 6, 7)
(1, 3, 6, 8)
(1, 3, 7, 8)
(1, 4, 5, 6)
(1, 4, 5, 7)
(1, 4, 5, 8)
(1, 4, 6, 7)
(1, 4, 6, 8)
(1, 4, 7, 8)
(1, 5, 6, 7)
(1, 5, 6, 8)
(1, 5, 7, 8)
(1, 6, 7, 8)
(2, 3, 4, 5)
(2, 3, 4, 6)
(2, 3, 4, 7)
(2, 3, 4, 8)
(2, 3, 5, 6)
(2, 3, 5, 7)
(2, 3, 5, 8)
(2, 3, 6, 7)
(2, 3, 6, 8)
(2, 3, 7, 8)
(2, 4, 5, 6)
(2, 4, 5, 7)
(2, 4, 5, 8)
(2, 4, 6, 7)
(2, 4, 6, 8)
(2, 4, 7, 8)
(2, 5, 6, 7)
(2, 5, 6, 8)
(2, 5, 7, 8)
(2, 6, 7, 8)
(3, 4, 5, 6)
(3, 4, 5, 7)
(3, 4, 5, 8)
(3, 4, 6, 7)
(3, 4, 6, 8)
(3, 4, 7, 8)
(3, 5, 6, 7)
(3, 5, 6, 8)
(3, 5, 7, 8)
(3, 6, 7, 8)
(4, 5, 6, 7)
(4, 5, 6, 8)
(4, 5, 7, 8)
(4, 6, 7, 8)
(5, 6, 7, 8)
I give it here to suggest a different approach - instead of thinking about incrementing numbers properly, think of it as picking elements in order. Implementation will be really different though as it's a completely different approach.
I don't see any reason to use (or not to use) recursion here, I wouldn't if I were you.
The problem is that you didn't define what should happen when one of the numerals is already at maxIndex.
I would've suggested a solution but you didn't define how your program should behave at this case.
What it does at this point is to increment it anyway.
You might want to skip all positions which are already at maxIndex (or above) with something along the lines of:
while (position >= 0 && index[position] >= maxIndex) position--;
if (position == -1) return; //in case all positions are already at maximum value
精彩评论