Shakib
Shakib
age = 21
print(f"Name: {name}")
print(f"Age: {age}")
Output:
a = 10
b=5
print("Sum:", a + b)
print("Difference:", a - b)
print("Multiplication:", a * b)
print("Quotient:", a / b)
print("Remainder:", a % b)
print("Power:", a ** b)
Output:
CODE:
number = -5
if number > 0:
print("The number is positive.")
else:
Output:
Code 2: Loops
count1 = 0
print(f"Count: {count1}")
count1+=1
Output:
def greet(name):
message = greet("Shakib")
print(message)
Output:
CODE:
Code 1: BFS
queue = deque([Start])
while queue:
node = queue.popleft()
visited.add(node)
queue.extend(graph[node])
graph = {
'A' : ['L','M'],
'C' : ['L'],
'K' : ['R'],
'G' : ['P'],
'Y' : ['T']
bfs(graph, 'A')
Output:
Code 2: DFS
visited = set()
stack = [Start]
while stack:
node = stack.pop()
visited.add(node)
stack.extend(reversed(graph[node]))
graph = {
'A' : ['L','M'],
'C' : ['L'],
'G' : ['P'],
'Y' : ['T']
dfs(graph, 'A')
Output:
CODE:
graph = {
'A':[('D', 3), ('E', 5)],
heuristic ={
'A': 15,
'D': 12,
'E': 14,
'L': 10,
'M': 8,
'K': 9,
'T': 6,
'Y': 0
visited = set()
pq = PriorityQueue()
pq.put((heuristic[start],start))
while not pq.empty():
_, current = pq.get()
if current in visited:
continue
if current not in visited:
print("visiting:", current)
visited.add(current)
if current == goal:
print("Goal Found")
return
best_first_search('A', 'Y')
Output:
Code 2: A* Search
graph = {
heuristic = {
'A': 15,
'D': 12,
'E': 14,
'L': 10,
'M': 8,
'K': 9,
'T': 6,
'Y': 0
pq = PriorityQueue()
if current in visited:
continue
print("Visiting:", current)
visited.add(current)
if current == goal:
return
a_star_search('A', 'Y')
Output:
CODE:
def step_function(x):
if x >= 0:
return 1
else:
return 0
class Perceptron:
self.weights = [0, 0]
self.bias = 0
self.learning_rate = learning_rate
self.epochs = epochs
def predict(self, inputs):
return step_function(total)
print(f"Epochs: {epoch+1}")
[0, 1],
[1, 0],
[1, 1]
]
labels = [0, 0, 0, 1]
p = Perceptron()
p.train(training_inputs, labels)
Output:
CODE:
import random
def fitness(x):
return 15 * x - x ** 2
def decode(chromosome):
return int(chromosome, 2)
def random_chromosome():
total = sum(fitness_values)
current = 0
for i in range(len(population)):
current += fitness_values[i]
return population[i]
point = random.randint(1, 3)
return parent1[:point] + parent2[point:], parent2[:point] + parent1[point:]
# Step 7: Mutation (bit flip)
new_chrom=''
else:
new_chrom += bit
return new_chrom
population = create_population(pop_size)
for gen in range(generations):
print(f"Generation {gen}:")
for i in range(pop_size):
new_population = []
new_population.append(best)
new_population.append(mutate(child1))
if len(new_population) < pop_size:
new_population.append(mutate(child2))
population = new_population
# Final result
genetic_algorithm()
OUTPUT:
CODE:
# Raw data
outlook = ['S','S','O','R','R','R','O','S','S','R','S','O','O','R']
temp = ['H','H','H','M','C','C','C','M','C','M','M','M','H','M']
humidity=['H','H','H','H','N','N','N','H','N','N','N','H','N','H']
windy = ['F','T','F','F','F','T','T','F','F','F','T','T','F','T']
play = ['N','N','P','P','P','N','P','N','P','P','P','P','P','N']
outlook_encoded = preprocessing.LabelEncoder().fit_transform(outlook)
temp_encoded = preprocessing.LabelEncoder().fit_transform(temp)
humidity_encoded = preprocessing.LabelEncoder().fit_transform(humidity)
windy_encoded = preprocessing.LabelEncoder().fit_transform(windy)
play_encoded = preprocessing.LabelEncoder().fit_transform(play)
print("\nFeatures:\n", features)
OUTPUT: