Professional Documents
Culture Documents
23CS101T PSPP - Unit 3 & 4 Programs
23CS101T PSPP - Unit 3 & 4 Programs
Program:
a=input("Enter a string:")b=a.lower()
c=b[::-1]
if b==c:
print(a,"is a palindrome")else:
print(a,"is not a palindrome")
Output:
Enter a string: madam
Madam is a palindrome
A=[[1,2,3],[1,2,3],[1,2,3]]
B=[[2,3,1],[2,3,1],[2,3,1]]
C=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
C[i][j]+=A[i][k]*B[k][j]
for r in C:
print(r)
Output :
[12, 18, 6]
[12, 18, 6]
[12, 18, 6]
A=[[3,3,3],[2,2,2],[1,1,1]]
B=[[1,1,1],[2,2,2],[3,3,3]]
C=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(A)):
for j in range(len(A[0])):
C[i][j]=A[i][j]+B[i][j]
for r in C:
print(r)
Output :
[4, 4, 4]
[4, 4, 4]
[4, 4, 4]
A=[[1,2,3],[4,5,6]]
B=[[0,0],[0,0],[0,0]]
for i in range(len(A)):
for j in range(len(A[0])):
B[j][i] = A[i][j]
for r in B:
print(r)
Output :
[1, 4]
[2, 5]
[3, 6]
6. Write a function called is_anagram that takes two strings and returns True if they are anagrams.
def is_anagram(s1,s2):
s1=sorted(s1)
s2=sorted(s2)
if(s1==s2):
return True
else:
return False
a=input("enter 1st string:")
b=input("enter 2nd string:")
print("Is Anagram:",is_anagram(a.lower(),b.lower()))
Output:
7. Write a function deleteChar() which takes two parameters one is a string and other is a
character. The function should create a new string after deleting all occurrences of the character from
the string and return the new string.
def delchar(string,char):
str=" "
for i in string:
if i==char:
str=string.replace(i, '')
return str
a=input("enter string:")
b=input("enter char to be deleted:")
print("the string:", delchar(a, b))
Output :
enter string:abcdxyzd
enter char to be deleted:d
the string: abcxyz
8. Write a Python program to count Uppercase, Lowercase, special character and numeric
values in a given string.
v='Shop open@3pm'
s=v.replace(" ","")
a,b,c,d=0
for i in s:
if i.isdigit()==True:
a+=1
elif i.isupper()==True:
b+=1
elif i.islower()==True:
c+=1
else:
d+=1
print('digit values:',a)
print('uppercase letters:',b)
print('lower case letters:',c)
print('special char:',d)
Output:
digit values: 1
uppercase letters: 1
lower case letters: 9
special charc: 1
str=input("Enter string:")
str1="".join(str.split())
vowel="aeiou"
vcount=0
ccount=0
for i in str1:
if i.lower() in vowel:
vcount+=1
else:
ccount+=1
print("No. of vowels in string ",str ,vcount)
print("No. of consonents in string ",str ,ccount)
Output
Enter string:python program
No. of vowels in string python program 3
No. of consonents in string p ython program 10
1. Partition the list into sorted and unsorted sections. The sorted section is initially emptywhile the
unsorted section contains the entire list
2. Set the first element of the unsorted array as minimum element
3. Compare the minimum element with the second element of unsorted array. If thesecond
element is smaller than the first, we assign it as a minimum element.
4. Again compare the second element to the third and if the third element is smaller thansecond,
assign it as minimum. Repeat the process until the last element of unsorted array is reached. Thus
find the minimum value from unsorted array
5. Swap the minimum value with the first element of the unsorted array
6. Repeat steps 2 to 5 until we get the sorted array.
Program:
def selectionsort(A):
for i in range(len(A)): # Traverse all array elements min_idx = i
for j in range(i+1, len(A)):
if A[min_idx] > A[j]: # Find min
min_idx = j
A[i], A[min_idx] = A[min_idx], A[i] # Swap
A = [12, 10, 16, 11, 9, 7]
selectionsort(A)
print ("Sorted array", A)
Output:
Sorted array [7,9,10,11,12,16]
a=[42,33,19,55,16]
print(“Sorted array is: “, insertion_sort(a))
Output:
Sorted array is : [16,19,33,42,55]
1. Split the unsorted list until it contains single element in the sublist
2. Compare each of the element and group them as sorted
3. Repeat step 2 until whole list is merged and sorted
Program:
def mergeSort(nlist):
print("Splitting ",nlist)
if len(nlist)>1:
mid = len(nlist)//2
lefthalf = nlist[:mid]
righthalf = nlist[mid:]
mergeSort(lefthalf)
mergeSort(righthalf)
#merging left half and right half into a single sorted list
i=j=k=0
while i<len(lefthalf) and j <len(righthalf):
if lefthalf[i] < righthalf[j]:
nlist[k]=lefthalf[i]
i=i+1
k=k+1
else:
nlist[k]=righthalf[j]
j=j+1
k=k+1
while i<len(lefthalf):
nlist[k]=lefthalf[i]
i=i+1
k=k+1
while j <len(righthalf):
nlist[k]=righthalf[j]
j=j+1
k=k+1
print("Merging ",nlist)
nlist = [14,46,43,27,57,41,45,21,70]
mergeSort(nlist)
print(nlist)
Output:
Splitting [14, 46, 43, 27, 57, 41, 45, 21, 70]
Splitting [14, 46, 43, 27]
Splitting [14, 46]
Splitting [14]
Splitting [46]
Merging [14, 46]
Splitting [43, 27]
Splitting [43]
Splitting [27]
Merging [27, 43]
Merging [14, 27, 43, 46]
Splitting [57, 41, 45, 21, 70]
Splitting [57, 41]
Splitting [57]
Splitting [41]
Merging [41, 57]
Splitting [45, 21, 70]
Splitting [45]
Splitting [21, 70]
Splitting [21]
Splitting [70]
Merging [21, 70]
Merging [21, 45, 70]
Merging [21, 41, 45, 57, 70]
Merging [14, 21, 27, 41, 43, 45, 46, 57, 70]
[14, 21, 27, 41, 43, 45, 46, 57, 70]
Output:
14.Write a python program to plot a line graph
Output
15.Write a Python program to compute element-wise sum of given tuples.
x = (1,2,3,4)
y = (3,5,2,1)
z = (2,2,3,1)
print("Original lists:")print(x)
print(y)
print(z)
print("\nElement-wise sum of the said tuples:")result = tuple(map(sum, zip(x, y, z)))
print(result)
Output:
Original lists:
(1, 2, 3, 4)
(3, 5, 2, 1)
(2, 2, 3, 1)
Element-wise sum of the said tuples:
(6, 9, 8, 6)
def lettercount(str):d={}
str=str.lower() str="".join(str.split())
for i in str:
if i in d:
d[i]=d[i]+1else:
d[i]=1
return d
print(lettercount("python prog"))
Output:
17.Write a recursive Python function that recursively computes sum of elementsin a list of lists.
Program:
def lsum(l):
sum=0
for i in l:
if type(i)== list:
sum=sum+lsum(i)
else:
sum=sum+i
return sum
lst=[1,2,[3,4],[5,6,7],8]
print(lsum(lst))
Output:
36