Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Python Program To Implement A Stack

Uploaded by

Bishwajit Salam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Python Program To Implement A Stack

Uploaded by

Bishwajit Salam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

A Python program to implement a stack

class Stack:

def __init__(self):

self.items = []

def is_empty(self):

return len(self.items) == 0

def push(self, item):

self.items.append(item)

def pop(self):

if not self.is_empty():

return self.items.pop()

else:

print("Stack is empty. Cannot pop from an empty stack.")

def peek(self):

if not self.is_empty():

return self.items[-1]

else:

print("Stack is empty. Cannot peek into an empty stack.")

def size(self):

return len(self.items)

# Example Usage:

my_stack = Stack()

my_stack.push(1)

my_stack.push(2)

my_stack.push(3)

print("Current Stack:", my_stack.items)

print("Top Element:", my_stack.peek())


print("Stack Size:", my_stack.size())

popped_item = my_stack.pop()

print("Popped Item:", popped_item)

print("Updated Stack:", my_stack.items)

Output:

Current Stack: [1, 2, 3]

Top Element: 3

Stack Size: 3

Popped Item: 3

Updated Stack: [1, 2]


A python program to implement a queue

class Queue:

def __init__(self):

self.items = []

def is_empty(self):

return len(self.items) == 0

def enqueue(self, item):

self.items.append(item)

def dequeue(self):

if not self.is_empty():

return self.items.pop(0)

else:

print("Queue is empty. Cannot dequeue from an empty queue.")

def front(self):

if not self.is_empty():

return self.items[0]

else:

print("Queue is empty. Cannot get front of an empty queue.")

def size(self):

return len(self.items)

# Example Usage:

my_queue = Queue()

my_queue.enqueue(1)

my_queue.enqueue(2)

my_queue.enqueue(3)
print("Current Queue:", my_queue.items)

print("Front Element:", my_queue.front())

print("Queue Size:", my_queue.size())

dequeued_item = my_queue.dequeue()

print("Dequeued Item:", dequeued_item)

print("Updated Queue:", my_queue.items)

output:
A python program to implement a deque

class Deque:

def __init__(self):

self.items = []

def is_empty(self):

return len(self.items) == 0

def add_front(self, item):

self.items.insert(0, item)

def add_rear(self, item):

self.items.append(item)

def remove_front(self):

if not self.is_empty():

return self.items.pop(0)

else:

print("Deque is empty. Cannot remove from the front of an empty deque.")

def remove_rear(self):

if not self.is_empty():

return self.items.pop()

else:

print("Deque is empty. Cannot remove from the rear of an empty deque.")

def front(self):

if not self.is_empty():

return self.items[0]

else:

print("Deque is empty. Cannot get front of an empty deque.")

def rear(self):

if not self.is_empty():

return self.items[-1]

else:

print("Deque is empty. Cannot get rear of an empty deque.")

def size(self):

return len(self.items)
# Example Usage:

my_deque = Deque()

my_deque.add_front(1)

my_deque.add_rear(2)

my_deque.add_rear(3)

print("Current Deque:", my_deque.items)

print("Front Element:", my_deque.front())

print("Rear Element:", my_deque.rear())

print("Deque Size:", my_deque.size())

removed_front = my_deque.remove_front()

print("Removed Front Element:", removed_front)

removed_rear = my_deque.remove_rear()

print("Removed Rear Element:", removed_rear)

print("Updated Deque:", my_deque.items)

Output:

Current Deque: [1, 2, 3]

Front Element: 1

Rear Element: 3

Deque Size: 3

Removed Front Element: 1

Removed Rear Element: 3

Updated Deque: [2]


python program to evaluate a postfix expression using a stack.

def evaluate_postfix(expression):

stack = []

def is_operand(char):

return char.isnumeric()

def apply_operator(operator, operand1, operand2):

if operator == '+':

return operand1 + operand2

elif operator == '-':

return operand1 - operand2

elif operator == '*':

return operand1 * operand2

elif operator == '/':

return operand1 / operand2

elif operator == '^':

return operand1 ** operand2

for char in expression:

if is_operand(char):

stack.append(int(char))

else:

operand2 = stack.pop()

operand1 = stack.pop()

result = apply_operator(char, operand1, operand2)

stack.append(result)

if len(stack) == 1:

return stack[0]

else:

print("Invalid postfix expression. More than one value left in the stack.")
# Example Usage:

postfix_expression = "235*+"

result = evaluate_postfix(postfix_expression)

print(f"Result of {postfix_expression} is: {result}")

Output:

Result of 235*+ is: 17


a python code to sort a list in ascending order using bubble sort

def bubble_sort(arr):

n = len(arr)

# Traverse through all array elements

for i in range(n):

# Last i elements are already sorted, so we don't need to check them

for j in range(0, n - i - 1):

# Swap if the element found is greater than the next element

if arr[j] > arr[j + 1]:

arr[j], arr[j + 1] = arr[j + 1], arr[j]

# Example Usage:

my_list = [64, 25, 12, 22, 11]

print("Original List:", my_list)

bubble_sort(my_list)

print("Sorted List:", my_list)

output:

Original List: [64, 25, 12, 22, 11]

