MODULE 3 – REMEDIAL NOTES
UCEST105 – Algorithmic Thinking with Python
MODULE 3
This module mainly focuses on:
Recursion
Functions
Matrices
Loops and conditions
Python basics
MOST IMPORTANT TOPICS
Recursion
Fibonacci using
recursion
Recursive search
Matrix type checking
Triangle program
Functions
WHAT IS RECURSION?
Recursion means:
A function calling itself.
TWO IMPORTANT PARTS OF RECURSION
Part Meaning
Stopping
Base Case
condition
Recursive Function calls
Call itself
SIMPLE EXAMPLE
def fun(n):
if n == 0:
return
print(n)
fun(n-1)
Here:
if n==0 → base case
fun(n-1) → recursive call
PART A QUESTIONS (3 MARKS)
These are short programs or concepts.
EXPECTED QUESTION 1
Sum of Even Numbers
Question
Write a Python program to find sum of even numbers from N numbers.
n = int(input("Enter limit: "))
sum = 0
for i in range(n):
num = int(input())
if num % 2 == 0:
sum = sum + num
print("Sum =", sum)
EXPECTED QUESTION 2
Recursive Search
Question
Write recursive function to search a key in list.
Easy Answer
def search(lst,key,index):
if index == len(lst):
return -1
if lst[index] == key:
return index
return search(lst,key,index+1)
numbers = [10,20,30,40]
print(search(numbers,30,0))
IMPORTANT POINTS
Statement Meaning
index == key not
len(lst) found
lst[index] ==
key found
key
search(...index recursive
+1) call
EXPECTED QUESTION 3
Randomized Approach
Question
What is motivation for using randomized approach?
Randomized Approach
Randomized approach uses random values to solve problems efficiently.
Advantages:
1. Faster execution
2. Useful for large problems
3. Reduces complexity
4. Gives good approximate solutions
Example:
Random password generation.
EXPECTED QUESTION 4
Triangle Checking Program
Question
Write a program to check type of triangle using three sides.
Easy Answer
a = int(input())
b = int(input())
c = int(input())
if a==b and b==c:
print("Equilateral")
elif a==b or b==c or a==c:
print("Isosceles")
else:
print("Scalene")
EXPECTED QUESTION 5
Function Returning Multiple Values
Question
Explain one method to return multiple values from function.
Using Tuple
A function can return multiple values using tuple.
Example
def calc(a,b):
return a+b, a*b
x,y = calc(2,3)
print(x)
print(y)
PART B QUESTIONS (9 MARKS)
IMPORTANT B PART QUESTION 1
Fibonacci using Recursion
Question
Generate first n Fibonacci numbers using recursion.
Fibonacci Series
Series:
0112358
Each number:
sum of previous two numbers
Recursive Function
def fib(n):
if n <= 1:
return n
return fib(n-1) + fib(n-2)
n = int(input())
for i in range(n):
print(fib(i), end=" ")
Explanation
Statement Meaning
n<=1 base case
fib(n- recursive
1)+fib(n-2) call
IMPORTANT B PART QUESTION 2
Minimum and Maximum using Recursion
Question
Write recursive function to find minimum and maximum in array.
def minmax(arr,index,minv,maxv):
if index == len(arr):
return minv,maxv
if arr[index] < minv:
minv = arr[index]
if arr[index] > maxv:
maxv = arr[index]
return minmax(arr,index+1,minv,maxv)
arr = [2,8,1,6,9]
mn,mx = minmax(arr,0,arr[0],arr[0])
print("Minimum =",mn)
print("Maximum =",mx)
The function minmax() takes the array, current index, minimum value, and
maximum value as arguments. First, it checks the base case using if index
== len(arr). This means all elements are checked, so the function returns
the minimum and maximum values. Then the program compares the
current array element with minv and maxv. If the current element is
smaller than minv, the minimum value is updated. If the current element
is larger than maxv, the maximum value is updated. After checking one
element, the function calls itself recursively using index+1 to move to the
next element in the array. For the array [2,8,1,6,9], the minimum value
becomes 1 and the maximum value becomes 9. Finally, the program
prints both values.