-3

If a user inputs some numbers like this (number of lines are arbitrary):

1
2
3
4
5

I need to create a method that makes a series of outputs like this:

First output:

1
2
3
4
5

Second output:

2
1
3
4
5

Third output:

4
3
2
1
5

Fourth output:

5
4
3
2
1

I'm just confused with the pattern that is being utilized here.

MontyLemons
  • 69
  • 1
  • 5
  • You missed one between the second and the third. – RaminS Feb 17 '19 at 22:01
  • I copied this example from the instructions I was given. That is actually the only place I'm confused with. – MontyLemons Feb 17 '19 at 22:08
  • We are also confused. What is your question exactly? And how is it on-topic for a site about programming (as opposed to puzzling) ? – Erwin Bolwidt Feb 17 '19 at 22:12
  • With the given input, I need to "flip" the input by following some sort of pattern. I'm just trying to figure out how to proceed. If you want to explain with code, by all means. – MontyLemons Feb 17 '19 at 22:15
  • Is the pattern fixed or do you want a random pattern? For random, have a look here: https://stackoverflow.com/questions/4228975/how-to-randomize-two-arraylists-in-the-same-fashion (do it like the code in the question) – Friwi Feb 17 '19 at 22:28
  • It has to be fixed, unfortunately. – MontyLemons Feb 17 '19 at 22:40

2 Answers2

0

I think you must flip from the middle on each iteration i the sub sequence from 0 to i*2 -1. Pseudo code assuming that seq is the input sequence:

void pattern(seq){
  print(seq)//First output
  for(int i=1;i*2<=seq.length-1;i++){
    seq = concatenate(seq.subSquence(i,i*2-1), seq.subSquence(0,i-1), seq.subSquence(i*2,seq.length-1) );
    print(seq)
  }
  seq = concatenate(seq.subSquence(i,seq.length-1), seq.subSquence(0,i-1))
  print(seq)//Last output
}
0

What you have to do is to take always next_element from original structure (array, list) and place it top.(others elements goes down by 1 till next element initial position)
next_element is start from second_position and it's increment by 1 till the end of structure (length or size)

import java.util.Arrays;
import java.util.stream.Collectors;

public class Test {

public static void main(String[] args) 
{
    int a1[] = {1,2,3,4,5};
    new Test().compute(a1);

    int a2[] = {5,4,3,2,1};
    new Test().compute(a2);


}

public void compute(int[] a)
{
    for(int i=1;i<a.length;i++)
    {
        //i : next_element which will be place always on first_position
        int temp= a[i];

        //j : current_element (iterate by till next_element)
        for(int j=i; j>0; j--)
        {
                a[j] = a[j-1];
        }
        a[0] = temp;
        String str = Arrays.stream(a).boxed().map(t->String.valueOf(t)).collect(Collectors.joining(","));
        System.out.println("step="+i+": array="+str);
    }
}

}

Output

step=1: array=2,1,3,4,5
step=2: array=3,2,1,4,5
step=3: array=4,3,2,1,5
step=4: array=5,4,3,2,1

and (reverse)

step=1: array=4,5,3,2,1
step=2: array=3,4,5,2,1
step=3: array=2,3,4,5,1
step=4: array=1,2,3,4,5
Traian GEICU
  • 1,750
  • 3
  • 14
  • 26