cs python programming file
cs python programming file
cs python programming file
12
Write a Python to display unique vowels and the total number of
unique vowels present in the given word using Stack.
Source Code:
vowels=[‘a’,’e’,’i’,’o’,’u’]
Stack=[]
if letter in vowels:
Stack.append(letter)
print(Stack)
Output:
Enter the word to search for vowels: Computer Science
[‘o’,’u’,’e’,’i’]
1|Page
Program no.06
WAP to remove all the lines that contain the character ‘a’ in a file and
write it to another file.
Source Code:
input_file=open(‘Sample.txt’,’r’)
output_file=open(‘dataoutput.txt’,’w’)
lines=input_file.readlines()
for i in lines:
if ‘a’ not in i:
output_file.write(i)
input_file.close()
output_file.close()
Output:
list of following mysql queries
2|Page
Program no.13
WAP to count the number o-f upper case alphabets, vowels and
consonants present in a text file and display the same.
Source Code:
def cnt():
f=open(“Sample.txt”,”r”)
rec=f.read()
print(rec)
v=0
cons=0
lwr=0
upr=0
for ch in rec:
if (ch.islower()):
lwr+=1
elif(ch.isupper()):
upr+=1
ch=ch.lower()
if(ch in[‘a’,’e’,’i):
3|Page
v+=1
else:
cons+=1
f.close()
print(“Vowels are:”,v)
print(“Consonants are:”,cons)
cnt()
Output:
Twinkle Twinkle little stars
Vowels are: 16
Consonants are: 38
4|Page
Program no.11
Write a menu-driven program in Python to achieve the following
objective using user-defined functions:
Given string is: ‘apple orange mango apple orange mango apple guava
mango mango’
Source Code:
def listDistinctWords(str):
str=str.split()
L=[]
if word not in L:
L.append(word)
return L
def revStr(S):
rev=’’
5|Page
for ch in S:
rev=ch+rev
return rev
def freq(str):
L=listDistinctWords(str)
print(L)
freq_values=[]
for i in range(0,len(L)):
c=str.count(L[i])
freq_values.append(c)
print(‘Frequency of :’,L[i],’is’,c)
print()
while True:
print(‘MAIN MENU’)
print(‘2. For reverse each distinct word that appears in the given
string’)
print(‘3. For counting the frequency of each word in the given string
’)
6|Page
print()
if choice==1:
L1=listdistinctWords(str)
print()
elif choice==2:
L=listDistinctWords(str)
print(‘Reversed Words:’)
for word in L:
print(revStr(word),end=’’)
print()
elif choice==3:
freq(str)
else:
break
Output:
MAIN MENU
7|Page
2. For reversing each distinct word in the given string
[‘apple’,’orange’,’mango’,’guava’]
MAIN MENU
Reversed Words:
elppa
egnaro
ognam
avaug
MAIN MENU
[‘apple’,’orange’,’mango’,’guava’]
Frequency of:guava is 1
MAIN MENU
9|Page
Program no.05
WAP to generate random numbers between 1 to 6.
Source Code:
import random
Guess=True
while Guess:
n=random.randint(1,6)
if userinput==n:
else:
if val in[‘y’,’Y’]:
Guess=True
else:
Guess=False
Output:
10 | P a g e
Enter a number between 1 to 6: 5
11 | P a g e
Program no.07
WAP to print Fibonacci Series using function.
Source Code:
def Fibonacci(n):
firstterm = -1
secondterm = 1
print(“***Fibonacci Series***”)
for i in range(n):
print(thirdterm,end=””)
firstterm = secondterm
secondterm = thirdterm
Fibonacci(n)
Output:
Enter the number of terms you want to print: 10
***Fibonacci Series***
0 1 1 2 3 5 8 13 21 34
12 | P a g e
Program no. 04
WAP to check whether a given string is a palindrome string or not.
Source Code:
def Palindrome(myLength):
for i in range(0,myLength//2):
if myString[i]!=myString[myLength-i-1]:
break
else:
continue
else:
print(“Palindrome Check”)
myLength = len(myString)
Palindrome(myLength)
Output:
Palindrome Check
13 | P a g e
The given string Malayalam is a Palindrome string
Palindrome Check
14 | P a g e
Program no.08
WAP to create and call a function INDEX_LIST(l), where L is the list of
elements passed as argument to the function. The function returns
another list named ‘indexList’ that stores the indices of all Non-Zero
Elements of L.
Source Code:
def INDEX_LIST(L):
indexList=[]
for i in range(len(L)):
if L[i]!=0:
indexList.append(i)
return indexList
L=[12,4,0,11,0,56]
print(INDEX_LIST(L))
Output:
L=[12,4,0,11,0,56]
[0,1,3,5]
15 | P a g e
Program no.03
WAP to create a Stack for storing only odd numbers out of all the
numbers entered by the user. Display the content of stack along with
the largest odd number in the Stack.
Source Code:
n=int(input(“Enter the number of values:”))
stack=[]
for i in range(n):
num=int(input(“Enter number:”))
if num%2 != 0:
stack.append(num)
print(‘***STACK CONTENTS***:’,stack)
largestNum=stack.pop()
while(len(stack)>0):
num=stack.pop()
if num>largestNum:
largestNum=num
Output:
Enter the number of values: 5
16 | P a g e
Enter number: 11
Enter number: 12
Enter number: 23
Enter number: 34
Enter number: 45
***STACK CONTENTS***:[11,23,45]
17 | P a g e
Program no. 09
WAP to create and call a function PUSH(Arr), where Arr is a list of
numbers. From this list push all numbers divisible by into a stack
implemented using a list. Display the stack if it has at least one
element, otherwise display appropriate error message.
Source Code:
def PUSH(Arr):
stck=[]
for x in range(0,len(Arr)):
if Arr[x]%5==0:
stck.append(Arr[x])
if len(stck)==0:
print(‘STACK IS EMPTY’)
else:
Arr=[]
for i in range(n):
Arr.append(num)
18 | P a g e
PUSH(Arr)
Output:
Enter the number of elements you want: 3
STACK IS EMPTY
19 | P a g e
Program no.02
Julie has created a dictionary containing names and marks as key
value pairs of 6 students. Write a program, with separate user defined
functions to perform the following operations:
Source Code:
R=[“OM’:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82]
def PUSH(S,N):
S.append(N)
def POP(S):
if S != []:
return S.pop()
else:
return None
ST=[]
for k in R:
if R[k].=75:
PUSH(ST,k)
20 | P a g e
while True:
if ST != []:
print(POP(ST).end=””)
else:
break
Output:
R=[“OM’:76, “JAI”:45, “BOB”:89, “ALI”:65, “ANU”:90, “TOM”:82]
21 | P a g e
Program no. 10
Write a program to generate random numbers between 1 to 6 and
check whether a user won a lottery or not.
Source Code:
import random
n=random.randint(1,6)
if n==guess:
else:
Output:
Enter a number between 1 to 6 : 1
22 | P a g e
Program no. 01
Write a program for linear search.
Source Code:
L=eval(input(“Enter the elements:”))
n=len(L)
for i in range(n):
if L[i]==item:
break
else:
Output:
23 | P a g e
Enter the elements: 45, 47, 85, 41, 32, 56, 74, 65
24 | P a g e
25 | P a g e
26 | P a g e
27 | P a g e
28 | P a g e
29 | P a g e
30 | P a g e