Section Problems Number 6 - Recursion

Be prepared to discuss these problems in discussion section.

1. What is the output of csc(5)?

public void csc(int n){
    if( n <= 0 )
        System.out.println( "done" );
    else {
        System.out.println( n );
        csc( n - 1);
}

 

2. What is the output of System.out.println( disc(7) );

public int disc(int x){
    int result = 0;
    if( x <= 2)
        result = 3;
    else
        result = x / 3 + disc( x - 2 );
    return result;
}

3. What is the output of lotto( 5 ) ?

public void lotto( int n ){
    if( n == 0 )
        System.out.prinltn( "There");
    else{
        System.out.prinltn( "Here");
        lotto( n - 2 );
}

4. What is the output of gas("Curious George");

public void gas(String s){
    if( s.length() > 0 ){
        System.out.print( "" + s.charAt(0) + s.charAt( s.length() - 1);
        gas( s.substring(1, s.length() );
        System.out.print( "" + s.charAt(0) + s.charAt( s.length() - 1);
    }
}

5. What is the output of System.out.println( make("001230") );

public static String make(String input){
    String rest = "";
    if( input.length() > 0 ){
        rest = make( input.substring( 0, input.length() - 1 ) );
        char c = input.charAt( input.length() - 1 );
        if( c == '0' )
            rest = "A" + rest;
        else if( c == '1' )
            rest = "B" + rest + "B";
        else if( c == '2' )
            rest = rest + "C";
    }
    return rest;
}

6. Consider method make from question 5. What value must be passed in for input so that make returns the String "ABACAB"?

7. Rewrite method make from question 5 so that it is iterative (uses a for or while loop) instead of recursive.

8. Rewrite the following method so that it is recursive instead of iterative.

public int max(int[] list){
    int max = list[0];
    for(int i = 1; i < list.length; i++)
        if( list[i] > max )
            max = list[i];
    return max
}

Hint: It is much easier if you write a recursive helper and call it from max.

public int max(int[] list){
    return maxOfPortionOfList(list, 0);
}

private int maxOfPortionOfList(int[] list, int pos)
{    //base case. When is it very simple to get the max of a list
    // you complete this
}

9. What is the output of System.out.println( f(7) );

//pre: n >= 1
public int f(int n)
{    if( n == 1 || n == 2)
        return 1;
    else
        return f(n-1) + f(n-2);
}

10. Execute the following code class. It makes use of the Stopwatch class from the assignments.

public class SP06 {
    public static void main(String[] args) {

        Stopwatch s = new Stopwatch();
        final int LIMIT = 45;
        int result;
        for(int i = 1; i <= LIMIT; i++){
            s.start();
            result = f(i);
            s.stop();
            System.out.println("value of f(" + i + ") = " + result + ". Time to calculate: " + s);
        }

    }


    public static int f(int n){
        if( n == 1 || n == 2)
            return 1;
        else
            return f(n-1) + f(n-2);
    }
}

How long does it take to the program to calculate f(20), f(30), f(40), f(44), f(45). How long do you think it will take the program to calculate f(46)? f(50)? f(100)?

What do you think the Big O of method f is?

Rewrite method f so it is iterative with a Big O no worse than O(N).

11.    Flood fill.  Write a method to simulate the flood fill option on many graphics programs. A window will be represented as an 2D array of integers where the integers represent different colors. Floodfill will be passed a 2D array of integer as well as the row and column to start filling from and the color to change to.  The method should branch out from this position (up, down, left, and right, but not diagonally) and change all squares equal to the original one to the new color:

public void floodfill(in[]][] canvas, int row, int column, int newColor)

If canvas is equal to the matrix below:

1 1 1 1 1 1 1 1 1 1
1 1 1 5 5 5 5 5 5 1
1 1 5 1 1 1 1 1 5 1
1 5 1 1 12 1 12 1 5 1
1 5 1 1 1 1 1 1 5 1
1 5 1 1 55 55 55 1 5 1
1 1 5 1 1 1 1 1 5 1
1 1 1 5 1 1 1 1 5 1
1 1 1 5 5 5 5 5 5 1
1 1 1 1 1 1 1 1 1 1

 and row = 4, column = 2, and newColor = 33 the resulting matrix would be

1 1 1 1 1 1 1 1 1 1
1 1 1 5 5 5 5 5 5 1
1 1 5 33 33 33 33 33 5 1
1 5 33 33 12 33 12 33 5 1
1 5 33 33 33 33 33 33 5 1
1 5 33 33 55 55 55 33 5 1
1 1 5 33 33 33 33 33 5 1
1 1 1 5 33 33 33 33 5 1
1 1 1 5 5 5 5 5 5 1
1 1 1 1 1 1 1 1 1 1

Complete method floodfill using recursion.

12. Write a method that recursively determines the number of different ways to roll a particular number on a given number of dice:

/*
    pre: numDice > 0
    post: return the number of different ways to roll numToRoll
    with numDice dice. Assume each die has sides numbered 1 to 6.
*/
public int waysToRoll(int numToRoll, int numDice)

All dice have six sides. For example if the methods was called as follows:

System.out.println( waysToRoll(4,2) );

The output would be 3.

Die 1 Value Die 2 Value Total
1 3 4
3 1 4
2 2 4

Thus with 2 dice there are 3 combinations that add up to 4.  Note even though the first 2 both have a 3 and a 1 they are considered different combinations of rolls.

Another example: 

System.out.println( waysToRoll(5,3) );

 The output would be 6: 

Die 1 Value Die 2 Value Die 3 Value Total
1 1 3 5
1 3 1 5
3 1 1 5
1 2 2 5
2 1 2 5
2 2 1 5

After implementing a recursive solution implement a non recursive solution.