1 of 30

Artificial Intelligence Lab

Dr. R.N. Uma Mahesh

Associate Professor

Dept. of Computer Science & Engineering (AI and ML)

ATMECE, Mysuru

2 of 30

  • SAMPLE PROGRAM 1
  • (a) Write a python program to print the multiplication table for the given number
  • (b) Write a python program to check whether the given number is prime or not?
  • (c) Write a python program to find factorial of the given number?

3 of 30

  • (a) num=int(input("Enter the number to print it's multiplication table"))
  • print("The multiplication table of:", num)
  • for count in range(1,11): print(num,"*",count,"=", num*count)

4 of 30

  • num = int(input("Enter the number to check for prime: "))
  • if num > 1:
  • for i in range(2, num):
  • if num % i == 0:
  • print("The number", num, "is not prime")
  • break
  • else: # This else belongs to the for loop, executed only if no break occurs
  • print(num, "is a prime number")
  • else: print(num, "is not a prime number") # 0 and 1 are not prime numbers

5 of 30

  • num=int(input("Enter a number:"))
  • factorial=1
  • if num < 0:
  • print("Factorial does not exist for negative numbers")
  • elif num == 0:
  • print("the factorial of 0 is 1")
  • else:
  • for i in range(1,num+1):
  • factorial=factorial*i
  • print("The factorial of",num,"is",factorial)

6 of 30

  • SAMPLE PROGRAM 2
  • (a) Write a python program to implement list operations (Nested List, Length, Concatenation, Membership, Iteration, Indexing, and Slicing)
  • (b) Write a python program to implement list methods (Add, append, Extend & Delete)

7 of 30

  • (a)
  • list1=[1,2,3,4]
  • list2=[5,6,7,8]
  • def length():
  • length=len(list1)
  • print('length of list is',length)
  • def concatenation():
  • concat=list1+list2
  • print("concatenated list is",concat)

8 of 30

  • def membership():
  • print("1 in list1")
  • print("2 in list1")
  • print("3 not in list1")
  • def iteration():
  • for i in list1:
  • print(i)
  • def indexing():
  • i=int(input('enter any index'))
  • print(list1[i],'is the number in index',i)

9 of 30

  • def slicing():
  • s_index=int(input('enter starting index'))
  • e_index=int(input('enter ending index')) print(list1[s_index:e_index])
  • def nestedlist():
  • list1.append(list2)
  • print(list1)

10 of 30

  • while(True):
  • 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.

28 of 30

29 of 30

  • def water_jug_dfs(capacity_x, capacity_y, target):
  • stack = [(0, 0)] # Start with empty jugs (x, y)
  • visited = set() # Track visited states
  • path = [] # Store the path to solution
  • while stack:
  • state = stack.pop()
  • if state in visited:
  • continue
  • visited.add(state)
  • path.append(state)
  • x, y = state
  • if x == target or y == target:
  • return path # Solution found

30 of 30

  • # Possible operations:
  • next_states = set()
  • next_states.add((capacity_x, y)) # Fill jug X
  • next_states.add((x, capacity_y)) # Fill jug Y
  • next_states.add((0, y)) # Empty jug X
  • next_states.add((x, 0)) # Empty jug Y
  • # Pour water from X to Y
  • transfer = min(x, capacity_y - y)
  • next_states.add((x - transfer, y + transfer))
  • # Pour water from Y to X
  • transfer = min(y, capacity_x - x)
  • next_states.add((x + transfer, y - transfer)) # Add new states to stack
  • for new_state in next_states:
  • if new_state not in visited:
  • stack.append(new_state)
  • return None # No solution
  • # Example
  • usage:capacity_x, capacity_y, target = 4, 3, 2 # Jug capacities and target amount
  • solution_path = water_jug_dfs(capacity_x, capacity_y, target)
  • print("Solution Path:", solution_path)