├── prgrm4.py ├── prgrm5.py ├── prgrm2.py ├── prgrm8.py ├── prgrm3.py ├── prgrm6.py └── prgrm1.py /prgrm4.py: -------------------------------------------------------------------------------- 1 | MAX, MIN = 1000, -1000 2 | 3 | def minimax(depth, nodeIndex, maximizingPlayer, 4 | values, alpha, beta): 5 | 6 | if depth == 3: 7 | return values[nodeIndex] 8 | 9 | if maximizingPlayer: 10 | 11 | best = MIN 12 | 13 | for i in range(0, 2): 14 | val = minimax(depth + 1, nodeIndex * 2 + i, False, values, alpha, beta) 15 | best = max(best, val) 16 | alpha = max(alpha, best) 17 | 18 | # Alpha Beta Pruning 19 | if beta <= alpha: 20 | break 21 | 22 | return best 23 | 24 | else: 25 | best = MAX 26 | 27 | for i in range(0, 2): 28 | val = minimax(depth + 1, nodeIndex * 2 + i, True, values, alpha, beta) 29 | best = min(best, val) 30 | beta = min(beta, best) 31 | 32 | # Alpha Beta Pruning 33 | if beta <= alpha: 34 | break 35 | 36 | return best 37 | 38 | # Driver Code 39 | if __name__ == "__main__": 40 | 41 | values = [10,9,14,18,5,4,50,3] 42 | print("The optimal value is :", minimax(0, 0, True, values, MIN, MAX)) 43 | -------------------------------------------------------------------------------- /prgrm5.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 | # Example fuzzy sets 24 | set1 = {'a': 0.8, 'b': 0.6, 'c': 0.4, 'd': 0.2, 'e': 0.1} 25 | set2 = {'a': 0.7, 'b': 0.5, 'c': 0.3, 'f': 0.9, 'g': 0.4} 26 | 27 | print("Fuzzy set 1:") 28 | display_fuzzy_set(set1) 29 | 30 | print("\nFuzzy set 2:") 31 | display_fuzzy_set(set2) 32 | 33 | print("\nUnion of the fuzzy sets:") 34 | union_set = fuzzy_union(set1, set2) 35 | display_fuzzy_set(union_set) 36 | 37 | print("\nIntersection of the fuzzy sets:") 38 | intersection_set = fuzzy_intersection(set1, set2) 39 | display_fuzzy_set(intersection_set) 40 | 41 | -------------------------------------------------------------------------------- /prgrm2.py: -------------------------------------------------------------------------------- 1 | def print_board(heap): 2 | print(f"Current heap: {'|' * heap}") 3 | 4 | def get_user_move(heap): 5 | while True: 6 | try: 7 | sticks_to_remove = int(input(f"Enter the number of sticks to remove (minimum 1, maximum {min(heap, heap // 2)}): ")) 8 | if 1 <= sticks_to_remove <= min(heap, heap // 2): 9 | break 10 | else: 11 | print(f"Invalid number of sticks. Please enter a number between 1 and {min(heap, heap // 2)}.") 12 | except ValueError: 13 | print("Invalid input. Please enter a valid number.") 14 | 15 | return sticks_to_remove 16 | 17 | def get_computer_move(heap): 18 | xor_sum = heap 19 | for i in range(heap): 20 | xor_sum ^= i 21 | 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 | 29 | return sticks_to_remove 30 | 31 | def nim_game(): 32 | heap = 16 33 | player_turn = 1 34 | 35 | while heap > 1: 36 | print_board(heap) 37 | 38 | if player_turn == 1: 39 | player_name = "Player 1" 40 | sticks_to_remove = get_user_move(heap) 41 | else: 42 | player_name = "Computer" 43 | sticks_to_remove = get_computer_move(heap) 44 | 45 | heap -= sticks_to_remove 46 | print(f"{player_name} removes {sticks_to_remove} sticks.") 47 | 48 | player_turn = 3 - player_turn # Switch player (1 -> 2, 2 -> 1) 49 | 50 | print_board(heap) 51 | winner = "Player 1" if player_turn == 2 else "Computer" 52 | print(f"Player {player_turn} picks the last stick ") 53 | print(f"\n{winner} is the winner!") 54 | 55 | if __name__ == "__main__": 56 | import random 57 | nim_game() 58 | -------------------------------------------------------------------------------- /prgrm8.py: -------------------------------------------------------------------------------- 1 | import random 2 | # Dictionary containing responses for different user inputs 3 | responses = { 4 | "hi": ["Hello!", "Hi there!", "Hey!"], 5 | "how are you": ["I'm doing well, thank you!", "I'm great, thanks for asking!"], 6 | "what's your name": ["I'm a chatbot!", "You can call me ChatBot."], 7 | "what do you do": ["I'm here to chat with you!", "I'm a conversational agent."], 8 | "bye": ["Goodbye!", "See you later!", "Bye! Have a great day!"], 9 | "tell me a joke": ["Why don't scientists trust atoms? Because they make up everything!", "I'm not good at jokes, but here's one: Why was the math book sad? Because it had too many problems."], 10 | "who created you": ["I was created by a team of developers.", "My creators prefer to remain anonymous."], 11 | "what is the weather today": ["I'm sorry, I can't provide real-time information.", "You can check the weather on a weather website or app."], 12 | "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."], 13 | "what is the meaning of life": ["The meaning of life is a philosophical question that has puzzled humans for centuries.", "I think the meaning of life is subjective and varies from person to person."] 14 | } 15 | 16 | # Function to get response from the bot 17 | def get_response(user_input): 18 | if user_input.lower() in responses: 19 | return random.choice(responses[user_input.lower()]) 20 | else: 21 | return "I'm sorry, I don't understand that." 22 | 23 | # Main function to run the chatbot 24 | def main(): 25 | print("Welcome to the ChatBot!") 26 | print("You can start chatting. Type 'bye' to exit.") 27 | while True: 28 | user_input = input("You: ") 29 | if user_input.lower() == 'bye': 30 | print(get_response(user_input)) 31 | break 32 | else: 33 | print("ChatBot:", get_response(user_input)) 34 | 35 | if __name__ == "__main__": 36 | main() 37 | -------------------------------------------------------------------------------- /prgrm3.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': 6, 11 | 'C': 99, 12 | 'D': 1, 13 | 'E': 7, 14 | 'G': 0, 15 | } 16 | return H_dist[n] 17 | 18 | while open_set: 19 | n = min(open_set, key=lambda x: g[x] + heuristic(x)) 20 | 21 | if n == stop_node or Graph_nodes.get(n) is None: 22 | pass 23 | else: 24 | for m, weight in Graph_nodes.get(n, []): 25 | if m not in open_set and m not in closed_set: 26 | open_set.add(m) 27 | parents[m] = n 28 | g[m] = g[n] + weight 29 | else: 30 | if g[m] > g[n] + weight: 31 | g[m] = g[n] + weight 32 | parents[m] = n 33 | if m in closed_set: 34 | closed_set.remove(m) 35 | open_set.add(m) 36 | if not n: 37 | print('Path does not exist!') 38 | return None 39 | if n == stop_node: 40 | path = [] 41 | while parents[n] != n: 42 | path.append(n) 43 | n = parents[n] 44 | path.append(start_node) 45 | path.reverse() 46 | return g[stop_node], path 47 | 48 | open_set.remove(n) 49 | closed_set.add(n) 50 | print('Path does not exist!') 51 | return None 52 | 53 | Graph_nodes = { 54 | 'A': [('B', 2), ('E', 3)], 55 | 'B': [('C', 1), ('G', 9)], 56 | 'C': None, 57 | 'E': [('D', 6)], 58 | 'D': [('G', 1)], 59 | } 60 | 61 | optimal_value_and_path = aStarAlgo('A', 'G') 62 | if optimal_value_and_path is not None: 63 | optimal_value, path = optimal_value_and_path 64 | print('Optimal value:', optimal_value) 65 | print('Path:', path) 66 | -------------------------------------------------------------------------------- /prgrm6.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 | 16 | self.words.append(word) 17 | placed = False 18 | 19 | while not placed: 20 | direction = random.choice(['across', 'down']) 21 | if direction == 'across': 22 | x = random.randint(0, self.width - len(word)) 23 | y = random.randint(0, self.height - 1) 24 | if self.check_fit(word, x, y, 1, 0): 25 | self.place_word(word, x, y, 1, 0) 26 | placed = True 27 | else: 28 | x = random.randint(0, self.width - 1) 29 | y = random.randint(0, self.height - len(word)) 30 | if self.check_fit(word, x, y, 0, 1): 31 | self.place_word(word, x, y, 0, 1) 32 | placed = True 33 | 34 | def check_fit(self, word, x, y, dx, dy): 35 | for i in range(len(word)): 36 | if self.grid[y][x] not in [' ', word[i]]: 37 | return False 38 | x += dx 39 | y += dy 40 | return True 41 | 42 | def place_word(self, word, x, y, dx, dy): 43 | for i in range(len(word)): 44 | self.grid[y][x] = word[i] 45 | x += dx 46 | y += dy 47 | 48 | def display(self): 49 | for row in self.grid: 50 | print(' '.join(row)) 51 | 52 | 53 | def main(): 54 | crossword = Crossword(12, 12) 55 | words = ["PYTHON", "ALGORITHM", "PROGRAMMING", "COMPUTER", "LANGUAGE"] 56 | for word in words: 57 | crossword.add_word(word) 58 | 59 | crossword.display() 60 | 61 | 62 | if __name__ == "__main__": 63 | main() 64 | 65 | -------------------------------------------------------------------------------- /prgrm1.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") --------------------------------------------------------------------------------