skiwi
skiwi

Reputation: 69269

Trouble with converting fixed-amount of for loops to parametrized amount

I am trying to make a generator for Spirals being parametrized by the amount of dimensions the answer should have.

Example on 2 dimensions (x, y)

static void caller()
{
  for (int t = 0; t < 10; t++)
  for (int x = 0; x <= t; x++)
  {
     int y = (t-x);
     printAllPossibleSigns(0, x, y);
  }
}

Example on 3 dimensions (x, y, z)

static void caller()
{
  for (int t = 0; t < 10; t++)
  for (int x = 0; x <= t; x++)
  for (int y = 0; y <= (t-x); y++)
  {
     int z = (t-x-y);
     printAllPossibleSigns(0, x, y, z);
  }
}

Example on 4 dimensions (x, y, z, alpha)

static void caller()
{
  for (int t = 0; t < 10; t++)
  for (int x = 0; x <= t; x++)
  for (int y = 0; y <= (t-x); y++)
  for (int z = 0; z <= (t-x-y); z++)
  {
     int alpha = (t-x-y-z);
     printAllPossibleSigns(0, x, y, z, alpha);
  }
}

However now I am trying to generate only 1 result (or batch of results) at once:

So how exactly would I need to do it now if I want to use it for an iterator, so using the next() it should retrieve 'the result' of one printAllPossibleSigns(0, ...); call.

It would be enough already if there would be some method replacing the bunch of for-loops in which I give as input the t and an array holding the x-value in case of x, y; holding the x, y-value in case of x, y, z; the x, y, z-value in case of x, y, z, alpha, etc.

I hope my question is clear enough.

Upvotes: 0

Views: 50

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533530

Ok, instead of stalling there is a solution which will work for ints, a general solution is much harder, note: This will "spiral" out in boxes.

public static void main(String... ignored) {
    caller(10, 7, new Callback<int[]>() {
        @Override
        public void on(int[] ints) {
            System.out.println(Arrays.toString(ints));
        }
    });
}

interface Callback<T> {
    public void on(T t);
}

public static void caller(int maxSum, int dimensions, Callback<int[]> callback) {
    int[] ints = new int[dimensions];
    for (int t = 0; t < maxSum; t++) {
        caller(t, 0, ints, callback);
    }
}

private static void caller(int sum, int idx, int[] ints, Callback<int[]> callback) {
    if (idx == ints.length) {
        callback.on(ints);
        return;
    }
    for (int i = 0; i < sum; i++) {
        ints[idx] = i;
        caller(sum - i, idx+1, ints, callback);
    }
}

prints

[0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 1, 0]
[0, 0, 0, 0, 1, 0, 0]
[0, 0, 0, 1, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0]
[0, 1, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 2]
[0, 0, 0, 0, 0, 1, 1]
[0, 0, 0, 0, 0, 2, 0]
[0, 0, 0, 0, 1, 0, 1]
[0, 0, 0, 0, 1, 1, 0]
[0, 0, 0, 0, 2, 0, 0]
[0, 0, 0, 1, 0, 0, 1]
[0, 0, 0, 1, 0, 1, 0]
[0, 0, 0, 1, 1, 0, 0]
[0, 0, 0, 2, 0, 0, 0]

...

[7, 0, 1, 0, 0, 0, 1]
[7, 0, 1, 0, 0, 1, 0]
[7, 0, 1, 0, 1, 0, 0]
[7, 0, 1, 1, 0, 0, 0]
[7, 0, 2, 0, 0, 0, 0]
[7, 1, 0, 0, 0, 0, 1]
[7, 1, 0, 0, 0, 1, 0]
[7, 1, 0, 0, 1, 0, 0]
[7, 1, 0, 1, 0, 0, 0]
[7, 1, 1, 0, 0, 0, 0]
[7, 2, 0, 0, 0, 0, 0]
[8, 0, 0, 0, 0, 0, 1]
[8, 0, 0, 0, 0, 1, 0]
[8, 0, 0, 0, 1, 0, 0]
[8, 0, 0, 1, 0, 0, 0]
[8, 0, 1, 0, 0, 0, 0]
[8, 1, 0, 0, 0, 0, 0]
[9, 0, 0, 0, 0, 0, 0]

Upvotes: 2

Related Questions