0% found this document useful (0 votes)
18 views2 pages

Solution For Practical Exam

Bio

Uploaded by

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

Solution For Practical Exam

Bio

Uploaded by

Priyanshu Pal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 1: function

def factorial(n):
if (n==1 or n==0):
return 1
else:
return (n * factorial(n - 1))

num=int(input("Enter number for factorial"))


print("Factorial of ", num,"is :", factorial(num))

Question 2: file handling


my=open(r'abc.txt', 'r')
a=input("enter the string to be counted")
count=0
str1=my.read()
lst=str1.split()
for i in lst:
if i.lower()==a:
count=count+1
print("total occurrences of ", a, "are ", count)

Question 3 stack
def is_full(stk):
if len(stk) == 5:
return False
else:
return True

def is_Empty(stk):
if stk == [ ]:
return True
else:
return False

def push(stk, item) :


if is_full(stk):
stk. append(item)
top = len(stk)+1
else:
return "Overflow"

def pop(stk):
if is_Empty (stk) :
return "Underflow"
else:
item = stk.pop()
if len(stk) == 0:
top = None
else:
top = len(stk)-1
return item

def peek(stk) :
if is_Empty(stk) :
return "Underflow"
else:
top = len(stk) -1
return stk[top]

def Display(stk) :
if is_Empty(stk) :
print(":stack empty")
else:
top = len(stk) -1
print(stk[top], "<- top" )
for a in range(top- 1, -1, -1 ) :
print(stk [a])

#_main_
stack = [ ] # initially stack is empty
top = None
while True:
print("STACK OPERATIONS")
print("1. Push")
print("2. Pop")
print("3. Peek")
print("4. Display stack")
print("5. Exit")
ch = int(input("Enter your choice (1-5) :"))
if ch == 1 :
item = int(input("Enter item:"))
status=push (stack, item)
if status =="Overflow":
print("Stack is full, Cannot Push")
else:
print(item, " pushed in stack")
elif ch == 2 :
item = pop(stack)
if item =="Underflow":
print("underflow : stack is empty !")
else:
print("popped item is", item)
elif ch == 3:
item = peek(stack)
if item =="Underflow":
print("underflow! Stack is empty!")
else:
print("Topmost item is", item)
elif ch == 4:
Display(stack)
elif ch == 5:
break
else:
print("Invalid choice!")

Question 4: SQL interface


import mysql.connector as ms
db = ms.connect(host="localhost", user="root", passwd="computer",
database="EDUCATION")
dcursor=db.cursor( )
dcursor.execute("select * from employees where deptname='IT'")
result=dcursor.fetchall()
for i in result:
print(i)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy