Prac12 2324
Prac12 2324
Prac12 2324
def factorial(n):
if n==1:
return n
else:
num=int(input("Enter Number"))
if num<0:
elif num==0:
print("Factorial of 0 is 1")
else:
output;
Enter Number4
Factorial of 4 is 24
Prog 2;
def findSum(list2):
if len(list2)==1:
return list2[0]
else:
return list2[0]+findSum(list2[1:])
list1 =[]
list1 = [1,2,3,4,5]
findSum(list1)
output;
def fibonacci(n):
if n==1:
elif n==2:
else:
if num<=0:
else:
print("Fibonacci sequence:")
for x in range(1,num+1):
print(fibonacci(x),"",end= "")
print(".........")
output;
Fibonacci sequence:
0 1 1 2 3 .........
>>>
Pgm4:
# Python code to
# demonstrate readlines()
L = ["India is My country\n", " All indians are my brothers and sisiters\n", "we love sainik school\n"]
# writing to file
file1.writelines(L)
file1.close()
# Using readlines()
Lines = file1.readlines()
count = 0
output:
>>>
Pgm5:
#Main=open("Eemail.txt")
Main=open("new.txt")
Readlt=Main.read()
NVowels=0
NConsonants=0
NUpperCase=0
NLowerCase=0
for C in Readlt:
if C.isupper():
NUpperCase=NUpperCase+1
if C.islower():
NLowerCase= NLowerCase+1
NVowels=NVowels+1
NConsonants=NConsonants+1
output:
>>>
Pgm6:
import pickle
data = []
for i in range(number_of_data):
data.append(raw)
pickle.dump(data, file)
file.close()
data = pickle.load(file)
# close the file
file.close()
cnt = 0
cnt += 1
output;
Enter data 0 : 5
Enter data 1 : 4
Enter data 2 : 6
Enter data 3 : 9
Enter data 4 : 8
The data 0 is : 5
The data 1 is : 4
The data 2 is : 6
The data 3 is : 9
The data 4 is : 8
>>>
Prog 7:
def isEmpty(stk):
if stk==[ ]:
return True
else :
return False
def Push(stk,item):
stk.append(item)
top=len(stk)-1
def Pop(stk):
if isEmpty(stk):
return "Underflow"
else :
item=stk.pop()
top=len(stk)-1
return item
def Peek(stk):
if isEmpty(stk):
return "Underflow"
else :
top=len(stk)-1
return stk[top]
def Display(stk):
if isEmpty(stk):
top=len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
Stack=[ ]
top=None
while True :
print("STACK OPERATIONS")
print("1.Push")
print("2.Pop")
print("3.Peek")
print("4.Display")
print("5.Exit")
if ch==1:
Push(Stack,item)
elif ch==2:
item=Pop(Stack)
if item=="Underflow":
else :
elif ch==3:
item=Peek(Stack)
if item=="Underflow":
print("Underflow! Stack is empty!")
else :
elif ch==4:
Display(Stack)
elif ch==5:
break
else :
output:
STACK OPERATIONS
1.Push
2.Pop
3.Peek
4.Display
5.Exit
Stack Empty
Pgm8:
myfile = open("WithoutA.txt" )
Str=" "
Str2=" "
Main=myfile.read()
SplitLineMain = (Main.splitlines())
for x in SplitLineMain:
if " a" in x:
else:
myfile.close()
wmyfile=open("WithoutA.txt","w")
myfile2.write(Str)
myfile2.write("\n")
myfile2.close()
wmyfile.write(Str2)
wmyfile.write("\n")
wmyfile.close()
output:
>>>
Pgm9:
# import random
import random
num = random.random()
s=int(input("start value"))
e=int(input("end value"))
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
string = "Ajinkyan"
print(random.choice(string))
output:
start value455
end value622
A
Pgm10: (1)
num=int(input("Enter a number:"))
if num >= 1:
# Iterate from 2 to n / 2
if (num % i) == 0:
break
else:
else:
output:
Enter a number:45
if stk==[]:
return True
else:
return False
stk.append(item)
top=len(stk)-1
def Pop(stk):
print("Underflow")
item=stk.pop()
if len(stk)==0:
top=None
else:
top=len(stk)
def Disply(stk):
if isEmpty(stk):
print("stack is empty")
else:
top=len(stk)-1
for i in range(top,-1,-1):
print(str(stk[i]))
#executable code
#if _name_=="_main_":
stk=[]
top=None
Push(stk,1)
Disply(stk)
Push(stk,2)
Disply(stk)
Disply(stk)
Push(stk,3)
Disply(stk)
Push(stk,4)
Disply(stk)
Push(stk,100)
Disply(stk)
Push(stk,-77)
Disply(stk)
Pop(stk)
Pop(stk)
Pop(stk)
Disply(stk)
Output:
100
-77
100
1
Popped item is-77
1
Pgm 11:
r=float(input("Enter the Rate of interest you want:")) #All inputs are Accepted
Cl=p*x-p
output:
>>>
Pgm 12:
def isStringPalindrome(str):
if len(str)<=1:
return True
else:
if str[0]==str[-1]:
return isStringPalindrome(str[1:-1])
else:
return False
#_main_
y=isStringPalindrome(s)
if y==True:
else:
output:
fin=open("new.txt",'r')
str=fin.read()
L=str.split()
count_words=0
for i in L:
count_words=count_words+1
output:
>>>
Pgm14:
"""
class Stack:
def __init__(self):
self.items = []
self.length = 0
self.items.append(val)
self.length += 1
def pop(self):
if self.empty():
return None
self.length -= 1
return self.items.pop()
def size(self):
return self.length
def peek(self):
if self.empty():
return None
return self.items[0]
def empty(self):
return self.length == 0
def __str__(self):
return str(self.items)
precedence = {}
precedence['*'] = 3
precedence['/'] = 3
precedence['+'] = 2
precedence['-'] = 2
precedence['('] = 1
def convert(expression):
print(__convert(expression.split()))
def __convert(tokens):
postfix = []
opstack = Stack()
if token.isidentifier():
postfix.append(token)
opstack.push(token)
elif token == ')':
while True:
temp = opstack.pop()
break
postfix.append(temp)
if not opstack.empty():
temp = opstack.peek()
postfix.append(opstack.pop())
temp = opstack.peek()
opstack.push(token)
postfix.append(opstack.pop())
return postfix
convert("A + B")
print("POSTFIX OF = A + B * C IS")
convert("A + B * C")
print("POSTFIX OF = A * ( B + C ) + D IS")
convert("A * ( B + C ) + D")
output:
POSTFIX OF = A+B IS
POSTFIX OF = A + B * C IS
POSTFIX OF = A * ( B + C ) + D IS
>>>
Pgm15:
class Evaluate:
self.top = -1
self.capacity = capacity
self.array = []
def isEmpty(self):
def peek(self):
return self.array[-1]
def pop(self):
if not self.isEmpty():
self.top -= 1
return self.array.pop()
else:
return "$"
self.top += 1
self.array.append(op)
# to postfix expression
for i in exp:
if i.isdigit():
self.push(i)
else:
val1 = self.pop()
val2 = self.pop()
self.push(str(eval(val2 + i + val1)))
return int(self.pop())
#exp = "231*+9-"
exp="17+"
obj = Evaluate(len(exp))
output:
postfix evaluation: 8
>>>
Pgm16:
import csv
fh=open("student199.csv",'w')
stuwriter=csv.writer(fh)
stuwriter.writerow(['Rollno','Name','Marks'])
for i in range(2):
print("stu rec",(i+1))
name=input("tell name")
sturec=[rollno,name,marks]
stuwriter.writerow(sturec)
fh.close()
output:
stu rec 1
tell nametejas
tell marks98
stu rec 2
tell nameguru
tell marks98
>>>