Blog

  • [Java] Nested For Loops, Exponential Palindromes

    [Java] Nested For Loops, Exponential Palindromes

    This post contains spoilers. If you are trying to solve this problem on your own, tread carefully

    Goal:
    Print the following in StdOut
    
    
                 1
               1 2 1
             1 2 4 2 1
           1 2 4 8 4 2 1
    ...


    Earlier this week while working with a student, I came across this problem above that I was actually quite stumped on. I couldn’t quite figure out how to solve it elegantly.

    The major problem is how indices work with Java

    For Loops

    The first thing needed for this problem is an understanding of how for loops work in java:

    for ( _____ ; ______ ; _____) {
          // Do stuff
    }

    There’s a standard structure for this, namely:

    for ( Start ; End ; Step ) {
        // Do cool stuff
    }

    Start is what we start with, and oftentimes it’s a variable,

    for (int i = 0; End ; Step ) {
       // Do cool stuff
    }

    this is setting up a new variable for us, “i”, with the value of 0. Note that this is actually optional, you don’t technically need anything there, but it’s helpful for us to keep track as engineers of what the initial value for the loop is going to be.

    Next, Step. What do we want to do, for each loop of our loop?

    for (int i = 0; End ; i++) {
        // Do cool stuff
    }

    i++?

    This is a shorthand way of writing “i = i + 1”, and will be very common as long as you use and write java.

    This means, for each turn of the loop, take whatever value we have at i and add +1 to it.

    So, first loop, we start at 0. Next, 0+1, so 1. Next, 1 + 1, so 2. Next, 2 + 1, so 3. And on, and on.

    So when does this loop finally stop? End!

    for (int i = 0; i < 3; i++) {
        // Do cool stuff
    }

    This says, starting at i = 0, add 1 to i as long as i < 3.

    So, we start with 0. is 0 > 3? yes.

    Execute the code inside, and then re-loop, adding 1 to i (0 in this case).

    Now, i = 1. is 1 < 3? yes.

    Execute the code inside, and then re-loop, adding 1 to i (1 in this case).

    Now, i = 2, 2< 3? yep! Loop again. Add one.

    Now, i = 3. Is 3 < 3? No!

    We are done with the loop.

    Nested For Loops

    A nested for loop, required for this problem, is a for loop inside another for loop.

    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            //Do cool stuff
        }
    }

    For each loop of i, we will execute the for loop of j. Cool, but how do we use this to get closer to our final answer?

    👷Article in progress, more coming soon. But, the main idea is: we need to track and print things between both for loops.

    In the meantime:


    Guided Practice Problem


    Here’s the code scaffold, with blanks (very easily a potential exam question — start with hard, and then go easier if needed):

    Hard
            for(int i = 0; i < 8; i++) { 
                int center = ________;
                String output = ________;
                for (int j = 0; j < 8; j++) { 
                    if (________) { 
                        int exponent = ________;
                        int value =________; 
                        output = ________; 
                    } else {
                        output = ________;
                    }
                }
                System.out.println(________);
            }
    Normal
    for(int i = 0; i < 8; i++) { 
                int center = ________;
                String output = ________;
                for (int j = 0; j < 8; j++) { 
                    if (________) { 
                        int exponent = ________;
                        int value =________; 
                        output = value + " " + output + " " + value; 
                    } else {
                        output = "  " + output + "  ";
                    }
                }
                System.out.println(output);
            }
    Easy
    for(int i = 0; i < 8; i++) { 
                int center = ________;
                String output = "" + center;
                for (int j = 0; j < 8; j++) { 
                    if (________) { 
                        int exponent = ________;
                        int value = (int) Math.pow(2, exponent); 
                        output = value + " " + output + " " + value; 
                    } else {
                        output = "  " + output + "  ";
                    }
                }
                System.out.println(output);
            }

    Solution
    for(int i = 0; i < 8; i++) { 
                int center = (int)Math.pow(2, i); 
                String output = "" + center;
                for (int j = 0; j < 8; j++) { 
                    if (j < i) { 
                        int exponent = i - j - 1; 
                        int value = (int) Math.pow(2, exponent); 
                        output = value + " " + output + " " + value;
                    } else {
                        output = "  " + output + "  ";
                    }
                }
                System.out.println(output);
            }