AI Practical Assignments
AI Practical Assignments
if number > 1:
if (number % i) == 0:
break
else:
else:
factorial = 1
if num < 0:
elif num == 0:
else:
for i in range(1,num + 1):
factorial = factorial*i
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
(b) Write a python program to implement list operations (add, append, extend & delete)
myList = ["Earth", "revolves", "around", "Sun"]
print(myList)
print(len(myList))
print(myList[0])
print(myList[3])
#Slice elements from a list
print(myList[1:3])
#Use negative index
print(myList[-1])
#print(myList[4])
#add element to a list
myList.insert(0,"The")
print(myList)
print(len(myList))
myList.insert(4,"the")
print(myList)
#append an element to a list
myList.append("continuously")
print(myList)
print(len(myList))
#When use extend the argument should be another list
#the elements of that list will be added
#to the current list as individual elements
myList.extend(["for", "sure"])
print(myList)
print(len(myList))
#you can append a sublist to the current list using append
myList.append(["The","earth","rotates"])
print(myList)
print(len(myList))
#delete a element in the list using element
myList.remove("The")
#delete a element in the list using index
myList.remove(myList[3])
print(myList)
(b) Write a python program to generate Calendar for the given month and year?
import calendar
yy = 2019
mm = 6
print(calendar.month(yy, mm))
import calendar
yy = int(input("Enter year: "))
mm = int(input("Enter month: "))
print(calendar.month(yy, mm))
(c)Write a python program to make a simple calculator that can add, subtract, multiply and
divide using functions
# define functions
def add(x, y):
"This function adds two numbers"
return x + y
print("Select operation.")
print("1.Add")
print("2.Subtract")
print("3.Multiply")
print("4.Divide")
if choice == '1':
print(num1,"+",num2,"=", add(num1,num2))
graph = {
'A' : ['B','C'],
'B' : ['D', 'E'],
'C' : ['F'],
'D' : [],
'E' : ['F'],
'F' : []
}
while queue:
s = queue.pop(0)
print (s, end = " ")
# Driver Code
bfs(visited, graph, 'A') #execution starts here bfs(visited[],Graph,’A’) PC jump to line 12
Explanation
● Lines 3-10: The illustrated graph is represented using an adjacency list. An easy way to do
this in Python is to use a dictionary data structure, where each vertex has a stored list of its
adjacent nodes.
● Line 12: visited is a list that is used to keep track of visited nodes.
● Line 13: queue is a list that is used to keep track of nodes currently in the queue.
● Line 29: The arguments of the bfs function are the visited list, the graph in the form of a
dictionary, and the starting node A.
1. It checks and appends the starting node to the visited list and the queue.
2. Then, while the queue contains elements, it keeps taking out nodes from the queue,
appends the neighbors of that node to the queue if they are unvisited, and marks
them as visited.
3. This continues until the queue is empty.
graph1 = {
'A' : ['B','S'],
'B' : ['A'],
'C' : ['D','E','F','S'],
'D' : ['C'],
'E' : ['C','H'],
'F' : ['C','G'],
'G' : ['F','S'],
'H' : ['E','G'],
'S' : ['A','C','G']
}
Explaination :
A is the root node. So since A is visited, we push this onto the stack.
Stack : A
Let’s go to the branch A-B. B is not visited, so we go to B and push B onto the stack.
Stack : A B
Now, we have come to the end of our A-B branch and we move to the n-1th node which is
A. We will now look at the adjacent node of A which is S. Visit S and push it onto the stack.
Now you have to traverse the S-C-D branch, up to the depth ie upto D and mark S, C, D as
visited.
Stack: A B S C D
Since D has no other adjacent nodes, move back to C and traverse its adjacent branch
E-H-G to the depth and push them onto the stack.
Stack : A B S C D E H G
On reaching D, there is only one adjacent node ie F which is not visited. Push F onto the
stack as well.
Stack : A B S C D E H G F
This stack itself is the traversal of the DFS.
if rno==2:
if y<3:
y=3
if rno==5:
if x>0:
x=0
if rno==6:
if y>0:
y=0
if rno==7:
if x+y>= 4 and y>0:
x,y=4,y-(4-x)
if rno==8:
if x+y>=3 and x>0:
x,y=x-(3-y),3
if rno==9:
if x+y<=4 and y>0:
x,y=x+y,0
if rno==10:
if x+y<=3 and x>0:
x,y=0,x+y
import numpy as np
import pandas as pd
sns.set()
breast_cancer = load_breast_cancer()
X = pd.DataFrame(breast_cancer.data, columns=breast_cancer.feature_names)
y = pd.Categorical.from_codes(breast_cancer.target, breast_cancer.target_names)
y = pd.get_dummies(y, drop_first=True)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
confusion_matrix(y_test, y_pred)
#Importing libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/3, random_state=0)
# Fitting Simple Linear Regression to the Training set
from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)
#Step 5: Build the model with theRandom Forest tree regressor function
regressor = RandomForestRegressor(n_estimators=100, random_state=0)
regressor.fit(x_trn,y_trn.values.ravel())