Sorted List: [11, 12, 22, 25, 64]


a python program to open a file “test.txt” in write mode and write the following lines. open the same file in read
mode to display the contents.

“COUNCIL OF HIGHER SECONDARY EDUCATION MANIPUR”

# Open the file in write mode and write the line

with open("test.txt", "w") as file:

file.write("COUNCIL OF HIGHER SECONDARY EDUCATION MANIPUR")

# Open the file in read mode and display its contents

with open("test.txt", "r") as file:

contents = file.read()

print("Contents of 'test.txt':")

print(contents)

OUTPUT:

Contents of 'test.txt':

COUNCIL OF HIGHER SECONDARY EDUCATION MANIPUR


Consider the table STUDENT and write the SQL commands for the following questions based on it :

SL.no NAME STREAM STIPEND AGE SEX


1 RAKESH COMMERCE 2000 20 M
2 BEENA MEDICAL 2500 22 F
3 AMITA HUMANITIES 2200 21 F
4 AMARJIT SCIENCE 2400 23 M
5 KIRAN MEDICAL 2100 24 M
6 DIANA MEDICAL 2800 22 F
7 LOREN SCIENCE 2400 21 M
8 ROGER COMMERCE 2600 25 M
9 DENIAL HUMANITIES 2500 26 M
10 HENERITA MEDICAL 3000 25 F

(a) To display all the information of male student


(b) Display NAME,STREAM and STIPEND of female students.
(c) List all the students whose name starts with “D”.
(d) List the names of the students having the stream commerce and science.
(e) Display the total stipend of male student.
(f) List all the students in descending order of their stipend.
(g) Count the number of students in MEDICAL stream.
(h) To insert a new row in the table with the values.
11,”JOHN”,”SCIENCE”,2700,24,”M”
(a)To display all the information of male students:
SELECT * FROM STUDENT WHERE SEX = 'M';
Output:
SL_no | NAME | STREAM | STIPEND | AGE | SEX
------+-----------+-------------+---------+-----+-----
1 | RAKESH | COMMERCE | 2000 | 20 | M
4 | AMARJIT | SCIENCE | 2400 | 23 | M
5 | KIRAN | MEDICAL | 2100 | 24 | M
7 | LOREN | SCIENCE | 2400 | 21 | M
8 | ROGER | COMMERCE | 2600 | 25 | M
9 | DENIAL | HUMANITIES | 2500 | 26 | M
11 | JOHN | SCIENCE | 2700 | 24 | M
(b) Display NAME, STREAM, and STIPEND of female students:
SELECT NAME, STREAM, STIPEND FROM STUDENT WHERE SEX = 'F';
Output: NAME | STREAM | STIPEND
-----------+-------------+---------
BEENA | MEDICAL | 2500
AMITA | HUMANITIES | 2200
DIANA | MEDICAL | 2800
HENERITA | MEDICAL | 3000
(c) List all the students whose name starts with “D”:
SELECT * FROM STUDENT WHERE NAME LIKE 'D%';

Output: SL_no | NAME | STREAM | STIPEND | AGE | SEX


------+-----------+-------------+---------+-----+-----
6 | DIANA | MEDICAL | 2800 | 22 | F
9 | DENIAL | HUMANITIES | 2500 | 26 | M
(d) List the names of the students having the stream commerce and
science:
SELECT NAME FROM STUDENT WHERE STREAM IN ('COMMERCE', 'SCIENCE');

Output: NAME
-----
RAKESH
AMARJIT
LOREN
ROGER
JOHN
(e)Display the total stipend of male students:
SELECT SUM(STIPEND) AS TotalStipend FROM STUDENT WHERE SEX = 'M';

output: TotalStipend
-------------
14700
(f) List all the students in descending order of their stipend:
SELECT * FROM STUDENT ORDER BY STIPEND DESC;

Output: SL_no | NAME | STREAM | STIPEND | AGE | SEX


------+-----------+-------------+---------+-----+-----
10 | HENERITA | MEDICAL | 3000 | 25 | F
6 | DIANA | MEDICAL | 2800 | 22 | F
8 | ROGER | COMMERCE | 2600 | 25 | M
11 | JOHN | SCIENCE | 2700 | 24 | M
2 | BEENA | MEDICAL | 2500 | 22 | F
9 | DENIAL | HUMANITIES | 2500 | 26 | M
4 | AMARJIT | SCIENCE | 2400 | 23 | M
7 | LOREN | SCIENCE | 2400 | 21 | M
3 | AMITA | HUMANITIES | 2200 | 21 | F
5 | KIRAN | MEDICAL | 2100 | 24 | M
1 | RAKESH | COMMERCE | 2000 | 20 | M
(g) Count the number of students in MEDICAL stream:
SELECT COUNT(*) AS MedicalStudentsCount FROM STUDENT WHERE STREAM = 'MEDICAL';

Output:
MedicalStudentsCount
---------------------
4

(h) To insert a new row in the table with the values. 11, "JOHN", "SCIENCE",
2700, 24, "M":
INSERT INTO STUDENT (SL_no, NAME, STREAM, STIPEND, AGE, SEX) VALUES (11, 'JOHN', 'SCIENCE', 2700, 24,
'M');

You might also like