AI

 

7) PROGRAM TO IMPLEMENT TOWER OF HANOI USING PYTHON

def tower_of_hanoi(n,source,aux,des):

    if n==0:

        return

    tower_of_hanoi(n-1,source,des,aux)

    print("Move disk",n," from source ",source," to ",des)

    tower_of_hanoi(n-1,aux,source,des)

n=3

tower_of_hanoi(n,"A","B","C")

 

OUTPUT:

 


 

8) PROGRAM TO IMPLEMENT MONKEY BANANA PROBLEM USING PYTHON

def monkey(n):

    climb=0

    banana=0

    hungry=True

    for i in range(n):

        if hungry:

            climb+=1

            banana+=1

            hungry=False

        else:

            climb+=1

    return climb,banana

n=10

climb,banana=monkey(n)

print (f"The monkey made {climb} climb and got {banana} bananas.")

 

OUTPUT:



 

 

9) PROGRAM TO IMPLEMENT ALPHA BETA PRUNING USING PYTHON

MAX, MIN=1000, -1000

def minimax(depth,nodeindex,maximizingPlayer,values,alpha,beta):

    if depth==3:

        return values[nodeindex]

    if maximizingPlayer:

        best=MIN

        for i in range(0,2):

            val=minimax(depth+1,nodeindex*2+i,False,values,alpha,beta)

            best=max(best,val)

            alpha=max(alpha,best)

            if beta<=alpha:

                break

        return best

    else:

        best=MAX

        for i in range(0,2):

            val=minimax(depth+1,nodeindex*2+i,True,values,alpha,beta)

            best=min(best,val)

            beta=min(beta,best)

            if beta<=alpha:

                break

        return best

if __name__=="__main__":

    values=[3,5,6,9,1,2,0,-1]

    print("The optimal values is" ,minimax(0,0,True,values,MIN,MAX))

 

OUTPUT:



 10) PROGRAM TO IMPLEMENT 8-QUEENS PROBLEM USING PYTHON

global N

N=4

def printSolution(board):

    for i in range(N):

        for j in range(N):

            print (board[i][j],end ='')

        print ()

def isSafe(board,row,col):

    for i in range(col):

        if board[row][i]==1:

            return False

    for i,j in zip(range(row,-1,-1),range(col,-1,-1)):

        if board[i][j]==1:

            return False

    for i,j in zip(range(row,N,1),range(col,-1,- 1)):

        if board[i][j] == 1:

            return False

    return True

def solveNQUtil(board,col):

    if col >= N:

        return True

    for i in range(N):

        if isSafe(board, i,col):

            board[i][col] = 1

            if solveNQUtil(board, col+1)== True:

                return True

            board[i][col]=0

    return False

def solveNQ():

    board = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]

    if solveNQUtil(board,0)==False:

        print("Solution Does not exist")

        return False

    printSolution (board)

    return True

solveNQ()

 

OUTPUT:


 


 

 

11. PROGRAM TO IMPLEMENT SIMPLE CHATBOT

import random

responses= ["Hello,How Can I help You?",

           "What do you want to talk about?",

           "i'm sure what do you mean?",

           "Can you repeat that?",

           "i'm sorry, i don't understand",

           "GoodBye!"]

def get_response():

    return random.choice(responses)

def start_chatbot():

    print("hello,i'm chatbot. What do you want to talk about?")

    while True:

        user_input=input()

        response=get_response()

        print(response)

start_chatbot()

 

OUTPUT:

 



 

12. PROGRAM TO IMPLEMENT HANGMAN GAME USING PYTHON

import random

def get_word(): words = [ 'apple', 'banana', 'cherry', 'date', 'fig', 'grape', 'honeydew', 'kiwi', 'lemon', 'mango', 'nectarine', 'orange', 'papaya', 'peach', 'pineapple', 'plum', 'raspberry', 'strawberry', 'tangerine', 'ugli fruit', 'vanilla', 'watermelon']

    return random.choice(words)

def display_word(word, guessed):

    return ''.join([char if char in guessed else '_' for char in word])

def play_game():

    word = get_word()

    guessed = set()

    attempts = 10  

    while attempts > 0:

        print(f'Attempts remaining: {attempts}')

        print(display_word(word, guessed))     

        guess = input('Guess a letter: ').lower()    

        if len(guess) != 1 or not guess.isalpha():

            print('Invalid guess. Please guess a single letter.')

            continue

        if guess in guessed:

            print('You already guessed that letter.')

            continue

        guessed.add(guess) 

        if guess in word:

            print('Correct!')

            if set(word).issubset(guessed):

                print(f'Congratulations! You guessed the word: {word}')

                break

        else:

            print('Incorrect.')

            attempts -= 1   

    if attempts == 0:

        print(f'Sorry, you ran out of attempts. The word was: {word}')

if __name__ == '__main__':

    play_game()


 


13) PYTHON PROGRAM TO REMOVE STOP WORDS FOR A GIVEN PASSAGE FROM A TEXT FILE USING NLTK

import nltk

from nltk.corpus import stopwords

nltk.download('stopwords')

with open("file1.txt", "r") as f1, open("file2.txt", "w") as f2:

    stop = set(stopwords.words('english'))  # Convert to set for faster lookup

    for line in f1:

        words = line.split()

        for word in words:

            if word.lower() not in stop:

                f2. write (word + " ")

 

 OUTPUT

file1.txt

i love to red english novels

file2.txt

love red english novels

 

Comments

Popular posts from this blog

MACHINE LEARNING

UI LAB