10. Recursion

  • Feel free to use your laptop

  • You are strongly encourage to work with others

    • When you get stuck, ask those sitting around you for help

    • Get used to working together in the labs

    • Peer teaching and peer learning has been empirically shown to be very effective

10.1. Recursion Visualization

  1. Check out the following website to help visualize recursion:

  2. Try running the following in the visualizer

    public class Recursion {
        public static void main(String[] args) {
            sum(5);
        }
    
        public static int sum(int n) {
            if (n == 1) {
                return 1;
            }
            return n + sum(n - 1);
        }
    }
    

10.2. Factorial

Note

Obviously one can simply copy the code from the Recursion Topic, but this defeats the purpose of the lab. Instead, slowly and deliberately implement each method and take the time to understand the details.

  1. Read up on factorial

  2. Write an iterative method to return the factorial of n

    • This method will take n as a parameter

    • The method will return the factorial

  3. Write a recursive method to return the factorial of n

    • This method will take n as a parameter

    • This recursive method must start at n and work down to 1

    • The method will return the factorial

  4. Write a recursive method to return the factorial of n

    • This method will take n and current as a parameter

    • This recursive method must start at current (which will start at 1) and work up to n

    • The method will return the factorial

10.3. Linear Search on an Array

  1. Write a recursive method to do a linear search

    • This method will take an array of integers haystack, an integer needle, and the current index current as parameters

    • This method will return a boolean indicating if the needle was found

10.4. Building a Linked Structure

  1. Write a recursive method to build a linked structure containing the numbers 0(n-1)

    • This method will take an integer n and any other required parameters

    • This method will return a reference to the head of the linked structure

    • The first node must contain 0

    • This will require a working Node class

10.5. Linear Search on a Linked Structure

  1. Write a recursive method that performs a linear search on a linked structure

    • The method will take a reference to the current node called current and the needle

    • This method will return a boolean indicating if the needle was found

10.6. Kattis Problems

  1. https://open.kattis.com/problems/quadrant

  2. https://open.kattis.com/problems/judgingmoose

  3. https://open.kattis.com/problems/timeloop

  4. https://open.kattis.com/problems/oddities

  5. https://open.kattis.com/problems/fizzbuzz

  6. https://open.kattis.com/problems/twostones

  7. https://open.kattis.com/problems/spavanac

  8. https://open.kattis.com/problems/cetvrta

  9. https://open.kattis.com/problems/bus

  10. https://open.kattis.com/problems/timeloop

  11. https://open.kattis.com/problems/oddities

  12. https://open.kattis.com/problems/fizzbuzz

  13. https://open.kattis.com/problems/sibice

  14. https://open.kattis.com/problems/datum

  15. https://open.kattis.com/problems/dicecup

  16. https://open.kattis.com/problems/autori

  17. https://open.kattis.com/problems/apaxiaaans

  18. https://open.kattis.com/problems/hissingmicrophone

  19. https://open.kattis.com/problems/trik

  20. https://open.kattis.com/problems/pot

  21. https://open.kattis.com/problems/filip

  22. https://open.kattis.com/problems/reversebinary

  23. https://open.kattis.com/problems/sevenwonders

  24. https://open.kattis.com/problems/zamka

  25. https://open.kattis.com/problems/bijele

  26. https://open.kattis.com/problems/cold

  27. https://open.kattis.com/problems/nastyhacks

  28. https://open.kattis.com/problems/grassseed

  29. https://open.kattis.com/problems/pet

  30. https://open.kattis.com/problems/batterup

  31. https://open.kattis.com/problems/aboveaverage

  32. https://open.kattis.com/problems/icpcawards

  33. https://open.kattis.com/problems/quickbrownfox

  34. https://open.kattis.com/problems/nodup

  35. https://open.kattis.com/problems/conundrum

  36. https://open.kattis.com/problems/bela

  37. https://open.kattis.com/problems/kornislav