Sunday, 24 November 2024

Write a Python program that demonstrates the use of a recursive function, nested functions (FPP Assignment no 6)



Problem Statement:

Write a Python program that demonstrates the use of a recursive function, nested functions, and functional programming techniques such as map, filter, lambda functions and print the result, which is the sum of the squares of the even number.

Implementation / Python Code:

def sum_of_squares_of_evens(numbers):
    # Recursive function to calculate the sum of a list
    def recursive_sum(myLst):
        if not myLst:
            return 0
        return myLst[0] + recursive_sum(myLst[1:])

    # Nested function to calculate the square of a number
    def square(n):
        return n * n

    # Filter even numbers using a lambda function
    evens = filter (lambda x: x % 2 == 0, numbers)

    # Map to calculate the square of each number
    squares = map (square, evens)

    # Calculate and return the sum of squares
    return recursive_sum(list(squares))

# Example usage
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = sum_of_squares_of_evens(numbers)
print ("Sum of the squares of even numbers:", result)



Output:
Sum of the squares of even numbers: 220

Explanation:
1. First, we are accepting numbers inside numbers [] list which is 1 to 10.

2. We are calling the function sum_of_squares_of_evens (numbers) by passing numbers list as an argument for processing.

3. Control goes to the definition of sum_of_squares_of_evens (numbers): function. here we have written two functions definition inside it. 1. recursive_sum () function to find sum of the square of the even numbers. 2. square () function to find the square of the even numbers. now to call function definition we know function calling must be there so,

4. directly control goes to the line
evens = filter (lambda x: x % 2 == 0, numbers) for execution. here we just filtering the even numbers with the help of lambda function from whole numbers [] list and stored it inside evens [] list.

5. once we get the even number then we are calling map function to find square of all even numbers and stored it inside squares [] objects list who stores the map objects.
    squares = map (square, evens)
Here we use the concept of nested function. how so we first we call
sum_of_squares_of_evens (numbers) function
                     |
                     |
inside this function we call map () function from map () functions we call the square () function.

6. Now we have a square of all even numbers in squares which we are passing to the recursive function recursive_sum(list(squares)) to find out sum of all those squares.
here recursive means:

    def recursive_sum(myLst):
        if not myLst:
            return 0
        return myLst[0] + recursive_sum(myLst[1:])

assume myLst contains the squares of all even numbers from 1 to 10.
                0   1    2    3    4
myLst = [4, 16, 36, 64, 100]

so first it checks myLst [] elements are there or not if not return 0 but here myLst[] is having elements in it call once again the same function with different value. this time
myLst[0] = myLst[0] + recursive_sum(myLst[1]) # 16

again, it checks myLst[] elements are there or not if not return 0 else call once again the same function with different value. this time
myLst[0] = myLst[0] + recursive_sum(myLst[2]) # 36

again, it checks myLst[] elements are there or not if not return 0 else call once again the same function with different value. this time
myLst[0] = myLst[0] + recursive_sum(myLst[3]) #64

again, it checks myLst[] elements are there or not if not return 0 else call once again the same function with different value. this time
myLst[0] = myLst[0] + recursive_sum(myLst[4]) # 100

again, it checks myLst[] elements are there or not if not return 0 else call once again the same function with different value. this time
myLst[0] = myLst[0] + recursive_sum(myLst[5]) # 0

Now this time for recursive_sum(myLst[5]) elements are not there so it returns 0 now calling is done 5 times now for each function call (5 times) it returns the value as follows:

myLst[0] = myLst[0] + recursive_sum(myLst[5]) # return 0
myLst[0] = 4 + 0 => 4

myLst[0] = myLst[0] + recursive_sum(myLst[4]) # return 100
myLst[0] = 4 + 100 => 104

myLst[0] = myLst[0] + recursive_sum(myLst[3]) # return 64
myLst[0] = 104 + 64 => 168

myLst[0] = myLst[0] + recursive_sum(myLst[2]) # return 36
myLst[0] = 168 + 36 => 204

myLst[0] = myLst[0] + recursive_sum(myLst[1]) # return 16
myLst[0] = 204 + 16 => 220

Output: So, finally we get the output of sum is 220.
Sum of the squares of even numbers: 220
  
Hope you all get the clear idea of this program.


data structures and algorithms Web Developer

No comments:

Post a Comment