4
\$\begingroup\$

From a programming assignment:

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:

Call                        Output

writeSequence(1);             1
writeSequence(2);            1 1
writeSequence(3);           2 1 2
writeSequence(4);          2 1 1 2
writeSequence(5);         3 2 1 2 3
writeSequence(6);        3 2 1 1 2 3
writeSequence(7);       4 3 2 1 2 3 4
writeSequence(8);      4 3 2 1 1 2 3 4
writeSequence(9);     5 4 3 2 1 2 3 4 5
writeSequence(10);   5 4 3 2 1 1 2 3 4 5

Notice that for odd numbers the sequence has a single 1 in the middle while for even values it has two 1s in the middle.

Your method should throw an IllegalArgumentException if passed a value less than 1. A client using this method would have to call println to complete the line of output.

I wrote code that is pretty ugly, but produces the correct output. I've put this up to see if anyone had a more efficient recursive algorithm and to put the example up. There are not many hits about more complex recursive methods in Java when searching.

public static void writeSequence(int n) {

    if( n < 1 )
        throw new IllegalArgumentException();

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

    } else if( n == 2 ) {
        System.out.print("1 1");

    } 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 + 1) / 2) + " ");
        writeSequence((n - 2));
        System.out.print(" " + ((n + 1) / 2));
    }       
}
\$\endgroup\$
3
\$\begingroup\$

Assuming you can use a helper function, you could do this:

  public static void writeSequence(int n) {
    if (n < 1) throw new IllegalArgumentException();
    writeSequenceHelper(n);
  }

  public static void writeSequenceHelper(int n) {
    if (n <= 0) return;
    int m = (n+1) / 2;
    System.out.print(m + " ");
    writeSequenceHelper(n - 2);
    System.out.print(m + " ");
  }

If you can't use a helper function like this to workaround the strange IllegalArgumentException requirement, you could do this (which is similar to your solution, but a little more succinct):

  public static void writeSequence2(int n) {
    if (n < 1) throw new IllegalArgumentException();
    if (n == 1) {
      System.out.print("1");
    } else if (n == 2) {
      System.out.print("1 1");
    } else {
      int m = (n+1) / 2;
      System.out.print(m + " ");
      writeSequence2(n - 2);
      System.out.print(" " + m);
    }
  }
\$\endgroup\$
  • \$\begingroup\$ public static void writeSequence2(int n) { if (n < 1) throw new IllegalArgumentException(); if (n == 1) { System.out.print("1"); } else if (n == 2) { System.out.print("1 1"); } else { System.out.print((n + 1) / 2 + " "); writeSequence2(n - 2); System.out.print(" " + (n + 1) / 2); } } \$\endgroup\$ – user2364427 May 10 '13 at 0:21
  • \$\begingroup\$ Not sure what your point is... \$\endgroup\$ – Mark May 10 '13 at 14:11
  • \$\begingroup\$ For some reason I cant figure out the elusive method of commenting in code formatting. Basically, I removed the local variable m and put the (m + 1) / 2 operation into the recursive call. After looking at your code, I realized that my n % 2 == 0 condition is unnecessary. Thanks for your help and perspective! \$\endgroup\$ – user2364427 May 10 '13 at 17:55
  • \$\begingroup\$ Don't think you can code format comments. IMHO, the problem should only require an IllegalArgumentException when n < 0. In-lining my local variable, m, is less efficient since it requires duplicate computation, but that's probably not significant if you like the way it looks better. Please consider accepting my answer (by clicking the check mark). \$\endgroup\$ – Mark May 11 '13 at 5:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.