CS Boards Pracs
CS Boards Pracs
'''
def bubble():
l=[1,6,-3,5,6,]
for i in range(len(l)-1):
for j in range(0,len(l)-1-i):
if l[j]>l[j+1]:
l[j],l[j+1]=l[j+1],l[j]
print(l)
bubble()
'''
#delete a data
'''
def del_data():
l=[1,2,3,4,5]
d=int(input("enter the data to delete"))
while d in l:
l.remove(d)
print(l)
del_data()
'''
def sum_rc():
for i in range(M):
s = 0
for j in range(N):
s += T[i][j]
print("The sum of row", i + 1, "is", s)
print()
for j in range(N):
s = 0
for i in range(M):
s += T[i][j]
print("The sum of column", j + 1, "is", s)
print()
def ld():
s = 0
for i in range(M):
for j in range(N):
if i == j:
s += T[i][j]
print("the sum of left diagnols is",s)
def rd():
s = 0
for i in range(M):
for j in range(N):
if i + j == M - 1:
s += T[i][j]
print("sum of right diagnols is",s)
print("\n boundary")
def boundary():
for i in range(M):
for j in range(N):
if i == 0 or i == M - 1 or j == 0 or j == N - 1:
print(T[i][j], end=" ")
else:
print(" ", end=" ")
print()
def upper():
print("\nupper triangle")
for i in range(M):
for j in range(N):
if i<=j:
print(T[i][j],end=" ")
else:
print(" ",end=" ")
print()
print("\nlower triangle")
def lower():
print("\nlower triangle")
for i in range(M):
for j in range(N):
if i>=j:
print(T[i][j],end="")
else:print(" ",end=" ")
print()
sum_rc()
ld()
rd()
boundary()
upper()
lower()
'''
#dict=total count update
'''
# Accepting employee records
employees = {}
for _ in range(5):
roll_no = int(input("Enter Roll Number: "))
name = input("Enter Name: ")
salary = float(input("Enter Salary: "))
employees[roll_no] = [name, salary]
def TOTAL():
total_salary = 0
for emp in employees.values():
total_salary += emp[1] # Adding salary manually
print("\nTotal Salary of all employees:", total_salary)
def Update():
for emp in employees.values():
emp[1] *= 1.10 # Directly updating salary using values()
print("\nSalaries updated successfully!")
def Count():
count = 0
for emp in employees.values():
if emp[1] > 10000: # Checking salary condition
count += 1
print("\nNumber of employees with salary above 10,000:", count)
# Function Calls
TOTAL()
Update()
TOTAL() # Check updated salary
Count()
'''
#frequency of streng
'''
def freq():
d = {} # Dictionary to store frequency of letters
for c in s: # Loop through each character in the sentence
if c.isalpha(): # Only consider alphabets
d.setdefault(c, 0) # If 'c' is not in d, set it to 0
d[c] += 1 # Increment count
print("\nLetter Frequency:")
for k, v in d.items(): # Print all letter frequencies
print(k, ":", v) # Printing without f-strings
def count():
num_words = 0
num_uppercase = 0
num_lowercase = 0
num_digits = 0
num_others = 0
def count_uppercase_vowel():
vowels = "AEIOU"
count = 0
def count_the():
c=0
for i in s.split():
if i=="the":c=+1
print("the number of the is",c)
def count_wc():
w=0;c=0
for i in s.split():
w+=1
for i in s:
c+=1
print(w,c)
print("File Contents:")
print(text)
print("Lines:", lines)
print("Words:", words)
print("Characters:", chars)
# Function b: Transfer Uppercase alphabets into a list and display the list
def transfer():
with open("BOARD.TXT", "r") as file:
text = file.read()
uppercase = []
for char in text:
if char.isupper():
uppercase.append(char)
# Function c: Count number of occurrences of the word "The" for whole file
def count_the():
with open("BOARD.TXT", "r") as file:
text = file.read()
the_count = text.lower().split().count("the")
print("The appears", the_count, "times.")
def count_the1():
with open("BOARD.TXT", "r") as file:
line_number = 1
line = file.readline()
while line:
the_count = line.split().count("the")
The_count = line.split().count("The")
print("In line", line_number, "'the' appears", the_count, "time(s).")
print("In line", line_number, "'The' appears", The_count, "time(s).")
line_number += 1 # Increment the line number after processing the
current line
line = file.readline()
def count1():
with open("BOARD.TXT", "r") as file:
text = file.read() # Read the entire content at once
uppercase = 0
lowercase = 0
digits = 0
others = 0
def transfer_1st_last():
print(l)
count()
transfer()
count_the()
count_the1()
count1()
transfer_1st_last()
'''
#binary files
'''
import pickle
import os
def create():
f = open("abc.dat", "wb")
name = input("Enter the name: ")
age = int(input("Enter the age: "))
salary = int(input("Enter the salary: "))
l = [name, age, salary]
pickle.dump(l, f)
f.close()
def display():
f = open("abc.dat", 'rb')
found = True
while found:
try:
data = pickle.load(f)
except EOFError:
found = False
else:
print("Name:", data[0])
print("Age:", data[1])
print("Salary:", data[2])
f.close()
def modify():
f = open("abc.dat", 'rb')
g = open("emp.dat", 'wb')
d = input("Enter the name of the person's record you want to modify: ")
found = True
while found:
try:
data = pickle.load(f)
except EOFError:
found = False
else:
if d == data[0]:
age = int(input("Enter the age: "))
salary = int(input("Enter the salary: "))
l = [data[0], age, salary]
pickle.dump(l, g)
else:
pickle.dump(data, g)
f.close()
g.close()
os.remove("abc.dat")
os.rename("emp.dat", "abc.dat")
def add_arecord():
f = open("abc.dat", 'ab')
n = int(input("Enter how many more records you want to enter: "))
for i in range(n):
name = input("Enter the name: ")
age = int(input("Enter the age: "))
salary = int(input("Enter the salary: "))
l = [name, age, salary]
pickle.dump(l, f)
f.close()
def update():
f = open("abc.dat", 'rb')
g = open("emp.dat", 'wb')
found = True
while found:
try:
data = pickle.load(f)
except EOFError:
found = False
else:
data[2] *= 1.10 # Assuming you want to increase salary by 1%
pickle.dump(data, g)
f.close()
g.close()
os.remove('abc.dat')
os.rename('emp.dat', 'abc.dat')
def insert():
f = open("abc.dat", 'rb')
g = open("emp.dat", 'wb')
d = input("Enter after which record to insert: ")
found = True
while found:
try:
data = pickle.load(f)
except EOFError:
found = False
else:
if data[0] == d:
name = input("Enter the name: ")
age = int(input("Enter the age: "))
salary = int(input("Enter the salary: "))
l = [name, age, salary]
pickle.dump(data, g)
pickle.dump(l, g)
else:
pickle.dump(data, g)
f.close()
g.close()
os.remove('abc.dat')
os.rename('emp.dat', 'abc.dat')
def menu():
ch=1
while True:
print('\n 1.create\n2.display\n3.modify\n4.add a record\n5.update\
n6.insert')
ch=int(input("choose a number"))
if ch>=2 and ch<=6:
try:f=open("abc.dat",'rb')
except FileNotFoundError:print("FIRST choose OPTION 1")
else:
f.close()
if ch==2:display()
elif ch==3:modify()
elif ch==4:add_arecord()
elif ch==5:update()
elif ch==6:insert()
elif ch==1:create()
menu()
'''
#csv file
'''
import csv
def create():
f=open('book.csv','w',newline='')
csvobj=csv.writer(f,delimiter=',')
for i in range(2):
bc=int(input("enter the book code"))
bn=input("enter book name")
bs=int(input("enter the book cost"))
l=[bc,bn,bs]
csvobj.writerow(l)
f.close()
def count():
f=open('book.csv','r',newline='')
data =csv.reader(f)
c=0
for i in data:
if float(i[2]) <100:c+=1
f.close()
print(c)
def transfer():
f=open('book.csv','r',newline='');l=[]
data=csv.reader(f)
for i in data:
if float(i[2])<100:
l.append(i)
f.close()
print(l)
create()
count()
transfer()
'''
#transfer from the list_csv
'''
import csv
for r in d:
w.writerow(r)
def d():
with open('ABC.CSV', 'r') as f:
r = csv.reader(f)
for row in r :
print(row)
if choice == 1:
t()
elif choice == 2:
d()
elif choice == 3:
c()
elif choice == 4:
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
m()
'''
#mysql
'''
import mysql.connector
pyobj = sqlobj.cursor()
# Create a table
pyobj.execute("CREATE TABLE exam (roll_no INT PRIMARY KEY, name VARCHAR(15), age
INT)")
def is_empty():
if ST == []:
return True
else:
return False
def pop(ST):
if is_empty() == True:
print("Underflow error")
else:
print("Data popped", ST.pop())
def display(ST):
if is_empty() == True:
print("No data")
else:
print("Stack contents:", ST[::-1])
def Pop_all(ST):
if is_empty() == True:
print("Underflow error")
else:
i = 0
while ST:
print("Data popped", ST.pop())
print("Empty stack")
def peek(ST):
if is_empty() == True:
print("No data")
else:
print("Peek data", ST[-1])
def Transfer():
L = [input("Enter string: ") for _ in range(5)]
for word in L:
if word[0].lower() in "aeiou":
stack(word)
ST = []
while True:
print("\nMenu:\n1. Transfer\n2. Pop\n3. Display\n4. Pop All\n5. Peek\n6. Exit")
choice = input("Enter choice: ")
if choice == "1":
Transfer()
elif choice == "2":
pop(ST)
elif choice == "3":
display(ST)
elif choice == "4":
Pop_all(ST)
elif choice == "5":
peek(ST)
elif choice == "6":
break
else:
print("Invalid choice")
'''
#tuples
'''
def Transfer_one(l):
N = len(l)
result = []
for i in range(N):
row = []
for j in range(N):
if j <= i:
row.append(l[j])
else:
row.append(0)
result.append(row)
def Transfer_two(l):
N = len(l)
result = []
for i in range(N):
row = []
for j in range(N):
if j >= N - i - 1:
row.append(l[j])
else:
row.append(0)
result.append(row)
l = [8, 2, 3, 4, 7]
Transfer_one(l)
print()
Transfer_two(l)
'''