40 likes | 165 Vues
Today, we'll delve into recursive functions by first reviewing the "count number of 'a's" example. We'll then explore an alternative recursive string function and discuss the natural recursive structure of files and directories. You'll have the opportunity to write a recursive function for files and directories, reinforcing your understanding of recursion. If time permits, we’ll tackle additional practice problems such as count7, bunnyEars2, and an ancient Fibonacci challenge. Join us in engaging with recursion and its applications!
E N D
What we will do today • You’ll try to write a recursive function to solve a simple problem • We’ll return to the count number of “a”s example from the previous day, and I’ll explore it a bit more • I’ll discuss an alternative recursive string function • I’ll discuss the “natural” recursive structure of files and directories, and go through a example with that • You will also write a recursive function with files and directories • If we have time: even more practice with recursive functions
First Recursion Problem • Go to http://codingbat.com/java/Recursion-1 • Solve count7 • If you finish, solve bunnyEars2 • If you finish, do fibonacci (this one is so old that I solved in my very first CS class…and it was pretty dang old then) • If you finish, do a tiny green dance in your chair and then move on to some more
publicintcountAs(String input) { if(input.isEmpty()) return 0; String restOfString = input.substring(1); intrestOfStringCount = countAs(restOfString); if(input.charAt(0) == 'a') { returnrestOfStringCount + 1; } else { returnrestOfStringCount; } }
A genuinely good example of recursion How would you find the largest file within a directory (oh, and this directory can contain other directories)