print("Menu for list operations") print('1.nestedlist')
print('2.length')
print('3.concatenation')
print('4.membership')
print('5.iteration')
print('6.indexing')
print('7.slicing')
print('exit')
choice=int(input('enter choice'))
11 of 30
if choice==1:
nestedlist()
elif choice==2:
length()
elif choice==3:
concatenation()
elif choice==4:
membership()
elif choice==5:
iteration()
elif choice==6:
indexing()
elif choice==7:
slicing()
else:
break
12 of 30
(b)
num=[1,2,3,4,5]
num2=[6,7,8,9,10]
def append():
n = int(input("Enter any number to append: ")) num.append(n)
print("Updated list:", num)
def extend():
num.extend(num2)
print("Extended list:", num)
13 of 30
def delete():
try:
index = int(input("Enter the index of the number to be deleted from num: "))
if 0 <= index < len(num):
del num[index]
print("Updated list after deletion:", num)
else:
print("Invalid index. Please try again.")
except ValueError:
print("Invalid input. Please enter a valid index.")
14 of 30
def add():
try:
position = int(input("Enter the position where you want to add the number: "))
value = int(input("Enter the value to insert: ")) if 0 <= position <= len(num): num.insert(position, value)
print("Updated list:", num)
else:
print("Invalid position. Please try again.")
except ValueError:
print("Invalid input. Please enter a valid number.")
15 of 30
while True:
print("\nMenu:")
print("1. Add")
print("2. Append")
print("3. Extend")
print("4. Delete")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
add()
16 of 30
elif choice == "2":
append()
elif choice == "3":
extend()
elif choice == "4":
delete()
elif choice == "5":
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
17 of 30
SAMPLE PROGRAM 3
Write a python program to implement simple Chatbot with minimum 10 conversations
class SimpleChatbot:
def __init__(self):
self.conversations = { "hi": "Hello! How can I assist you today?",
"hello": "Hi there! What can I do for you?",
"how are you": "I'm just a program, but I'm functioning as expected! How about you?",
"what is your name": "I'm a chatbot created to assist you. You can call me ChatBot!",
18 of 30
"what can you do": "I can have simple conversations and assist with basic queries!",
"tell me a joke": "Why don't programmers like nature? It has too many bugs!",
"bye": "Goodbye! Have a great day!",
"thank you": "You're welcome! Happy to help.",
"what is python": "Python is a versatile programming language known for its simplicity and readability.",
"help": "Sure! Please tell me what you need help with, and I'll do my best to assist.", }
19 of 30
def respond(self, user_input):
user_input = user_input.lower()
return self.conversations.get(user_input, "I'm sorry, I didn't understand that. Can you please rephrase?")
if __name__ == "__main__":
chatbot = SimpleChatbot()
print("ChatBot: Hi! I'm your ChatBot. Type 'bye' to end the chat.")
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print("ChatBot: Goodbye! Have a great day!")
break
response = chatbot.respond(user_input)
print(f"ChatBot: {response}")
20 of 30
SAMPLE PROGRAM 4
Write a python program to Illustrate Different Set Operations
def create():
global set1,set2
user_input=input("Enter numbers for set1:") set1=set(int(item) for item in user_input.split()) user_input2=input("Enter numbers for set2:") set2=set(int(item) for item in user_input2.split())
21 of 30
def add():
set1.add(8)
print(“set1 after addition”)
print(set1)
set2.add(5)
print(“set2 after addition”)
print(set2)
def remove():
set1.remove(0)
print("set after addition")
print(set1)
22 of 30
def display():
print("set1 is",set1)
print("set2 is",set2)
def union():
print("union of set1 and set2 is",set1|set2)
def intersection():
print("Intersection of set1 and set2 is",set1&set2)
def difference():
print("Difference of set1 and set2 is",set1-set2)
23 of 30
def symmetric_difference():
print("Symmetric difference of set1 and set2 is",set1^set2)
while(True):
print("Menu for set operations") print("1.Create")
print("2.Add")
print("3.Remove")
print("4.Display")
print("5.Union")
print("6.Intersection")
print("7.Difference")
print("8.Symmetric Difference")
print("9.Exit")
choice=int(input("make the choice"))
24 of 30
if choice==1:
create()
elif choice==2:
add()
elif choice==3:
remove()
elif choice==4:
display()
25 of 30
elif choice==5:
union()
elif choice==6:
intersection()
elif choice==7:
difference()
elif choice==8:
symmetric_difference()
elif choice==9:
exit()
26 of 30
Prog 1: Implement and demonstrate depth-first search algorithm on water jug problem
Water Jug Problem:
Problem statement: We have two jugs, a 5-gallon (5-g) and the other 3-gallon (3-g) with no measuring marker on them. There is endless supply of water through tap. Our task is to get 4 gallon of water in the 5-g jug.
27 of 30
Solution: State space for this problem can be described as the set of ordered pairs of integers (X, Y) such that X represents the number of gallons of water in 5-g jug and Y for 3-g jug.
1. Start state is (0,0)
2. Goal state is (4, N) for any value of NS3.
The possible operations that can be used in this problem are listed as follows:
Fill 5-g jug from the tap and empty the 5-g jug by throwing water down the drain
Fill 3-g jug from the tap and empty the 3-g jug by throwing water down the drain
Pour some or 3-g water from 5-g jug into the 3-g jug to make it full
Pour some or full 3-g jug water into the 5-g jug
These operations can formally be defined as production rules as given in table.