Case StudyAI
Case StudyAI
while queue:
node = queue.popleft() # Remove the first element from the queue
if node not in visited:
print(node, end=" ") # Process the node (e.g., print it)
visited.add(node) # Mark the node as visited
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
while stack:
node = stack.pop() # Get the last element (LIFO order) from the stack
if node not in visited:
print(node, end=" ") # Process the node (e.g., print it)
visited.add(node) # Mark the node as visited
# Example usage:
graph = {
'A': ['B', 'C'],
'B': ['A', 'D', 'E'],
'C': ['A', 'F'],
'D': ['B'],
'E': ['B', 'F'],
'F': ['C', 'E']
}
Pruning in the context of a Tic-Tac-Toe game is typically implemented using the Minimax algorithm with
Alpha-Beta Pruning to make the AI's decision-making more efficient. Minimax helps determine the best
move by simulating all possible moves and picking the one that maximizes the AI's chances of winning
while minimizing the opponent's chances.
Alpha-Beta Pruning optimizes the Minimax by “pruning” branches that do not need to be explored
because they will not affect the final decision. This helps reduce the number of computations,
making the algorithm faster.
Here’s an example of implementing Tic-Tac-Toe with Minimax and Alpha-Beta Pruning:
import math
def is_draw(board):
return all(cell is not None for cell in board)
def best_move(board):
best_val = -math.inf
move = -1
for i in range(9):
if board[i] is None:
board[i] = "X"
move_val = minimax(board, 0, False, -math.inf, math.inf)
board[i] = None
if move_val > best_val:
best_val = move_val
move = i
return move
# Example gameplay
board = [None] * 9
current_player = "X" # AI starts as "X"
while True:
if current_player == "X":
move = best_move(board)
print(f"AI chooses position {move}")
board[move] = "X"
else:
print("Current board:")
print_board(board)
move = int(input("Enter your move (0-8): "))
if board[move] is None:
board[move] = "O"
else:
print("Invalid move. Try again.")
continue
if is_winner(board, current_player):
print_board(board)
print(f"{current_player} wins!")
break
elif is_draw(board):
print_board(board)
print("It's a draw!")
break
# Switch player
current_player = "O" if current_player == "X" else "X"
4. Hangman game
import random
def choose_word():
return random.choice(word_list).upper()
def hangman():
word = choose_word()
guessed_letters = set()
attempts = 6 # Number of allowed wrong guesses
print("Welcome to Hangman!")
print(f"The word has {len(word)} letters.")
if attempts == 0:
print(f"Game over! The word was: {word}")
while queue:
jug1, jug2 = queue.popleft()
# Example usage
jug1_capacity = 4
jug2_capacity = 3
target = 2
if isinstance(solution_path, list):
print("Solution path (jug1, jug2):")
for step in solution_path:
print(step)
else:
print(solution_path)
6. Sort the sentence in alphabetical order/ remove punctuations from the given string
import string
def sort_sentence(sentence):
# Remove punctuation from the sentence
sentence = sentence.translate(str.maketrans("", "", string.punctuation))
# Example usage
sentence = "Hello, world! This is a test sentence."
sorted_sentence = sort_sentence(sentence)
print("Sorted sentence:", sorted_sentence)
7. Remove stop words for a given passage from a text file using NLTK.
from nltk.corpus import stopwords
import string
def remove_stopwords_from_file(filename):
# Load stop words from NLTK
stop_words = set(stopwords.words('english'))
# Example usage
filename = 'passage.txt' # Replace with your text file's name
filtered_text = remove_stopwords_from_file(filename)
print("Text after removing stop words:")
print(filtered_text)
# Evaluate accuracy
print("Classifier accuracy percent:", accuracy(classifier, test_set) * 100)
# Example usage
sentence = "This movie was absolutely fantastic and thrilling!"
classification = classify_sentence(sentence)
print("Classification:", classification)