Welcome back, everyone! In this article, we'll walk you through the solutions for the Week 6 programming assignment of the NPTEL course "Joy of Computing Using Python." These answers are provided for reference, so feel free to use them as a guide. If you have any doubts or questions, don't hesitate to ask in the comments section below. Ready to dive in? Let's solve these problems together!
To Join Telegram: CLICK HERE
NPTEL Joy Of Computing Using Python Week 6 Programming Assignment 2024
Last Date: 05-09-2024
Q1.
Write a Python program that takes two positive integers a and b as input and returns their product using a recursive function. The function should only use the + and - operators to calculate the product.
Input Format:
The first line of input consists of two space-separated positive integers, a and b.
Output Format:
The output consists of a single integer representing the product of a and b.
Example:
Input:
3 4
Output:
12
Program Code:
Program Code:
def recursive_multiply(a, b):if b == 1:return areturn a + recursive_multiply(a, b - 1)a, b = map(int, input().split())print(recursive_multiply(a, b), end="") {codeBox}
Explanation:
- Base Case: When b is 1, the function returns a, because the product of any number and 1 is the number itself.
- Recursive Case: The function calls itself with b-1 and adds a to the result of that call. This effectively adds a to itself b times, which is equivalent to multiplication.
Example:
If the input is 3 4, the function will proceed as follows:
- recursive_multiply(3, 4) → 3 + recursive_multiply(3, 3)
- recursive_multiply(3, 3) → 3 + recursive_multiply(3, 2)
- recursive_multiply(3, 2) → 3 + recursive_multiply(3, 1)
- recursive_multiply(3, 1) → 3
Summing these results:
3 + 3 + 3 + 3 = 12
The output will be 12.
Q2.
Write a Python program that takes a positive integer x as input and returns the logarithm of x to the base 2 using a recursive function. The logarithm of x to the base 2, denoted by log₂(x), is the number of times 2 has to be multiplied by itself to get x. Assume that x is a power of 2.
Input Format:
The input consists of a single positive integer x which is a power of 2.
Output Format:
The output consists of a single integer representing log₂(x).
Example:
Input:
8
Output:
3
Program Code:
def recursive_log2(x):# Base case: if x is 2, the log2(x) is 1if x == 2:return 1# Recursive case: divide x by 2 and add 1 to the result of the recursive callreturn 1 + recursive_log2(x // 2)# Inputx = int(input())# Outputprint(recursive_log2(x), end="") {codeBox}
Explanation:
- Base Case: When 𝑥 is 2, the logarithm of 2 to the base 2 is 1, so the function returns 1.
- Recursive Case: The function divides x by 2 and adds 1 to the result of the recursive call. This counts the number of times you can divide 𝑥 by 2 before reaching 2.
Example:
If the input is 8, the function will proceed as follows:
- recursive_log2(8) → 1 + recursive_log2(4)
- recursive_log2(4) → 1 + recursive_log2(2)
- recursive_log2(2) → 1
Summing these results:
1 + 1 + 1 = 3
Q3.
Write a Python program that includes a recursive function named non-decreasing which takes a non-empty list L of integers as input. The function should return True if the elements in the list are sorted in non-decreasing order from left to right, and False otherwise.
Input Format:
The input consists of a single line containing space-separated integers that form the list L.
Output Format:
The output consists of a single boolean value (True or False) indicating whether the list is sorted in non-decreasing order.
Example:
Input:
1 2 2 3 4
Output:
True
Program Code:
def non_decreasing(L):# Base case: If the list has one or zero elements, it is sortedif len(L) <= 1:return True# Recursive case: Check if the first two elements are in non-decreasing order# and if the rest of the list is sortedif L[0] <= L[1]:return non_decreasing(L[1:])else:return False# InputL = list(map(int, input().split()))# Outputprint(non_decreasing(L)) {codeBox}
Explanation:
- Base Case: If the list L has one or zero elements, it is considered sorted, so the function returns True.
- Recursive Case: The function checks if the first element is less than or equal to the second element. If this condition is met, it recursively checks the rest of the list (i.e., the sublist excluding the first element). If the condition is not met, the function returns False.
Example:
If the input is 1 2 2 3 4, the function will proceed as follows:
- non_decreasing([1, 2, 2, 3, 4]) → True (since 1 ≤ 2, check [2, 2, 3, 4])
- non_decreasing([2, 2, 3, 4]) → True (since 2 ≤ 2, check [2, 3, 4])
- non_decreasing([2, 3, 4]) → True (since 2 ≤ 3, check [3, 4])
- non_decreasing([3, 4]) → True (since 3 ≤ 4, check [4])
- non_decreasing([4]) → True (base case, single element list)
Since all recursive checks return True, the final output will be True.