0% found this document useful (0 votes)
33 views9 pages

NAME

This document contains 5 Python programs: 1) A CustomList class with methods to insert, delete, and display list elements. 2) A N Queens problem solver that places queens on a chessboard safely. 3) A Stack class with methods to check if empty, push, pop, peek, and size. 4) A program using a Queue to add elements and display size. 5) A function to insert an element into an array at a given index.

Uploaded by

Abdul rauf Khan
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)
33 views9 pages

NAME

This document contains 5 Python programs: 1) A CustomList class with methods to insert, delete, and display list elements. 2) A N Queens problem solver that places queens on a chessboard safely. 3) A Stack class with methods to check if empty, push, pop, peek, and size. 4) A program using a Queue to add elements and display size. 5) A function to insert an element into an array at a given index.

Uploaded by

Abdul rauf Khan
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
You are on page 1/ 9

NAME : RIDA NAYB

ROLL NO : 1041

DEPARTMENT : BS SOFTWARE ENGINEERING

SEMESTER : 3 (M)
rd

ASSIGNMENT : DSA

UNIVERSITY OF OKARA
PROGRAM NO # 01
Python program to append, insert, and element

class CustomList:
def _init_(self):
self.items = []

def insert(self, item):


self.items.append(item)
print(f"Inserted {item} into the list.")

def delete(self, item):


if item in self.items:
self.items.remove(item)
print(f"Deleted {item} from the list.")
else:
print(f"{item} not found in the list. Cannot delete.")

def display(self):
print("Current list elements:", self.items)

my_list = CustomList()
my_list.insert(10)
my_list.insert(20)
my_list.insert(30)

my_list.display()

my_list.delete(20)

my_list.display()

PROGRAM NO # 02

Python Quee programm

def is_safe(board, row, col, n):


for i in range(col):
if board[row][i] == 1:
return False
for i, j in zip(range(row, -1, -1), range(col, -1, -1)):
if board[i][j] == 1:
return False
for i, j in zip(range(row, n, 1), range(col, -1, -1)):
if board[i][j] == 1:
return False
return True
def solve_n_queens(n):
board = [[0 for _ in range(n)] for _ in range(n)]
if solve_n_queens_util(board, 0, n) is False:
print("Solution does not exist")
return
print_solution(board)

def solve_n_queens_util(board, col, n):


if col >= n:
return True
for i in range(n):
if is_safe(board, i, col, n):
board[i][col] = 1
if solve_n_queens_util(board, col + 1, n):
return True
board[i][col] = 0
return False

def print_solution(board):
for row in board:
print(" ".join(["Q" if cell == 1 else "." for cell in row]))

n = 8 # Change n to the desired board size


solve_n_queens(n)

PROGRAM NO # 03
Python program to find stack is is empty or not also find
the size of stack.

class Stack:
def _init_(self):
self.stack = []

def is_empty(self):
return len(self.stack) == 0

def push(self, item):


self.stack.append(item)

def pop(self):
if not self.is_empty():
return self.stack.pop()
else:
print("Stack is empty. Cannot pop an element.")

def peek(self):
if not self.is_empty():
return self.stack[-1]
else:
print("Stack is empty. No elements to peek.")

def size(self):
return len(self.stack)
my_stack = Stack()

my_stack.push(1)
my_stack.push(2)
my_stack.push(3)

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


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

popped_element = my_stack.pop()
print("Popped element:", popped_element)

print("Stack size after popping:", my_stack.size())

PROGRAM NO # 04
Python Quee program
import queue
q = queue.Queue()
for x in range(4):
q.put(x)
print("Members of the queue:")
y=z=q.qsize()

for n in list(q.queue):
print(n, end=" ")
print("\nSize of the queue:")
print(q.qsize())

PROGRAM NO # 05
Program to insert element in an arrays

def insert_item(arr, item, index):


if index < 0 or index > len(arr):
print("Invalid index. Item cannot be inserted.")
else:
arr.insert(index, item)
print(f"{item} has been inserted at index {index}.")
return arr
my_array = [1, 2, 3, 4, 5]
item_to_insert = 100
insert_position = 2

result_array = insert_item(my_array, item_to_insert, insert_position)


print("Updated Array:", result_array)

You might also like