BCA203P- Python Programming
Part A
1. Write a python Program To find “Anagram” and “Palindrome”
Code:
print("****Anagram****")
s1 =input("Enter string1 ")
s2 =input("Enter string2")
def anagram_check(s1, s2):
if(sorted(s1)== sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
anagram_check(s1, s2)
print("****palindrome****")
p1=input("Enter string to check palindrome ")
def palind_check(p1):
rev_p=p1[::-1]
if p1==rev_p:
print("It's a palindrome")
else:
print("It's not a palindrome")
palind_check(p1)
, 2. Write a python program to Demonstrate linear search.
Code:
list1=[]
#input the number of elements
n=int(input("enter the number of elements: "))
print("Enter the elements")
for i in range(n):
ele=int(input())
list1.append(ele)
print("list elemens are : " , list1)
# input the key to be searched
key=int(input("enter the key to be searched"))
def linear_Search(list1,n,key):
for i in range(0,n):
if list1[i]==key:
return i
return -1
print("______Linear Search_____")
#perform the linear search
res=linear_Search(list1,n,key
if res== -1:
print("elements not found")
else:
print("elements found at index : ", res)
, 3. Write a Menu Driven Program to add and Delete Stationary items. Use
a Dictionary for Adding Items and Brands.
i. 1.Add an item to the stationaries
ii. 2.Remove an item from stationaries
iii. 3.Display stationery
iv. 4.exit
Code:
dict={}
print("Choose the Option from the Menu")
print("--------------------------------------------")
print("1.Add an item to the stationaries ")
print("2.Remove an item from stationaries ")
print("3.Display stationery ")
print("4.exit")
#key - brand
#value - item
ch=int(input("Enter your Choice: "))
while(ch!=5):
if ch==1:
brand=input("Enter the brand [key] to be inserted : ")
item=input("Enter the item to be inserted : ")
dict[brand]=item
print("Added successfully" )
elif ch==2:
brand=input("Enter the brand [key] to be deleted: ")
if brand in dict.keys():
del dict[brand]
print(" Deleted successfully" )
elif brand not in dict.keys():
print("invalid key")
elif ch==3:
print(dict)
elif ch==4:
print( "Exiting!!!!!")
break
else:
print("Invalid choice....!! Choose option between 1 to 4")
ch=int(input("Enter your choice: "))
Part A
1. Write a python Program To find “Anagram” and “Palindrome”
Code:
print("****Anagram****")
s1 =input("Enter string1 ")
s2 =input("Enter string2")
def anagram_check(s1, s2):
if(sorted(s1)== sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
anagram_check(s1, s2)
print("****palindrome****")
p1=input("Enter string to check palindrome ")
def palind_check(p1):
rev_p=p1[::-1]
if p1==rev_p:
print("It's a palindrome")
else:
print("It's not a palindrome")
palind_check(p1)
, 2. Write a python program to Demonstrate linear search.
Code:
list1=[]
#input the number of elements
n=int(input("enter the number of elements: "))
print("Enter the elements")
for i in range(n):
ele=int(input())
list1.append(ele)
print("list elemens are : " , list1)
# input the key to be searched
key=int(input("enter the key to be searched"))
def linear_Search(list1,n,key):
for i in range(0,n):
if list1[i]==key:
return i
return -1
print("______Linear Search_____")
#perform the linear search
res=linear_Search(list1,n,key
if res== -1:
print("elements not found")
else:
print("elements found at index : ", res)
, 3. Write a Menu Driven Program to add and Delete Stationary items. Use
a Dictionary for Adding Items and Brands.
i. 1.Add an item to the stationaries
ii. 2.Remove an item from stationaries
iii. 3.Display stationery
iv. 4.exit
Code:
dict={}
print("Choose the Option from the Menu")
print("--------------------------------------------")
print("1.Add an item to the stationaries ")
print("2.Remove an item from stationaries ")
print("3.Display stationery ")
print("4.exit")
#key - brand
#value - item
ch=int(input("Enter your Choice: "))
while(ch!=5):
if ch==1:
brand=input("Enter the brand [key] to be inserted : ")
item=input("Enter the item to be inserted : ")
dict[brand]=item
print("Added successfully" )
elif ch==2:
brand=input("Enter the brand [key] to be deleted: ")
if brand in dict.keys():
del dict[brand]
print(" Deleted successfully" )
elif brand not in dict.keys():
print("invalid key")
elif ch==3:
print(dict)
elif ch==4:
print( "Exiting!!!!!")
break
else:
print("Invalid choice....!! Choose option between 1 to 4")
ch=int(input("Enter your choice: "))