Pdflabreport AI
Pdflabreport AI
Lab Report
On
Artificial Intelligence
Submitted to:
Department of Bachelor of Information Management
SPA College
Under the Supervision of:
Mr. Keshab Pal
Lecturer, BIM Department
Submitted By:
Jeewan Bhatta
BIM 5th Semester
August, 2024
LETTER OF RECOMMENDATION
Date: 05 /03 /2081
This is to certify that the “Artificial Intelligence” has been carried out by Mr.
Jeewan Bhatta in partial fulfilment of the degree of Bachelor of Information
Management for Tribhuvan University, during the academic year 2081. We
further declare that the work reported in this lab report has not been submitted
and will not be submitted, either in part or in full, for the award of any other
degree or diploma in this institute or any other institute or university.
To the best of our knowledge and belief, this work embodies the work of the
candidate himself/herself, has duly been completed, fulfils the requirement of the
ordinance relating to the bachelor’s degree of the university, and is up to the
standard in respect of content, presentation, and language for being referred to the
examiner.
………………………….. ……………………………
Keshab Pal Dharma Raj Bhatta
(Lecturer, Lab tutor) (Program Director, BIM)
…………………………
External
1) Creating multiple family trees in Python
Creating multiple family trees in Python involves representing family members and their
relationships. Here's a basic outline on how you can implement this:
1.Define the Family Member Structure: Create a structure to represent each family member.
This structure can include pointers to other family members to represent relationships.
2.Create Family Trees: Implement functions to create and manage multiple family trees.
3.Add Members and Relationships: Implement functions to add members and define their
relationships within a family tree.
4. Display Family Trees: Implement functions to display the family trees for easy visualization.
Code:-
class Family:
def __init__(self, name, gender):
self.name = name
self.gender = gender
self.parents = []
self.children = []
def __repr__(self):
return self.name
class FamilyMember:
def __init__(self):
self.members = {}
def __repr__(self):
return '\n'.join(f"{member}: Parents: {[parent.name for parent in
member.parents]}, Children: {[child.name for child in member.children]}"
for member in self.members.values())
family_1 = FamilyMember()
family_2 = FamilyMember()
family_1.add_member("Basudev", "Male")
family_1.add_member("Puspa", "Female")
family_1.add_member("Jeewan", "Male")
family_1.relationship("Basudev", "Jeewan")
family_1.relationship("Puspa", "Jeewan")
family_2.add_member("Naresh", "Male")
family_2.add_member("Parbati", "Female")
family_2.add_member("Prakash", "Male")
family_2.relationship("Naresh", "Prakash")
family_2.relationship("Parbati", "Prakash")
Output:-
2) Factorial program
To write a Python program that generates and displays the factorial series, you need to
understand the concept of factorials. The factorial of a non-negative integer \( n \) is the product
of all positive integers less than or equal to \( n \), and it's denoted by \( n! \). For example, \(
5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 \).
Here's how you can write a Python program to generate and display the factorial series for
numbers from 0 to a specified limit:
Step-by-Step Implementation
3.Main function: Get the limit from the user and display the factorial series.
Code:-
def factorial(n):
if n == 0 or n==1 :
return 1
else:
return n * factorial(n-1)
print(f"{i}!={factorial(i)}")
Output:-
3) Fibonacci series
To write a Python program that generates and displays the Fibonacci series, you need to
understand the concept of the Fibonacci sequence. The Fibonacci sequence is a series of
numbers in which each number is the sum of the two preceding ones, usually starting with 0
and 1.
Here's how you can write a Python program to generate and display the Fibonacci series up to
a specified number of terms:
Example Program.
Code:-
def fibonacci(num):
a, b = 0, 1
result = []
if num==0:
return 0
elif num==1:
return 1
else:
for _ in range(num):
result.append(a)
a, b = b, a + b
return result
Output:-
4) Ancestor Program in Python
Creating an ancestor program in Python involves managing a family tree structure and
allowing for queries about the ancestors of a specific family member. This can be done by
defining a `FamilyMember` structure and implementing functions to manage the family tree
and query ancestors.
Example Program
1.Define the Family Member Structure: Define a structure to represent each family
member.
2.Add Family Members: Implement functions to add members and define their
relationships.
3.Find Ancestors: Implement functions to find and display the ancestors of a specific
member.
4.Display Ancestors: Implement a main function to interact with the user and display the
results.
Code:-
class Family:
self.name = name
self.gender = gender
self.parents = []
self.parents.append(parent)
def __repr__(self):
return self.name
class Familymember:
def __init__(self):
self.members = {}
return self.members[name]
parent = self.members.get(parent_name)
child = self.members.get(child_name)
child.add_parent(parent)
else:
return self.members.get(name)
if ancestors is None:
ancestors = set()
for parent in member.parents:
ancestors.add(parent)
find_ancestors(parent, ancestors)
return ancestors
def main():
family_tree = Familymember()
family_tree.add_member("BasuDev", "Male")
family_tree.add_member("Jeewan", "Male")
family_tree.add_member("Bishnu", "Male")
family_tree.add_member("Laxmi", "Female")
family_tree.add_member("Ganesh", "Male")
family_tree.add_member("Kumar", "Male")
family_tree.add_member("Saraswati", "Female")
family_tree.add_relationship("BasuDev", "Jeewan")
family_tree.add_relationship("Bishnu", "Ganesh")
family_tree.add_relationship("Laxmi", "Ganesh")
family_tree.add_relationship("Kumar", "Bishnu")
family_tree.add_relationship("Saraswati", "Bishnu")
member = family_tree.get_member(member_name)
if member:
ancestors = find_ancestors(member)
if ancestors:
else:
else:
if __name__ == "__main__":
main()
Output:-
5) Tower of Hanoi program in Python
The Tower of Hanoi is a classic problem in computer science and mathematics, involving
three pegs and a number of disks of different sizes. The objective is to move all the disks
from the first peg to the third peg, following these rules:
3. All disks start on the first peg, and they must end on the third peg.
Code:-
def TowerOfHanoi(n, source, target, auxiliary):
if n > 0:
# Move n - 1 disks from source to auxiliary, so they are out of the way
TowerOfHanoi(n - 1, source, auxiliary, target)
# Move the nth disk from source to target
print(f"Move disk {n} from rod {source} to rod {target}")
State Representation
Actions
- `Move to box`
- `Climb box`
- `Grab bananas`
Code:-
class MonkeyBananaProblem:
def __init__(self, monkey_pos, box_pos, banana_pos):
self.monkey_pos = monkey_pos
self.box_pos = box_pos
self.banana_pos = banana_pos
self.monkey_on_box = False
self.monkey_has_bananas = False
def move_to_box(self):
print(f"Monkey moves from {self.monkey_pos} to the box at
{self.box_pos}.")
self.monkey_pos = self.box_pos
def push_box_to_bananas(self):
print(f"Monkey pushes the box from {self.box_pos} to the banana
position at {self.banana_pos}.")
self.box_pos = self.banana_pos
def climb_box(self):
print("Monkey climbs onto the box.")
self.monkey_on_box = True
def grab_bananas(self):
if self.monkey_on_box and self.box_pos == self.banana_pos:
print("Monkey grabs the bananas!")
self.monkey_has_bananas = True
else:
print("Invalid move. Monkey is not on the box at the banana
position.")
def is_goal_state(self):
return self.monkey_has_bananas
def solve_monkey_banana_problem():
monkey_pos = "A"
box_pos = "B"
banana_pos = "C"
if problem.is_goal_state():
print("Monkey successfully grabbed the bananas!")
else:
print("Monkey couldn't reach the bananas.")
if __name__ == "__main__":
solve_monkey_banana_problem()
Output:-
7) Realization of logic gates in Python
Implementing logic gates in Python can be done by creating functions that emulate the
behavior of each logic gate. Below are examples of how to implement basic logic gates
(AND, OR, NOT, NAND, NOR, XOR, XNOR) in Python.
Code:-
def AND(a, b):
if a == 1 and b == 1:
return 1
else:
return 0
def NOT(a):
if a == 1:
return 0
else:
return 1
print("\nNOT Gate:")
print(f"(0):{NOT(0)}") # 1
print(f"(1):{NOT(1)}") # 0
print("\nNAND Gate:")
print(f"(0,0):{NAND(0, 0)}") # 1
print(f"(0,1):{NAND(0, 1)}") # 1
print(f"(1,0):{NAND(1, 0)}") # 1
print(f"(1,1):{NAND(1, 1)}") # 0
print("\nNOR Gate:")
print(f"(0,0):{NOR(0, 0)}") # 1
print(f"(0,1):{NOR(0, 1)}") # 0
print(f"(1,0):{NOR(1, 0)}") # 0
print(f"(1,1):{NOR(1, 1)}") # 0
print("\nXOR Gate:")
print(f"(0,0):{XOR(0, 0)}") # 0
print(f"(0,1):{XOR(0, 1)}") # 1
print(f"(1,0):{XOR(1, 0)}") # 1
print(f"(1,1):{XOR(1, 1)}") # 0
print("\nXNOR Gate:")
print(f"(0,0):{XNOR(0, 0)}") # 1
print(f"(0,1):{XNOR(0, 1)}") # 0
print(f"(1,0):{XNOR(1, 0)}") # 0
print(f"(1,1):{XNOR(1, 1)}") # 1
Output:-