Recursion || P.I.P = 6.1 || Sumita Arora || Practical || Class 12 || Computer science
Q4. Designing recursive code. A Geometric Progression (GP) is a progression where the each term is a multiple of the previous one. The multiplying factor is called the common ratio.
So a GP with a first term a and a common ratio r with n terms, can be stated as:
a, ar, ar2, ar³, ar4…. arn-1
(a) Write a recursive function that prints a GP. Input a, r and n in_main_part..
(b) Write a recursive function that calculates the sum of a GP by changing the function that you wrote in part (a). Obtain a, r and n in_main_ part. Highlight the changes that were made to get the desired result.
Q 5 = We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10.
Implement this recursive functionality in Python and test it using a main function that calls this with the values 15, 105, and 15105.
Hint. Remember, in Python 3.x if n is an integer, n/10 will not be an integer.
Q 6 = Write a recursive Python function that has a parameter representing a list of integers and returns the maximum stored in the list. Thinking recursively, the maximum is either the first value in the list or the maximum of the rest of the list, whichever is larger. If the list only has 1 integer, then its maximum is this single value, naturally.
Hint. If A is a list of integers, and you want to set the list B to all of the integers in A except the first one, you can write B = A [1: len (A)]
Post a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )