Professional Documents
Culture Documents
STACK IMPLEMENTATION
AIM: To implement the stack operation using a list data structure
ALGORITHM:
Step 1: Start the program execution Step 2: Read the value of choice
Step 3: Check if choice==1, then call the function view( ) – To view the
element in stack , else go to step 4
Step 4: Check if choice==2, then call the function push( ) – To insert an
element into stack, else go to step 5
Step 5: Check if choice==3, then call the function pop( ) – To delete an
element from stack, else go to step 6
Step 6: Check if choice==4, then call the function peek( ) – To display top
element of stack, else go to step 7
Step 7: Print “Wrong choice”
Step 8: Stop the program execution
Expected Input:
Push (Choice:2) Data →11,22,33,44,55
Peek ( Choice : 4)
Pop (Choice: 3)
View (Choice: 1)
Expected Output:
Data Pushed
Peeked: 55
Popped: 55
Traversed:
44
33
22
11
Code:
stack=[ ]
def c_stkMT():
if len(stack) == 0:
return True
else:
return False
def view( ):
if c_stkMT():
print("Stack empty.. ")
else:
for x in range(len(stack)-1,-1,-1):
print(stack[x])
def push( ):
item=int(input("Enter integer value"))
stack.append(item)
def pop( ):
if c_stkMT():
print("Stack empty")
else:
item=stack.pop(-1)
print("Deleted element:",item)
def peek( ):
if c_stkMT():
print("Stack is empty.. ")
else:
item=stack[-1]
print("Peeked element:",item)
print("Stack operation")
print("************")
print("1.View")
print("2.Push")
print("3.Pop")
print("4.Peek")
print("5.Quit")
while True:
choice=int(input("Enter your choice: "))
if choice ==1:
view( )
elif choice == 2:
push( )
elif choice == 3:
pop( )
elif choice == 4:
peek( )
elif choice == 5:
break
else:
print("Wrong choice")
OUTPUTS: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL
ALONG WITH THE BOX)
Stack operation
************
1.View
2.Push
3.Pop
4.Peek
5.Quit
Enter your choice: 2
Enter integer value11
Expected input
B = {"AI 2041: Ten Visions for Our Future":7.9,
"Beginners: The Transformative Joy of Lifelong Learning":8.5,
"Bravey: Chasing Dreams, Befriending Pain, and Other Big Ideas":9,
"Chatter: The Voice in Our Head, Why It Matters, and How to Harness It":8.2,
"The Code Breaker: Jennifer Doudna, Gene Editing, and the Future of the Human
Race":7.5 }
Expected Output
Beginners: The Transformative Joy of Lifelong Learning,
Bravey: Chasing Dreams, Befriending Pain, and Other Big Ideas,
Chatter: The Voice in Our Head, Why It Matters, and How to Harness It
Code:
libBookDic = {"AI 2041: Ten Visions for Our Future":7.9,
"Beginners: The Transformative Joy of Lifelong Learning":8.5,
"Bravey: Chasing Dreams, Befriending Pain, and Other Big Ideas":9,
"Chatter: The Voice in Our Head, Why It Matters, and How to Harness It":8.2,
"The Code Breaker: Jennifer Doudna, Gene Editing, and the Future of the
Human Race":7.5}
stkLst = []
top = -1
def isEmpty(top):
if top==-1:
return True
else:
return False
def PushBooks(bookDic):
global stkLst, top
for i in libBookDic:
if libBookDic[i] > 8:
stkLst.append(i)
top+=1
print("Elements are pushed")
def PopBooks():
global stkLst, top
if isEmpty(top):
print("Stack Empty")
else:
print("Poped Book is ", stkLst.pop())
top-=1
def ViewBook():
global stkLst, top
if isEmpty(top):
print("Stack Empty")
else:
print ("Top element -->",end=' ')
for ind in range(top,-1,-1):
print(stkLst[ind])
while True:
print("Book Stack Operation")
print("1. Add Books \n 2. View Books in stack \n 3. Remove a book \n 4. Exit")
ch = int(input("Enter your choice "))
if ch == 1:
PushBooks(libBookDic)
libBookDic.clear()
elif ch == 2:
ViewBook()
elif ch == 3:
PopBooks()
elif ch == 4:
break
else:
print("Enter a valid choice...")
Result:
The database and the tables were created.
Output: (Write on the left side; parallel to aim)
create database sports;
c) Inserting data:
insert into team
values(1,'Tehlka'),
values(2,’Toofan'),
values(1,’Aandhi’),
values(1,’Shailab’);
d) Show the content of table - team:
select * from team;
Result:
d) select distinct(teamname)
from match_details, team
where match_details.firstteamid = team.teamid;
AIM: To create two tables for item and traders and execute the given commands using
SQL.
TABLE:ITEM
Code IName Qty Price Company TCode
1001 DIGITAL PAD 121 120 1100 XENTIA T01
0
1006 LED SCREEN 40 70 3800 SANTORA T02
0
1004 CAR GPS SYSTEM 50 2150 GEOKNOW T01
1003 DIGITAL CAMERA 12X 160 8000 DIGICLICK T02
TABLE:TRADERS
i) To display the details of all the items in ascending order of item names (i.e IName)
ii) To display item name and price of all those items, whose price is in the
range of 10000 and 22000 (both values inclusive)
iii) To display the number of items , which are traded by each trader. The
expected output of this query should be
T01 2
T02 2
T03 1
iv) To display the Price , item name(i.e IName) and quantity(i.e Qty) of those
items which have quantity more than 150.
v) To display the names of those traders, who are either from DELHI or
from MUMBAI.
Qty int ,
Price int ,
Company char(25),
TCode char(5));
TCode char(5) ,
TName char(25),
City char(20));
ii) select IName , Price from ITEM where Price between 10000 and 22000;
IName Price
DIGITAL PAD 121 11000
Tcode Count(*)
T01 2
T02 2
T03 1
TName
ELECTRONICS SALES
BUSY STORE CORP
21. DOCTOR AND SALARY (Aggregate Functions)
AIM: To create two tables for doctor and salary and execute the given
commands using SQL.
TABLE: DOCTOR
TABLE: SALARY
i. Display NAME of all doctors who are in “MEDICINE” having more than 10
years experience from table DOCTOR
ii. Display the average salary of all doctors working in “ENT” department using the
tables DOCTOR and SALARY. (Salary=BASIC+ALLOWANCE)
iii. Display minimum ALLOWANCE of female doctors.
iv. Display DOCTOR.ID , NAME from the table DOCTOR and BASIC ,
ALLOWANCE from the table SALARY with their corresponding matching ID.
v. To display distinct department from the table doctor.
NAME
Bill
ii) select avg(BASIC+ALLOWANCE) "avg salary" from DOCTOR , SALARY where DOCTOR.ID =
SALARY.ID and DEPT="ENT";
Avg salary
13000.00
iii) select min(ALLOWANCE) from SALARY, DOCTOR where SEX='F' and DOCTOR.ID =
SALARY.ID;
min(ALLOWANCE)
1700
iv) select DOCTOR.ID, NAME, BASIC ,ALLOWANCE from DOCTOR, SALARY where
DOCTOR.ID = SALARY.ID;
DEPT
ENT
ORTHOPEDIC
CARDIOLOGY
SKIN
MEDICINE
22. INTEGRATE PYTHON WITH SQL - FETCHING RECORDS FROM TABLE
AIM: To integrate SQL with Python by importing the MySQL module and extracting data
from result set
PROGRAM:
import mysql.connector as sqltor
mycon=sqltor.connect(host=”localhost”, user=”root”, passwd=”root”,databse=”trinity”)
if mycon.is_connected( ) = = False:
print(“Error connecting to MySQL database”)
cursor=mycon.cursor( )
cursor.execute(“select * from student”)
data=cursor.fetchmany(3)
for row in data:
print(row)
mycon.close( )
OUTPUTS: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL
ALONG WITHIN THE BOX)
AIM: To integrate SQL with Python by importing the MySQL module and extracting data
from result set
PROGRAM:
if mycon.is_connected( ) = = False:
print(“Error connecting to MySQL database”)
cur=mycon.cursor( )
cur.execute(“select * from student”)
data=cur.fetchone( )
count=cur.rowcount
print(“Total number of rows retrieved from resultset :”, count)
data=cur.fetchone( )
count=cur.rowcount
data=cur.fetchmany(3)
count = cur.rowcount
OUTPUTS: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL
ALONG WITHIN THE BOX)
Total number of rows retrieved from resultset : 1
AIM: Integrate SQL with Python by importing the MySQL module to search an employee
using eno , if it is present in table display the record
PROGRAM:
import mysql.connector as mc
mycon=mc.connect(host='localhost',user='root',passwd='root1',data base='db12')
if mycon.is_connected( ):
print("Py->Sql connected")
eno=int(input("Enter num:"))
mcursor=mycon.cursor( )
allrow=mcursor.fetchall( )
if row[0]==eno:
print(row)
mycon.close( )
RESULT:
Thus the given program executed successfully.
OUTPUTS: (SHOULD BE WRITTEN IN LEFT HAND SIDE OF THE PROGRAM USING PENCIL
ALONG WITHIN THE BOX)
Py-> sql is connected
AIM: To integrate SQL with Python by importing the MySQL module to search a student using
rollno and delete the record.
PROGRAM :
import mysql.connector as mc
mycon=mc.connect(host='localhost',user='root',passwd='root1',database='db12')
if mycon.is_connected():
print("Py->Sql connected")
eno=int(input("Enter num:"))
mcursor=mycon.cursor()
allrow=mcursor.fetchall()
if row[0]==eno:
print(mcursor.fetchall())
mycon.commit()
mycon.close()
RESULT:
Py -> sql is
connected
Enter num
: 102
(101,’Anu’,
23,’Salem’)
(103,’Cinu’,43,’Namakkal’)
(104, ‘Nishanth’, 46,’Chennai’)
(105, ‘Nanda’, 56, ‘Erode’)