Os Lab 1..
Os Lab 1..
ENGINEERING AND
TECHNOLOGY TAXILA
Lab Manual# 2
Discrete Structures and
Algorithm(OS)
Code:
Output:
Task 2:
Advanced Python Arrays
Code:
Output:
Task 3:
Solving Real-Life
Problems with Array
Code:
Output:
Task 4:
Python Classes and Objects
Code:
Output:
Task 5:
Encapsulation Using Private Members
Code:
Output:
Task 6:
Python Inheritance
Code:
Output:
Task 7:
Polymorphism (Overloading and Overriding)
Code:
Output:
Task 8:
Abstraction Using Abstract Classes
Code:
Output:
Task 9:
Python Modules
Code:
Output:
Task 10:
Python File handling
Code:
Output:
Task 11:
Python JSON
Code:
Output:
Task 12:
Python Regular Expressions
Code:
Output:
Exercise Questions:
Basic Problems (5 Questions)
Q 1:
1. Array Rotation Write a Python program to rotate an array by a given number of
steps. o Input: [1, 2, 3, 4, 5], Steps: 2 o Output: [4, 5, 1, 2, 3]
Q 2:
Encapsulation with Getters and Setters Create a class BankAccount with private
attributes account_number and balance. Use getters and setters to update and
retrieve these values.
Input: account_number=12345, balance=10000 o
Output: Account Number: 12345, Balance: 10000
Code:
Output:
Q 3:
Date Difference Write a program to calculate the number of days between two given
dates. Input: Date 1: 2025-01-01, Date 2: 2025-01-15
Output: Difference: 14 days
Code:
Output:
Q 4:
Basic JSON Handling Write a program to create a JSON object with keys name, age,
and grade. Then read and print the values.
Input: { "name": "Alice", "age": 20, "grade": "A" }
Output: Name: Alice Age: 20 Grade: A
Code:
Output:
Q 5:
Using Python Math Library Create a program to find the greatest common divisor
(GCD) and least common multiple (LCM) of two numbers using the math module.
Input: 12, 15
Output: makefile CopyEdit GCD: 3 LCM: 60
Code:
Output:
Q 6:
File Handling: Find Longest Word Write a program to read a text file and find the
longest word in the file.
Input File Content: "The quick brown fox jumps over the lazy dog"
Output: Longest Word: jumps
Code:
Output:
Q 7:
Inheritance: Employee and Manager Classes Create a base class Employee with
attributes name and salary. Create a derived class Manager with an additional
attribute department. Write methods to display their details.
Input: name=John, salary=50000, department=HR
Output: makefile CopyEdit Name: John Salary: 50000 Department: HR
Code:
Output:
Q 8:
Regex Validation for Email Write a program to validate whether an input string is a
valid email address using re (Regex).
Input: [email protected]
Output: Valid Email
Code:
Output:
Q 9:
Python String Formatting Write a program to generate a formatted invoice using
string formatting.
Input: Item=Pen, Price=5, Quantity=10
Output: makefile CopyEdit Item: Pen Price: Rs. 5 Quantity: 10 Total: Rs. 50
Code:
Output:
Q 10:
User Input and PIP Library Usage Write a program to install a Python package using
pip and ask the user for the package name at runtime.
Input: Package name: numpy
Output: CopyEdit Installing numpy... Installation successful.
Code:
Output:
Q 11:
User Input and PIP Library Usage Write a program to install a Python package using
pip and ask the user for the package name at runtime.
Input: Package name: numpy
Output: CopyEdit Installing numpy... Installation successful.
Code:
Output:
Q 12:
Abstract Class for Payment Processing Create an abstract class Payment with an
abstract method process_payment(). Implement subclasses CreditCardPayment and
PaypalPayment with their respective implementations.
Input: Payment Type: Credit Card, Amount: 1000
Output: yaml CopyEdit Processing Credit Card Payment of Rs. 1000
Code:
Output:
Q 13:
JSON Data Analysis Write a program to load a JSON file containing student data
(name, marks) and calculate the average marks of all students.
Input JSON: json CopyEdit [ {"name": "Alice", "marks": 85}, {"name": "Bob",
"marks": 90}, {"name": "Charlie", "marks": 78} ]
Output: Average Marks: 84.33
Code:
Output:
Q 14:
Regex Search in Log File Write a program to extract all IP addresses from a given
server log file using regex.
Input File Content: csharp CopyEdit 192.168.1.1 - Accessed on 2025-01-19 10.0.0.2
- Accessed on 2025-01-20
Output: yaml CopyEdit IP Addresses: 192.168.1.1, 10.0.0.2
Code:
Output:
Q 15:
File Handling: Merge and Sort Files Write a program to merge two text files and sort
the combined content alphabetically. o Input File 1: "apple, banana, orange"
Input File 2: "cherry, fig, grape"
Output File: "apple, banana, cherry, fig, grape, orange"
Code:
Output: