├── alphabeta.py ├── fuzzy.py ├── heap.py ├── astar.py ├── crossword.py ├── chatbot.py └── tictactoe.py /alphabeta.py: -------------------------------------------------------------------------------- 1 | MAX,MIN=1000,-1000 2 | def minmax(depth,nodeIndex,maximizingPlayer,values,alpha,beta): 3 | if depth==3: 4 | return values[nodeIndex] 5 | if maximizingPlayer: 6 | best=MIN 7 | for i in range(0,2): 8 | val=minmax(depth+1,nodeIndex*2+i,False,values,alpha,beta) 9 | best=max(best,val) 10 | alpha=max(alpha,best) 11 | if beta<=alpha: 12 | break 13 | return best 14 | else: 15 | best=MAX 16 | for i in range(0,2): 17 | val=minmax(depth+1,nodeIndex*2+i,True,values,alpha,beta) 18 | best=min(best,val) 19 | alpha=min(alpha,best) 20 | if beta<=alpha: 21 | break 22 | return best 23 | if __name__=="__main__": 24 | values=[10,9,14,4,5,18,50,22,13,14] 25 | print(f"Output:",minmax(0,0,True,values,MIN,MAX)) 26 | -------------------------------------------------------------------------------- /fuzzy.py: -------------------------------------------------------------------------------- 1 | def fuzzy_union(set1,set2): 2 | union_set={} 3 | for element in set1: 4 | union_set[element]=max(set1[element],set2.get(element,0)) 5 | for element in set2: 6 | if element not in union_set: 7 | union_set[element]=set2[element] 8 | return union_set 9 | 10 | def fuzzy_intersection(set1,set2): 11 | intersection_set={} 12 | for element in set1: 13 | if element in set2: 14 | intersection_set[element]=min(set1[element],set2[element]) 15 | return intersection_set 16 | 17 | def display_fuzzy_set(fuzzy_set): 18 | print("{",end="") 19 | for element,membership in fuzzy_set.items(): 20 | print(f"{element}:{membership}",end="") 21 | print("}") 22 | 23 | set1={'a':0.8,'b':0.6,'c':0.4,'d':0.2,'e':0.1} 24 | set2={'a':0.7,'b':0.5,'c':0.3,'d':0.9,'e':0.2} 25 | 26 | print("Fuzzy set 1:") 27 | display_fuzzy_set(set1) 28 | 29 | print("Fuzzy set 2:") 30 | display_fuzzy_set(set2) 31 | 32 | print("Fuzzy set union:") 33 | union_set=fuzzy_union(set1,set2) 34 | display_fuzzy_set(union_set) 35 | 36 | print("Fuzzy set intersection:") 37 | intersction_set=fuzzy_intersection(set1,set2) 38 | display_fuzzy_set(intersction_set) 39 | -------------------------------------------------------------------------------- /heap.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | def print_board(heap): 4 | print(f"Current heap: {'|'*heap}") 5 | 6 | def get_user_move(heap): 7 | while True: 8 | try: 9 | sticks_to_remove=int(input(f"Enter the number of sticks to remove:(minimum1,maximum{min(heap,heap//2)})")) 10 | if 1<=sticks_to_remove<=min(heap,heap//2): 11 | break 12 | else: 13 | print(f"Invalid number enter a number between 1 and{min(heap,heap//2)}") 14 | except ValueError: 15 | print("Invalid number") 16 | return sticks_to_remove 17 | 18 | def get_computer_move(heap): 19 | xor_sum=heap 20 | for i in range(heap): 21 | xor_sum^=i 22 | if xor_sum==0: 23 | max_sticks=min(heap,heap//2) 24 | sticks_to_remove=random.randint(1,max_sticks) 25 | else: 26 | max_sticks=min(heap//2,heap) 27 | sticks_to_remove=max(1,min(max_sticks,heap-xor_sum)) 28 | return sticks_to_remove 29 | 30 | def nim_game(): 31 | heap=16 32 | player_turn=1 33 | while heap>1: 34 | print_board(heap) 35 | player_name="Player1" if player_turn==1 else"Computer" 36 | sticks_to_remove=get_user_move(heap) if player_turn==1 else get_computer_move(heap) 37 | heap-=sticks_to_remove 38 | print(f"{player_name} removes {sticks_to_remove} sticks") 39 | player_turn=3-player_turn 40 | print_board(heap) 41 | winner="player1" if player_turn==2 else "Computer" 42 | print(f"player {player_turn} picks the last stick") 43 | print(f"{winner} wins!") 44 | if __name__=="__main__": 45 | nim_game() -------------------------------------------------------------------------------- /astar.py: -------------------------------------------------------------------------------- 1 | def astaralgo(start_node, stop_node): 2 | open_set = {start_node} 3 | closed_set = set() 4 | g = {start_node: 0} 5 | parents = {start_node: start_node} 6 | 7 | def heuristic(n): 8 | H_dist={ 9 | 'A':11, 10 | 'B':12, 11 | 'C':20, 12 | 'D':77, 13 | 'E':3, 14 | 'G':0 15 | } 16 | return H_dist[n] 17 | while open_set: 18 | n=min(open_set,key=lambda x:g[x]+heuristic(x)) 19 | if n==stop_node or Graph_nodes.get(n) is None: 20 | pass 21 | else: 22 | for m,weight in Graph_nodes.get(n,[]): 23 | if m not in open_set and m not in closed_set: 24 | open_set.add(m) 25 | parents[m]=n 26 | g[m]=g[n]+weight 27 | else: 28 | if g[m]>g[n]+weight: 29 | g[m]=g[n]+weight 30 | parents[m]=n 31 | if m in closed_set: 32 | closed_set.remove(m) 33 | open_set.add(m) 34 | if not n: 35 | print("Path doesnot exist") 36 | return None 37 | if n==stop_node: 38 | path=[] 39 | while parents[n]!=n: 40 | path.append(n) 41 | n=parents[n] 42 | path.append(start_node) 43 | path.reverse() 44 | return g[stop_node],path 45 | open_set.remove(n) 46 | closed_set.add(n) 47 | print(":Path doesnt exist") 48 | return None 49 | 50 | Graph_nodes={ 51 | 'A':[('B',2),('E',3)], 52 | 'B':[('C',1),('G',9)], 53 | 'C':None, 54 | 'D':[('D',6)], 55 | 'E':[('G',1)] 56 | } 57 | optimal_value_path=astaralgo('A','G') 58 | if optimal_value_path is not None: 59 | optimal_value, path = optimal_value_path 60 | print('Optimal value:', optimal_value) 61 | print('Path:', path) -------------------------------------------------------------------------------- /crossword.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | class Crossword: 4 | def __init__(self, height, width): 5 | self.height = height 6 | self.width = width 7 | self.grid = [[' ' for _ in range(width)] for _ in range(height)] 8 | self.words = [] 9 | 10 | def add_word(self, word): 11 | word = word.upper() 12 | if len(word) > max(self.height, self.width): 13 | print(f"Word '{word}' is too long to fit in the grid.") 14 | return 15 | self.words.append(word) 16 | placed = False 17 | while not placed: 18 | direction = random.choice(['across', 'down']) 19 | if direction == 'across': 20 | x = random.randint(0, self.width - len(word)) 21 | y = random.randint(0, self.height - 1) 22 | if self.check_fit(word, x, y, 1, 0): 23 | self.place_word(word, x, y, 1, 0) 24 | placed = True 25 | else: 26 | x = random.randint(0, self.width - 1) 27 | y = random.randint(0, self.height - len(word)) 28 | if self.check_fit(word, x, y, 0, 1): 29 | self.place_word(word, x, y, 0, 1) 30 | placed = True 31 | 32 | def check_fit(self, word, x, y, dx, dy): 33 | for i in range(len(word)): 34 | if self.grid[y][x] not in [' ', word[i]]: 35 | return False 36 | x += dx 37 | y += dy 38 | return True 39 | 40 | def place_word(self, word, x, y, dx, dy): 41 | for i in range(len(word)): 42 | self.grid[y][x] = word[i] 43 | x += dx 44 | y += dy 45 | 46 | def display(self): 47 | for row in self.grid: 48 | print(' '.join(row)) 49 | 50 | 51 | def main(): 52 | crossword = Crossword(12, 12) 53 | words = ["PYTHON", "ALGORITHM", "PROGRAMMING", "COMPUTER", "LANGUAGE"] 54 | for word in words: 55 | crossword.add_word(word) 56 | crossword.display() 57 | 58 | if __name__ == "__main__": 59 | main() 60 | -------------------------------------------------------------------------------- /chatbot.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | # Dictionary containing responses for different user inputs 4 | responses = { 5 | "hi": ["Hello!", "Hi there!", "Hey!"], 6 | "how are you": ["I'm doing well, thank you!", "I'm great, thanks for asking!"], 7 | "what's your name": ["I'm a chatbot!", "You can call me ChatBot."], 8 | "what do you do": ["I'm here to chat with you!", "I'm a conversational agent."], 9 | "bye": ["Goodbye!", "See you later!", "Bye! Have a great day!"], 10 | "tell me a joke": ["Why don't scientists trust atoms? Because they make up everything!", 11 | "I'm not good at jokes, but here's one: Why was the math book sad? Because it had too many problems."], 12 | "who created you": ["I was created by a team of developers.", "My creators prefer to remain anonymous."], 13 | "what is the weather today": ["I'm sorry, I can't provide real-time information.", 14 | "You can check the weather on a weather website or app."], 15 | "how old are you": ["I don't have an age. I'm just a program!", "I exist in the realm of ones and zeros, so I don't age."], 16 | "what is the meaning of life": ["The meaning of life is a philosophical question that has puzzled humans for centuries.", 17 | "I think the meaning of life is subjective and varies from person to person."] 18 | } 19 | 20 | # Function to get response from the bot 21 | def get_response(user_input): 22 | if user_input.lower() in responses: 23 | return random.choice(responses[user_input.lower()]) 24 | else: 25 | return "I'm sorry, I don't understand that." 26 | 27 | # Main function to run the chatbot 28 | def main(): 29 | print("Welcome to the ChatBot!") 30 | print("You can start chatting. Type 'bye' to exit.") 31 | 32 | while True: 33 | user_input = input("You: ") 34 | if user_input.lower() == 'bye': 35 | print(get_response(user_input)) 36 | break 37 | else: 38 | print("ChatBot:", get_response(user_input)) 39 | 40 | if __name__ == "__main__": 41 | main() 42 | -------------------------------------------------------------------------------- /tictactoe.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | 4 | board = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '] 5 | player = 1 6 | 7 | # Win Flags 8 | Win = 1 9 | Draw = -1 10 | Running = 0 11 | Stop = 1 12 | 13 | Game = Running 14 | Mark = 'X' 15 | 16 | 17 | def DrawBoard(): 18 | print(" %c | %c | %c " % (board[1], board[2], board[3])) 19 | print("___|___|___") 20 | print(" %c | %c | %c " % (board[4], board[5], board[6])) 21 | print("___|___|___") 22 | print(" %c | %c | %c " % (board[7], board[8], board[9])) 23 | print(" | | ") 24 | 25 | 26 | def CheckPosition(x): 27 | if board[x] == ' ': 28 | return True 29 | else: 30 | return False 31 | 32 | 33 | def CheckWin(): 34 | global Game 35 | 36 | if board[1] == board[2] and board[2] == board[3] and board[1] != ' ': 37 | Game = Win 38 | elif board[4] == board[5] and board[5] == board[6] and board[4] != ' ': 39 | Game = Win 40 | elif board[7] == board[8] and board[8] == board[9] and board[7] != ' ': 41 | Game = Win 42 | elif board[1] == board[4] and board[4] == board[7] and board[1] != ' ': 43 | Game = Win 44 | elif board[2] == board[5] and board[5] == board[8] and board[2] != ' ': 45 | Game = Win 46 | elif board[3] == board[6] and board[6] == board[9] and board[3] != ' ': 47 | Game = Win 48 | elif board[1] == board[5] and board[5] == board[9] and board[5] != ' ': 49 | Game = Win 50 | elif board[3] == board[5] and board[5] == board[7] and board[5] != ' ': 51 | Game = Win 52 | elif board[1] != ' ' and board[2] != ' ' and board[3] != ' ' and \ 53 | board[4] != ' ' and board[5] != ' ' and board[6] != ' ' and \ 54 | board[7] != ' ' and board[8] != ' ' and board[9] != ' ': 55 | Game = Draw 56 | else: 57 | Game = Running 58 | 59 | 60 | print("Tic-Tac-Toe Game Designed By Sourabh Somani") 61 | print("Player 1 [X] --- Player 2 [O]\n") 62 | print() 63 | print() 64 | print("Please Wait...") 65 | time.sleep(3) 66 | 67 | while Game == Running: 68 | os.system('cls') 69 | DrawBoard() 70 | 71 | if player % 2 != 0: 72 | print("Player 1's chance") 73 | Mark = 'X' 74 | else: 75 | print("Player 2's chance") 76 | Mark = 'O' 77 | 78 | choice = int(input("Enter the position between [1-9] where you want to mark: ")) 79 | if CheckPosition(choice): 80 | board[choice] = Mark 81 | player += 1 82 | CheckWin() 83 | 84 | os.system('cls') 85 | DrawBoard() 86 | 87 | if Game == Draw: 88 | print("Game Draw") 89 | elif Game == Win: 90 | player -= 1 91 | if player % 2 != 0: 92 | print("Player 1 Won") 93 | else: 94 | print("Player 2 Won") --------------------------------------------------------------------------------