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:
- Problems with a natural recursive structure, such as tree traversal, factorial calculation, Fibonacci sequence, or solving mazes.
- Code simplicity: Recursion can make code simpler and more elegant for problems like calculating permutations, combinations, or solving mathematical series.
How Recursive Functions Work
- Base Case: Stops the recursion when a certain condition is met.
- 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:
For example:
In recursive terms:
Where the base case is
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:
factorial (5)
callsfactorial (4)
.factorial (4)
callsfactorial (3)
,- factorial (3) calls factorial (2),
- factorial (2) calls factorial (1)
- When
factorial (1)
is called, it returns 1 (base case). - The values are then multiplied as the recursion "unwinds":
- factorial (1) returns 1 as an output.
- which gets multiply with n= 2 so 2 * factorial (2-1) => 2 * factorial (1) => 2 * 1 => 2
- 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
- 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
- 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
- So, finally we get factorial of 5 is 120.
- .
Questions to write in Theory are as follows.
- Why do we require recursive function explain with example? already answer given above.
- What is mean by nested function explain with example?
- What is map () function and why do we require map () function explain with example?
- What is filter () function and why do we require filter () function explain with example?
- 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:
- Why do we require Inheritance?
- With example explain the concepts of multiple inheritance.
- Explain with example multiple exceptions?
- What is assert statement?
- Explain with example how to raise an exception?
- What is IO Stream and buffers?
- Explain how to create file in writing mode? also how to write content inside the file and how to read those and print?
- When to use seek () and tell () method explain with example.?
- Explain the difference between readline() and readlines() method?
- 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.
No comments:
Post a Comment