Friday, 15 November 2024

FPP Python Assignments 6 to 7. Theory and Flowchart.


 




Assignment No:06

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.

Learning Objectives:
  • To understand the basics about python inbuilt functions like map, filter and lambda.
  • To learn how to write and execute recursive and nested function.
Learning Outcomes:
  • Upon completing this lab exercise, the learner will be able to
  • Able to identify how and when to use map function.
  • Able to identify how and when to use filter function.
  • Understand the use of lambda function and its implementation.
  • Implementation of recursive function and nested function.

Theory:

1. What is recursive function?

Ans: A recursive function is a function that calls itself to solve a smaller instance of the same problem. This process continues until a base condition is met, which stops the recursion.

Why Do We Need Recursive Functions?

Recursive functions are especially useful when solving problems that can be broken down into smaller, similar sub-problems. Common use cases include:

  1. Problems with a natural recursive structure, such as tree traversal, factorial calculation, Fibonacci sequence, or solving mazes.
  2. Code simplicity: Recursion can make code simpler and more elegant for problems like calculating permutations, combinations, or solving mathematical series.

How Recursive Functions Work

  1. Base Case: Stops the recursion when a certain condition is met.
  2. Recursive Case: The function calls itself with a smaller input, working toward the base case.

Example: Factorial Calculation

The factorial of a number n is defined as:

n!=n×(n1)×(n2)××1

For example:

5!=5×4×3×2×1=120

In recursive terms:

n!=n×(n1)!

Where the base case is 1!=1


Program:

def factorial(n):

    # Base case

    if n == 0 or n == 1:

        return 1

    # Recursive case

    return n * factorial (n - 1)


# Using the recursive function

print (factorial (5)) # Output: 120

How it works:

  1. factorial (5) calls factorial (4).
  2. factorial (4) calls factorial (3)
  3. factorial (3) calls factorial (2),
  4. factorial (2) calls factorial (1)
  5. When factorial (1) is called, it returns 1 (base case).
  6. The values are then multiplied as the recursion "unwinds":
  7. factorial (1) returns 1 as an output.
  8. which gets multiply with n= 2 so 2 * factorial (2-1) => 2 * factorial (1) => 2 * 1 => 2
  9.  That means factorial (2) returns 2 as an output which gets multiply with, n = 3 so 3 * factorial (3-1) => 3 * factorial (2) => 3 * 2 => 6
  10. That means factorial (3) returns 6 as an output which gets multiply with, n = 4 so 4 * factorial (4-1) => 4 * factorial (3) => 4 * 6 => 24
  11. That means factorial (4) returns 24 as an output which gets multiply with, n = 5 so 5 * factorial (5-1) => 5 * factorial (4) => 5 * 24 => 120
  12. So, finally we get factorial of 5 is 120. 
  13. .
Questions to write in Theory are as follows.
  1. Why do we require recursive function explain with example? already answer given above.
  2. What is mean by nested function explain with example?
  3. What is map () function and why do we require map () function explain with example?
  4. What is filter () function and why do we require filter () function explain with example?
  5. What is lambda () function and why do we require lambda () function explain with example?
Note: From question no 3 to 5 all answers you will get in this link, click on it read it, understand it, try to implement each example for better understanding and then write as an assignment in your file so that at the time of oral you are able to answer all the questions confidently.   

Click on the below link:

Algorithm:

Step 1: Start
Step 2: Accept n numbers from the user and stored it inside numbers [] list.
Step 3: Pass numbers [] list as an argument to sum_of_square_of_evens(numbers)
Step 3.1 First find out the even numbers from the list with the help of filter function.
Step 3.2 Secondly with the help of map function find out the square of all even numbers and stored it into squares.
Step 3.3 Pass this square in list type to recursive_sum function to find out sum of square of even numbers.
Step 4: Show the sum to user
Step 5: Stop

Flowchart:




Conclusion: Hence, we learn how to execute the recursive and nested function in python. Also learn how to implement built in function map, filter and lambda in python.


----------------------------------------------------------------------------------------------------------------------
Assignment No:07
Problem Statement: 
Write a python program by creating a class called Employee to store the details of Name, Employee_ID, Department and Salary, and implement a method to update salary of employees belonging to a given department.

Aim:  Basic operations on class and its operations.

Learning Objectives:
  • To understand the basics about python Classes concept
  • To learn how to write and execute python object-oriented program.
Learning Outcomes:
  • Upon completing this lab exercise, the learner will be able to
  • Write a basic python program using class.
  • Able to implement various features of object-oriented programming.
  • Understand the memory management of objects.

Theory:
  1. Why do we require Inheritance?
  2. With example explain the concepts of multiple inheritance.
  3. Explain with example multiple exceptions?
  4. What is assert statement?
  5. Explain with example how to raise an exception?
  6. What is IO Stream and buffers?
  7. Explain how to create file in writing mode? also how to write content inside the file and how to read those and print?
  8. When to use seek () and tell () method explain with example.? 
  9. Explain the difference between readline() and readlines() method?
  10. Explain with example method overriding concept?

Algorithm:
a) Algorithm to implement a method to update salary of employees belonging to a given department.
Step 1: Start
Step 2: Ask user how many (n) employees' information he /she wants to store
Step 3: Accept the n employee details like employee id, name, department and salary from the user.
Step 4: Display the n employee information.
Step 5: Now ask user which department salary he/she wants to update.
Step 6: Ask user for the amount to update in existing salary. 
Step 7: Search the department among n available objects and update accordingly.
Step 8: Finally, once again show the updated record of employees.
Step 9: Stop.

Flowchart:

a) Flowchart to implement a method to update salary of employees belonging to a given department.




Note: The above code shows lab time explanation.


Conclusion: Hence, we learn how to execute the python class and object program through array of objects and also learn how to accept input from the user and how to process this input with basic arithmetic operations.

data structures and algorithms Web Developer

No comments:

Post a Comment