For the last weeks we've seen some bunch of code related with recursion but this week we started getting into it. For a very quick explanation, recursion is a very useful tool used in programming languages in order to solve a problem. Functions call themselves to solve smaller subproblems. We use recursion for our first assignment. In this week's lectures our lecturer showed us the function move_cheeses, and we traced it. move_cheeses(n - 1, source, destination, intermediate) is an example of recursion in a function.
Week- 5 Lab:
In this week's lab we dealt with recursion as I mentioned above. First, there were two functions that we had to trace before getting into writing our own recursive functions. Thus, we traced the GCD, Greatest Common Denominator. This function uses; csc148_gcd(n2, n1 % n2) if n2 > 0 else n1. Therefore csc148_gcd(15, 35) gives us the result 5, since 5 is the greatest number that can divide both 15 and 35. Afterwards we traced Binary Representation just like GCD. Then we wrote our own recursive function for SearchList to determine whether a given integer is somewhere in the SearchList or not.
No comments:
Post a Comment