0% found this document useful (0 votes)
6 views

EXPERIMENT 1 AIML

The document contains six experiments demonstrating various Python programming tasks. These include implementing Breadth First Search, solving the Water Jug problem, removing punctuation from strings, sorting sentences alphabetically, creating a Hangman game, and developing a Tic Tac Toe game. Each experiment provides a code snippet and an example usage.

Uploaded by

jindalshivangi19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

EXPERIMENT 1 AIML

The document contains six experiments demonstrating various Python programming tasks. These include implementing Breadth First Search, solving the Water Jug problem, removing punctuation from strings, sorting sentences alphabetically, creating a Hangman game, and developing a Tic Tac Toe game. Each experiment provides a code snippet and an example usage.

Uploaded by

jindalshivangi19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT 1

Write a python program to implement Breadth First


Search Traversal?
from collections import deque.

def bfs(graph, start):

visited = set()

queue = deque([start])

traversal = []

while queue:

node = queue.popleft()

if node not in visited:

visited.add(node)

traversal.append(node)

queue.extend(graph[node] - visited)

return traversal

# Example usage

graph = {

'A': {'B', 'C'},

'B': {'D', 'E'},

'C': {'F'},

'D': set(),

'E': {'F'},

'F': set()

print(bfs(graph, 'A'))
EXPERIMENT 2
Write a python program to implement Water Jug
Problem?

def water_jug_problem(jug1, jug2, target):

visited = set()

queue = [(0, 0)]

while queue:

x, y = queue.pop(0)

if (x, y) in visited:

continue

visited.add((x, y))

if x == target or y == target:

return True

queue.extend([

(jug1, y), (x, jug2), (0, y), (x, 0),

(x - min(x, jug2 - y), y + min(x, jug2 - y)),

(x + min(y, jug1 - x), y - min(y, jug1 - x))

])

return False

print(water_jug_problem(4, 3, 2))
EXPERIMENT 3
Write a python program to remove punctuations from
the given string?

def remove_punctuation(text):

import string

return text.translate(str.maketrans('', '', string.punctuation))

text = "Hello, World! How's everything?"

print(remove_punctuation(text))
EXPERIMENT 4
Write a python program to sort the sentence in
alphabetical order?

def sort_sentence(sentence):

words = sentence.split()

words.sort(key=str.lower)

return ' '.join(words)

sentence = "Python programming is amazing"

print(sort_sentence(sentence))

EXPERIMENT 5
Write a program to implement Hangman game using
python.
import random

def hangman():

words = ['python', 'java', 'kotlin', 'javascript']

word = random.choice(words)

guessed = ['_'] * len(word)

attempts = 6

guessed_letters = set()

while attempts > 0 and '_' in guessed:

print(' '.join(guessed))

guess = input("Guess a letter: ")

if guess in guessed_letters:

print("Already guessed!")

elif guess in word:

for i, letter in enumerate(word):

if letter == guess:

guessed[i] = guess

else:

attempts -= 1

print(f"Wrong! {attempts} attempts left.")

guessed_letters.add(guess)

if '_' not in guessed:

print("You won!")

else:

print(f"You lost! The word was {word}.")

hangman()
EXPERIMENT 6
Write a program to implement Tic Tac Toe game using
python.
def print_board(board):

for row in board:

print(" | ".join(row))

print("-" * 5)

def check_winner(board):

for row in board:

if row[0] == row[1] == row[2] != ' ':

return row[0]

for col in range(3):

if board[0][col] == board[1][col] == board[2][col] != ' ':

return board[0][col]

if board[0][0] == board[1][1] == board[2][2] != ' ' or board[0][2] ==


board[1][1] == board[2][0] != ' ':

return board[1][1]

return None

def tic_tac_toe():

board = [[' ' for _ in range(3)] for _ in range(3)]

current_player = 'X'

moves = 0

while moves < 9:

print_board(board)

row, col = map(int, input(f"Player {current_player}, enter row and col


(0-2): ").split())

if board[row][col] == ' ':

board[row][col] = current_player

moves += 1
winner = check_winner(board)

if winner:

print_board(board)

print(f"Player {winner} wins!")

return

current_player = 'O' if current_player == 'X' else 'X'

else:

print("Cell already taken, try again!")

print("It's a draw!")

tic_tac_toe()

You might also like