0% found this document useful (0 votes)
41 views6 pages

Computer Science Practical File

Uploaded by

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

Computer Science Practical File

Uploaded by

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

NAME- SUBHADIP RAY

CLASS -11
SUBJECT- COMPUTER SCIENCE
PRACTICAL FILE
1] Implement stack(Push, Pop and Display)
Ans.
stk=[]
top =-1

def isEmpty(stk):
if stk==[]:
return True
else:
return False
def Push_val(stk,val):
stk.append(val)
top=len(stk)-1
disp(stk)
def Pop_val(stk):
if isEmpty(stk):
print("Under flow")
else:
el= stk.pop()
top=len(stk)-1
disp(stk)
return el
def disp(stk):
for i in range (len(stk)-1,-1,-1):
print(stk[i],end="->")
print()
Push_val(stk,12)
Push_val(stk,22)
Push_val(stk,23)
Pop_val(stk)
Pop_val(stk)

2]Bubble Sort
def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]

array = [64, 34, 25, 12, 22, 11, 90]


print("Original array:", array)
bubble_sort(array)
print("Sorted array:", array)

3]Insertion Sort

def insertion_sort(arr):
# Traverse through 1 to len(arr)
for i in range(1, len(arr)):
key = arr[i]

# Move elements of arr[0..i-1] that are greater than key,


# to one position ahead of their current position
j=i-1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j +1] = key

# Example usage:
array = [12, 11, 13, 5, 6]
print("Original array:", array)
insertion_sort(array)
print("Sorted array:", array)

4] Create a phonebook using dictionary

class Phonebook:
def __init__(self):
self.contacts = {}

def add_contact(self, name, number):


self.contacts[name] = number
print(f"Added {name} to the phonebook with number {number}")

def remove_contact(self, name):


if name in self.contacts:
del self.contacts[name]
print(f"Removed {name} from the phonebook")
else:
print(f"{name} not found in the phonebook")
def search_contact(self, name):
if name in self.contacts:
print(f"Contact found - {name}: {self.contacts[name]}")
else:
print(f"{name} not found in the phonebook")

def display_contacts(self):
if self.contacts:
print("Phonebook contacts:")
for name, number in self.contacts.items():
print(f"{name}: {number}")
else:
print("Phonebook is empty")

# Example usage:
phonebook = Phonebook()

phonebook.add_contact("Alice", "1234567890")
phonebook.add_contact("Bob", "9876543210")
phonebook.add_contact("Charlie", "1112223334")

phonebook.display_contacts()

phonebook.remove_contact("Bob")
phonebook.display_contacts()

phonebook.search_contact("Alice")
phonebook.search_contact("David")
5] Dice Simulation

import random

def roll_dice():
return random.randint(1, 6)

# Simulate a single dice roll


result = roll_dice()
print(f"The dice rolled: {result}")

import random

def roll_dice(sides=6):
return random.randint(1, sides)

# Simulate ten dice rolls


for _ in range(10):
result = roll_dice()
print(f"The dice rolled: {result}")

You might also like