0% found this document useful (0 votes)
8 views24 pages

Python DSA Lab Programs Overview

The document is a lab file for a Data Structures and Algorithms course, detailing various programming experiments conducted by a student named Kishlay. It includes objectives, algorithms, and conclusions for each experiment, covering topics such as battery life calculation, array manipulation with NumPy, matrix operations, conditional statements, and recursion. The lab file serves as a comprehensive guide to practical applications of data structures and algorithms in Python.

Uploaded by

jatin.gupta4208
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views24 pages

Python DSA Lab Programs Overview

The document is a lab file for a Data Structures and Algorithms course, detailing various programming experiments conducted by a student named Kishlay. It includes objectives, algorithms, and conclusions for each experiment, covering topics such as battery life calculation, array manipulation with NumPy, matrix operations, conditional statements, and recursion. The lab file serves as a comprehensive guide to practical applications of data structures and algorithms in Python.

Uploaded by

jatin.gupta4208
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Kishlay DSA 24/A17/014

DATA STRUCTURES
AND ALGORITHMS
EE102
LAB FILE

Academic Year
2024-25

Submitted to: Submitted by


[Link] Singh Kishlay
Dept. of Electrical Engineering 24/A17/014
Kishlay DSA 24/A17/014

Index
S. No. Objective Date Signature
1 Write a program to Calculate Battery Life
capacity
2 Write a program to make array and print
it by importing numpy
3 Write a program to perform operation on
list
4 Write a program to addition of matrix

5 Write a program to multiplication of


matrix
6 To understand various Conditional
Statements in python
7 Write a program to find resistance of
resistor with color code
8 Write a program to make a linked list

9 Write a program to Generate Electricity


Bill
10 Write a program to find factorial
11 Write a program to find greatest
common divisor
12 Write a program to form pascal’s triangle

13 Write a program to solving tower of


Hanoi problem using recursion
14 Write a program to sorting a stack using
recursion
Kishlay DSA 24/A17/014

Experiment 1: Write a program to Calculate Battery Life


capacity Algorithm:

Information:
This program calculates the battery life of a device based on its battery capacity,
power consumption, and efficiency. The formula used is:

Battery Life (hours)=Battery Capacity (mAh)Device Consumption (mAh)×Efficiency


Battery Life (hours)=Device Consumption (mAh)Battery Capacity (mAh)×Efficiency
For example, if a device has a 5000mAh battery, consumes 1000mAh per hour, and
operates at 85% efficiency, the estimated battery life is:
Battery Life=50001000×0.85=4.25 hoursBattery Life=10005000×0.85=4.25 hours

Algorithm
1. Input the battery capacity in mAh.
2. Input the device consumption rate in mAh.
3. Input the efficiency factor (default is 85% or 0.85).
4. Apply the formula to calculate battery life.
5. Return and display the calculated battery life.

Code:
Kishlay DSA 24/A17/014

Output:

Conclusion
The program provides an estimate of how long a device can run on its battery based
on its consumption rate and efficiency factor.

Experiment 2: Write a program to make array and print it


by importing numpy
Information
This program demonstrates how to create and print an array using NumPy. NumPy is
a powerful Python library for numerical computations, offering multi-dimensional
arrays and mathematical functions.

Algorithm
1. Import the numpy library.
2. Create an array using [Link]() with predefined elements.
3. Print the created array.

Code:
Kishlay DSA 24/A17/014

Output:

Conclusion
The program showcases how to create arrays using NumPy, which is widely used for
scientific computing and data analysis due to its efficiency and simplicity.

Experiment 3: Write a program to perform operation on


list

Information
This program performs basic operations on a list, such as adding, removing, sorting,
and reversing elements. Lists are one of Python's most versatile data structures.

Algorithm
1. Create a list of appliances (e.g., ["Fan", "Light", "AC", "Heater"]).
2. Perform operations:

• Add an item using .append().


• Remove an item using .remove().
• Sort items alphabetically using .sort().
• Reverse items using .reverse().
3. Print the list after each operation.
Kishlay DSA 24/A17/014

Code:

Output:

Conclusion
The program demonstrates how to manipulate lists in Python effectively for various
use cases like managing data dynamically.
Kishlay DSA 24/A17/014

Experiment 4: Write a program to addition of matrix

