3

I have a number 0127

i am trying logic to generate number following sequence in JAVA

0
1
2
7
01
12
27
012
127
0127
1270
2701
7012
01270
12701
27012
.
.

I am breaking my head :-)

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
Soft
  • 1,796
  • 5
  • 19
  • 30

5 Answers5

1

Divide it up into two steps: the substrings and superstrings.

Sometimes the best way to solve a big problem is to divide the work up into smaller problems that are easier to solve.

For the substrings, use nested for loops.

  1. Loop substring length from 1 to string.length() - 1
  2. Loop starting character from 0 to string.length() - 1 - substringLength

In the inner loop, generate the substring of the given length from the given starting character. This will generate all the substrings.

For the superstrings, you only need one loop to pick the starting character. For each item in the loop, start at that character and build your string until the given length, wrapping from the last character to the first.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
1

I would convert the number to a String object, then to an array of chars.

From there, you can iterate over the array with the following logic:

        int MAX_LENGTH = 10;
        char[] array = "0127".ToCharArray();
        for (int i = 0; i < MAX_LENGTH; i++)
        {
            for (int offset = 0; offset < array.Length; offset++)
            {
                String disp = "";
                for (int j = 0; j <= i; j++)
                {
                    int index = j + offset;
                    while (index >= array.Length)
                        index -= array.Length;
                    disp += array[index];
                }
                Console.WriteLine(disp);
            }
        }

Change MAX_LENGTH to be what ever the maximum length of the output string should be.

Here is the output that this code produces: enter image description here

joe_coolish
  • 7,201
  • 13
  • 64
  • 111
  • 1
    Errr, this is not Java and it does not produce the same sequence (but I still think, that Soft's question misses some numbers - so the algorithm, translated to Java, may be the one that solves the problem ;) ) – Andreas Dolk Apr 20 '11 at 14:30
  • I did minor change to this and i got the result i expect – Soft Apr 20 '11 at 14:33
  • I forgot to change the `if` to a `while` on the interior. I updated it and posted a picture of the results. @Andreas, I believe that it does produce the same sequence :) – joe_coolish Apr 20 '11 at 14:35
  • 2
    As I said, I was sure, it's the solution, but 70, 270 and 701 are not in the question's sequence. @Soft - please double-check your question!! – Andreas Dolk Apr 20 '11 at 14:51
1

The first few lines starting from 0 to 0127 (inclusively) are all subsequences of {0,1,2,7} (empty set is missing).

For the rest - it's like a ring, you pick a starting number and "go" n steps in one direction:

               0
              / \
             7   1
              \ /
               2

this would produce:

n=1: 0,    1,    2,    7
n=2: 01,   12,   27,   70
n=3: 012,  127,  270,  701
n=4: 0127, 1270, 2701, 7012

But I can't see a link between the two parts - are you sure, the sequence in your question is complete, no numbers missing? especially 70, 270 and 701?

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
1
int MAX_LENGTH = 5;
        String[] numStr = {"0","1","2","7"};
        for (int i = 0; i < MAX_LENGTH; i++)
        {
            for (int offset = 0; offset < numStr.length; offset++)
            {
                if(i>0 && offset+1 == numStr.length) continue;
                String disp = "";
                for (int j = 0; j <= i; j++)
                {
                    int index = j + offset;
                    if (index >= numStr.length)
                        index -= numStr.length;
                    disp += numStr[index];
                }
                System.out.println(disp);

            }
        }
Soft
  • 1,796
  • 5
  • 19
  • 30
0

This is my solution. It was executed with the tested number and it generates the same sequence:

public static void printSequence(String number) {
    final char[] charNumber = number.toCharArray();
    final int sizeNumber = number.length();

    int MAX_ITER = 5;
    for (int i = 0; i < MAX_ITER; i++) {
        for (int offset = 0; offset < sizeNumber; offset++) {
            String subSequence = "";
            for (int j = 0; j <= i; j++) {
                int index = (j + offset) % sizeNumber;
                subSequence += charNumber[index];
            }
            System.out.println(subSequence);
        }
    }
}
Mr. Nobody
  • 386
  • 1
  • 14