'Using recursion, in java, to have a variety of outputs from a single int parameter

If anyone is familiar with PracticeIt, it's what I'm doing and I'm on recursion problem sets. I'm having trouble doing them. Here's one problem:

Write a method writeSquares that accepts an integer parameter n and prints the first n squares separated by commas, with the odd squares in descending order followed by the even squares in ascending order. The following table shows several calls to the method and their expected output:

   writeSquares(5); ----> Output: 25, 9, 1, 4, 16 
   writeSquares(1); ----> Output: 1

I've spent a few hours each day for the past 3 days figuring out recursions but I just can't figure it out. Can anyone point me in the right direction?

My code looks like:

public static void writeSquares(int n)
{
    if(n<1)
        throw new IllegalArgumentException();
    else{
        if(n%2==0){
            System.out.print((n-1)*(n-1));
            writeSquares2(n-1, n-1, "down");
        }
        else{
            System.out.print(n*n);
            writeSquares2(n-1, n-1, "down");
        }
    }
}

public static void writeSquares2(int n, int m, String s)
{
    if(m==0){
        return;
    }

    String ss = s;

    if(n<=1){
        ss = "up";}

    if(n%2==1&&s=="down"){
        System.out.print(", " + n*n);
        writeSquares2(n-2,m-1,ss);
    }
    if(n%2==0&&s=="down"){
        writeSquares2(n-1,m-1,ss);
        System.out.print(", " + n*n);
    }
    if(n%2==1&&s=="up"){
        System.out.print(", " + n*n);
        writeSquares2(n+2,m-1,ss);
    }
    if(n%2==0&&s=="up"){
        writeSquares2(n+1,m-1,ss);
        System.out.print(", " + n*n);
    }

EDIT: Woops I fixed the code below

And another question from another problem set is:

Write a method writeSequence that accepts an integer n as a parameter and prints a symmetric sequence of n numbers with descending integers ending in 1 followed by ascending integers beginning with 1, as in the table below:

  writeSequence(9);     -----> 5 4 3 2 1 2 3 4 5
  writeSequence(10);    -----> 5 4 3 2 1 1 2 3 4 5

My code:

public void writeSequence(int n)
{
    if(n<1)
        throw new IllegalArgumentException();
    else
        writeSequence2(n, n, "down"); //I actually dont need the second parameter
}

    public void writeSequence2(int n, int m, String s)
    {
        String ss = s;
        if(n/2-1==1)
            ss = "up";

        if(n==1)
            System.out.print(n);


        else if(ss.equals("down")){
            if(n%2==0){
                System.out.print(n/2+" ");
                writeSequence2(n-1, m-1, ss);
            }
            else if(n%2==1){
                writeSequence2(n-1, m-1, ss);
                System.out.print(" "+ (n/2+1));
            }

        }

        else if(ss.equals("up")){
            if(n%2==0){
                System.out.print(n/2+" ");
                writeSequence2(n-1, m-1, ss);
            }
            else if(n%2==1){
                writeSequence2(n-1, m-1, ss);
                System.out.print(" " + (n/2+1));
            }
        }
    }

For the second one, my code is somewhat correct. Except when n is odd. Also another question - is it possible to do these with just a single method?

Thanks for your time. The tutors in my school aren't very helpful and neither are my classmates.



Solution 1:[1]

public void writeSequence(int n){
    if( n < 1){
        throw new IllegalArgumentException();
    }
    if(n==1){
        System.out.print(n);
    }
    else if(n==2){
        System.out.print(n/2 +" " + n/2);
    }
    else if(n%2 ==0){
        System.out.print(n/2 +" ");
        writeSequence(n-2);
        System.out.print(" " +n/2);
    }
    else if(n%2 ==1){
        System.out.print( (n/2+1) +" ");
        writeSequence(n-2);
        System.out.print( " "+(n/2 +1));
    }
}

Solution 2:[2]

I realize this is an old thread but in case anyone still gets directed to this page, I just had this question as well. this is how I answered it without a helper method. by the way its really close to Eran's 1st answer except I took my writeSequence out of the if statement. this worked.

public void writeSquares(int n) {

    //for exception
    if (n < 1) {
        throw new IllegalArgumentException();

    //for base
    } else if (n == 1) {
        System.out.print("1");
        return;
    }

    //printing evens before base
    // commas trail number now
    if ((n % 2) != 0) {
        System.out.print((n * n)  + ", ");
    }
    //does this until base
    writeSquares(n - 1);

    //then as we start coming back out of the method calls print evens
    // commas before lead numbers now
    if ((n % 2) == 0) {
        System.out.print(", " + (n * n));
    }
}

Solution 3:[3]

Create two arraylists that EvenList and oddList

if squared value matches even condition put it in evenList, else in oddList.

First print oddList in reverse order and print evenList as it is from index 0.

Solution 4:[4]

The idea is that if n is odd, you want to print n*n immediately (which would print the odd squares in descending order), and then make a recursive call with n-1, while if n is even, you first make the recursive call with n-1 and then print n*n (which would print the even squares in ascending order after all the odd squares are printed).

public static void writeSquares(int n){
    if(n<1)
        throw new IllegalArgumentException();
    if (n==1)
        System.out.print(1 + ",");
    else if (n % 2 == 1) {
        System.out.print(n*n + ",");
        writeSquares (n - 1);
    } else {
        writeSquares (n - 1);
        System.out.print(n*n + ",");
    }
}

EDIT : this code would produce an extra , at the end.

25,9,1,4,16,

To get rid of that, you might have to add a boolean parameter to the method, which indicates whether it's the first call to the method or not.

For writeSequence, the idea is to solve the problem for n assuming you already have the solution for n - 1. If you have a method the writes the sequence for n - 1, in order to expand it to n, you have to print n, print the sequence for n - 1, and print n again. In addition, you need a stopping condition, which is n==1, in which case you just print 1.

public static void writeSequence(int n)
{
    if(n<1)
        throw new IllegalArgumentException();
    if (n==1)
        System.out.print(1 + " ");
    else {
        System.out.print(n + " ");
        writeSequence (n-1);
        System.out.print(n + " ");
    }
}

EDIT:

I missed some details in the question about the second recursion. Here's the updated method. I'm not sure if it's possible to have all the logic in a single method. I had to split it to two recursive methods, based on whether n is odd or even.

public static void writeSequence(int n)
{
  if (n%2 == 0)
      writeSequenceEven (n);
  else 
      writeSequenceOdd (n);
}

public static void writeSequenceOdd(int n)
{
    if (n == 1) {
        System.out.print (1 + " ");
    } else if (n>1) {
        System.out.print((1+n/2) + " ");
        writeSequenceOdd (n-2);
        System.out.print((1+n/2) + " ");
    }
}

public static void writeSequenceEven(int n)
{
    if (n>1) {
        System.out.print(n/2 + " ");
        writeSequenceEven (n-2);
        System.out.print(n/2 + " ");
    }
}

Solution 5:[5]

Here is my solution, it's pretty compact and should work.

public static void writeSequence(int n) {
    if (n <= 1){
        System.out.print(1);
    } else if (n % 2 != 0){
        System.out.print(n*n + ", ");
        writeSequence(n-1);
    } else{
        writeSequence(n-1);
        System.out.print(", " + n*n );
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dejan Skledar
Solution 2 ubiksCube
Solution 3 dvrnaidu
Solution 4
Solution 5 thorzos