Information
Matrix addition involves adding corresponding elements of two matrices of the same
dimensions. This program uses NumPy for matrix operations.

Algorithm
1. Define two matrices using [Link]().
2. Add matrices element-wise using + operator.

3. Print the resulting matrix.

Code:

Output:
Kishlay DSA 24/A17/014

Conclusion
The program simplifies matrix addition using NumPy's built-in capabilities for
efficient computation.

Experiment 5: Write a program to multiplication of


matrix
Information
Matrix multiplication involves computing the dot product of rows from one matrix
with columns from another matrix. This operation is widely used in linear algebra and
machine learning.

Algorithm
1. Define two matrices using [Link]().
2. Multiply matrices using [Link]() or @ operator.
3. Print the resulting matrix.

Code:

Output:
Kishlay DSA 24/A17/014

Conclusion
The program efficiently performs matrix multiplication using NumPy's optimized
functions for high-performance computing.

Experiment 6: To understand various Conditional


Statements in Python
Information
Conditional statements (if, elif, else) allow decision-making in Python programs
based on conditions. This program categorizes appliances based on their power
ratings.

Algorithm
1. Define a dictionary with appliance names as keys and their power ratings as
values (e.g., {"Fan": 75, "Light": 60}).
2. Use conditional statements to categorize appliances:
• Low-power: Rating <=100.
• Medium-power: Rating >100 but <=1000.
• High-power: Rating >1000.
3. Print the category for each appliance.
Kishlay DSA 24/A17/014

Code:

Output:

Conclusion
The program highlights how conditional statements can be used to make decisions
dynamically in Python programs.
Kishlay DSA 24/A17/014

Experiment 7: Write a program to find resistance of


resistor with color code
Information
Resistors use color codes to indicate their resistance values. Each color represents a
digit or multiplier value:
• Black = 0
• Brown = 1
• Red = 2
• Orange = 3
• Yellow = 4
• Green = 5
• Blue = 6
• Violet = 7
• Gray = 8
• White = 9
Resistance is calculated as:
Resistance=(Band1×10+Band2)×(10Band3)Resistance=(Band1×10+Band2)×(10Band
3)

Algorithm
1. Define a dictionary mapping colors to digits.
2. Input three color bands representing Band1, Band2, and Band3.
3. Calculate resistance using the formula above.
4. Return and display resistance value.
For example:

• Colors: Brown (1), Black (0), Red (Multiplier 102102).

• Resistance: (10+0)×(102)=1000Ω(10+0)×(102)=1000Ω.
Kishlay DSA 24/A17/014

Code:

Output:

Conclusion
The program accurately converts resistor color codes into numeric resistance values.
Kishlay DSA 24/A17/014

Experiment 8: Write a program to make a linked list


Information
A linked list is a data structure consisting of nodes where each node contains data
and a reference to the next node in the sequence.

Algorithm
1. Define a Node class with attributes for data and next pointer.
2. Define a LinkedList class with methods to:
• Append nodes to the list.
• Display all nodes sequentially.

3. Append nodes containing appliance names like "Fan", "Light", etc.


4. Display the linked list by traversing it from head to tail.

Code:
Kishlay DSA 24/A17/014

Output:

Conclusion
The program demonstrates how to implement and manipulate linked lists
dynamically in Python.
Kishlay DSA 24/A17/014

Experiment 9: Write a program to find Ackermann


function
Information
Electricity bills are calculated based on total units consumed by appliances and a
fixed rate per unit of electricity consumed.

Algorithm
1. Input appliance usage data (in kWh).
2. Sum up total units consumed across all appliances.
3. Multiply total units by rate per unit to calculate bill amount.
4. Print the bill amount.
For example:

• Usage Data: Fan = 5050, Light = 3030, AC = 200200 kWh.


• Rate per unit = ₹5.
• Total Bill Amount = 280×₹5=₹1400280×₹5=₹1400.

Code:
Kishlay DSA 24/A17/014

Output:

Conclusion
The program provides an easy way to calculate electricity bills dynamically based on
usage data.

Experiment 10: Write a program to find factorial


Information
Factorial of nn is calculated as:
n!=n×(n−1)×(n−2)⋯×1n!=n×(n−1)×(n−2)⋯×1
This program uses recursion to compute factorials efficiently.

