27
loading...
This website collects cookies to deliver better user experience
Day 1 We learnt how to make user defined functions. User defined functions are custom functions designed by the programmer. They provide code reusability and flexibility while solving complicated problems. All this was backed up with two sample questions and one conceptual exercise.
Day 2 We learnt how to return values from functions. By breaking complicated operations into functions, we can make the code more compact and flexible. This is possible because we can return data-types back to the function call. Returning multiple values is invalid, but we can return a list which can hold the values.
Day 3 We studied recursion in Python. We implemented it by calling the function from inside it. We then solved some good questions related to recursion.
Recursion is a tool to allow programmer to concentrate on the key step of an algorithm, without initially having to worry about how to couple that step with the others.
The Greatest Common Divisor (GCD) of two positive integers is the largest integer that divides both of the two integers. Thus, the GCD of 8 and 12 is 4, the GCD of 9 and 18 is 9, and the GCD of 16 and 25 is 1. Write a recursive function namd GCD (x,y)
that implements this division algorithm: if y=0, then the GCD of x and y is x; otherwise the GCD of x and y is the same as the GCD of y and x%y.
The binomial coefficients may be defined by the following recurrence relation, which is the idea of the pascal's triangle.
C(n,0)=1 and C(n,n)=1 for n>=0
C(n,k)=C(n-1,k)+C(n-1,k-1) for n>k>0
Write a recursive function which will generate C(n,k) by using the above formula
The Ackermann's function, defined as follows, is a standered device to determine how well recursion is implemented in any device.
A(0,n)=n+1 for n>=0
A(m,0)=A(m-1,1) for m>0
A(m,n)=A(m-1,A(m,n-1)) for m>0 and n>0
Yes, the above exercises are a bit difficult. But the key is finding C(n,k) or A(m,n) and directly implementing it. The rest is just about when to stop the recursion conditions. All the best for solving them! Solving these exercises will make you all fluent with recursion and it's problem solving principles.
For those who have not yet made account in Dev.to, you can have a free easy sign-up using your mail or GitHub accounts. I would suggest the budding developers to create your GitHub free account right away. You would require to register sooner or later anyways