0

My assignment is to create a function called "roll" that implements the roll(n,k) operation on the specified stack. My function should check that n and k are both nonnegative and that n is not larger than the stack size; if either of these conditions are violated, the function should throw a runtime exception with the message "roll: argument out of range" Here is what I have so far:

import java.util.Stack;
public class PostScript{
    public static void main(String [] args){
      int n = Integer.parseInt(args[0]);
      int k = Integer.parseInt(args[1]);
      Stack<String> s = new Stack<String>();
      s.push("A"); s.push("B"); s.push("C"); s.push("D");
      System.out.println("Initial : " + s);
      roll(s,n,k);
      System.out.println("after roll(" + n + ", " + k + "): " + s);
    }
    public static void roll(Stack<String> stack, int n, int k){
    
    }
}

Here are some example test runs:

$ java PostScript 4 1\
Initial : [A, B, C, D]\
after roll(4, 1): [D, A, B, C]

$ java PostScript 3 2\
Initial : [A, B, C, D]\
after roll(3, 2): [A, C, D, B]

$ java PostScript 2 4\
Initial : [A, B, C, D]\
after roll(2, 4): [A, B, C, D]

The effect of applying roll(n,k) is to rotate the top n elements of a stack by k positions, where the general direction of the rotation is toward the top of the stack. More specifically, roll(n,k) has the effect of removing the top n elements, cycling the top element to the last position k times, and then replacing the reordered elements on the stack. For example,

roll(4,1) roll(3,2) roll(2,4)

| D | | C | | D | | B | | D | | D |
| C | | B | | C | | D | | C | | C |
| B | -> | A | | B | -> | C | | B | -> | B |
| A | | D | | A | | A | | A | | A |

John
  • 27
  • 5
  • You have basically posted you assignment template and ask us to solve it for you. What part are you having problems with? – Tarmo Mar 08 '21 at 18:59
  • Well the issue for me is that I have no clue on how to solve this which is why im asking for pointers. – John Mar 08 '21 at 19:01
  • Divide it to a smaller problems and solve them one by one. Validation for example is one of them. Implement this first. And btw, you are not saying what the roll function actually has to do. You are only saying what are the input validation rules. – Tarmo Mar 08 '21 at 19:03
  • Take a look at my answer here: https://stackoverflow.com/questions/15997536/how-do-you-keep-the-roll-operator-straight There are lots of ways to conceptualize the `roll` operator. Some may be easier or more efficient to implement. For example, it could be done by breaking into 3 subarrays and then appending in a different order. Depending on the use case, this could be much more efficient than a for loop. Also, note that PostScript allows `k` to be negative. – luser droog Mar 09 '21 at 18:20

1 Answers1

0

Looks like I got it. Here's my code in case anyone ever wants this in the future. import java.util.Stack; import java.util.ArrayList;

public class PostScript {

public static void main(String [] args){
    int n = Integer.parseInt(args[0]);
    int k = Integer.parseInt(args[1]);
    Stack<String> s = new Stack<String>();
    s.push("A"); s.push("B"); s.push("C"); s.push("D");
    System.out.println("Initial : " + s);
    roll(s,n,k);
    System.out.println("after roll(" + n + ", " + k + "): " + s);
}

public static void roll(Stack<String> stack, int n, int k){

    if(n < 0 || k < 0 || n > stack.size()) throw new IllegalArgumentException("argument of of range");


    ArrayList<String> arr = new ArrayList<String>(n);
    for(int i = 0; i < n ; i++) arr.add(stack.pop());


    int track = 0;
    for(int i = 1; i < k; i++){
        track++;
        if(track == n) track = 0;
    }


    for(int i = 0; i < n; i++){
        stack.push(arr.get(track--));

        if(track < 0) track = n - 1;
    }

}

}
John
  • 27
  • 5