Algorithm
1. Define a recursive function:
• Base case: If n=0n=0 or n=1n=1, return 11.
• Recursive case: Return n×factorial(n−1)n×factorial(n−1).
2. Input a number nn.
3. Compute factorial recursively and print it.
For example:
Factorial of 5!=5×4×3×2×1=1205!=5×4×3×2×1=120.
Kishlay DSA 24/A17/014

Code:

Output:

Conclusion
The recursive approach simplifies factorial computation for any positive integer input.
Kishlay DSA 24/A17/014

Experiment 11: Write a program to find greatest


common divisor
Information
The Greatest Common Divisor (GCD) of two integers is the largest number that
divides both numbers without leaving a remainder. For example, the GCD of 56 and
98 is 14.
Python provides a built-in function [Link]() to calculate the GCD efficiently.

Algorithm
1. Import the math module.
2. Input two integers, a and b.
3. Use the [Link](a, b) function to calculate the GCD.
4. Print the result.

Code:

Output:
Kishlay DSA 24/A17/014

Conclusion
The program efficiently computes the GCD of two numbers using Python's built-
in [Link]() function, which is optimized for this purpose.

Experiment 12: Write a program to form Pascal’s triangle


Information:
Pascal's Triangle is a triangular array of numbers where each number is the sum of
two directly above it in the previous row. The first row starts with 1, and subsequent
rows are generated based on this rule.

For example:
text
1
11
121
1331
14641

Algorithm
1. Start with one row containing ``.
2. For each subsequent row:
• Add 1 at both ends.
• Compute intermediate values as sums of adjacent values from the
previous row.
3. Repeat for n rows (where n is user input).
4. Print each row of Pascal's Triangle.

Code:
Kishlay DSA 24/A17/014

Output:

Conclusion
The program dynamically generates Pascal's Triangle for any number of rows entered
by the user, showcasing recursive patterns in mathematics.
Kishlay DSA 24/A17/014

Experiment 13: Write a program to solve Tower of Hanoi


problem using recursion
Information
The Tower of Hanoi is a classic mathematical puzzle where you move disks from one
peg to another following these rules:
• Only one disk can be moved at a time.
• A larger disk cannot be placed on top of a smaller disk.
• All disks must be moved from the source peg to the target peg using an
auxiliary peg.
This program solves the problem recursively.

Algorithm
1. Define a recursive function:
• Base case: If there are no disks (n = 0), return.
• Recursive case:
• Move n-1 disks from source to auxiliary peg.
• Move the nth disk from source to target peg.
• Move n-1 disks from auxiliary peg to target peg.
2. Input the number of disks and call the recursive function.

3. Print each move step-by-step.


Example:

For n = 3, moves are:


Move disk 1 from A to C
Move disk 2 from A to B
Move disk 1 from C to B
Move disk 3 from A to C
Move disk 1 from B to A
Move disk 2 from B to C
Move disk 1 from A to C
Kishlay DSA 24/A17/014

Code:

Output:

Conclusion
The recursive solution elegantly solves this classic problem while demonstrating
recursion principles and problem-solving techniques.
Kishlay DSA 24/A17/014

Experiment 14: Write a program to sort a stack using


recursion
Information
Sorting a stack involves rearranging its elements so that they are in ascending order
while maintaining stack properties (Last-In-First-Out or LIFO). This program uses
recursion to sort a stack without using any additional data structures.

Algorithm
1. Define a recursive function sort_stack(stack):

• Base case: If the stack is empty, return.


• Recursive case:
• Pop an element from the stack.
• Recursively sort the remaining stack.
• Insert the popped element into its correct position in the sorted
stack using another recursive function.
2. Define another recursive function insert_sorted(stack, element):
• If the stack is empty or the element is greater than the top of the stack,
push it onto the stack.
• Otherwise, pop an element, recursively call insert_sorted(), and then
push back the popped element.
3. Input an unsorted stack and call sort_stack() on it.
4. Print the sorted stack after completion.
Example:

• Input Stack: [34, -5, -23, -45]


• Sorted Stack: [-45, -23, -5, 34]
Kishlay DSA 24/A17/014

Code:

Output:

Conclusion
The recursive approach efficiently sorts stacks without additional data structures
beyond recursion calls. It demonstrates how recursion can be used for complex data
manipulations like sorting while adhering to constraints (e.g., LIFO).

You might also like