├── A-STAR.py ├── Diabetes.py ├── MLPClassifier.py ├── Uninformed_BFS.py ├── WaterjugBFS.py ├── hangman.py ├── hangman_art.py ├── hangman_words.py ├── nqueens.py └── waterjug.py /A-STAR.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | 7 | graph=[['A','B',1,3], 8 | ['A','C',2,4], 9 | ['A','H',7,0], 10 | ['B','D',4,2], 11 | ['B','E',6,6], 12 | ['C','F',3,3], 13 | ['C','G',2,1], 14 | ['D','E',7,6], 15 | ['D','H',5,0], 16 | ['F','H',1,0], 17 | ['G','H',2, 0]] 18 | 19 | 20 | # In[2]: 21 | 22 | 23 | temp=[] 24 | temp1=[] 25 | for i in graph: 26 | temp.append(i[0]) 27 | temp1.append(i[1]) 28 | 29 | 30 | # In[3]: 31 | 32 | 33 | print(temp) 34 | 35 | 36 | # In[4]: 37 | 38 | 39 | print(temp1) 40 | 41 | 42 | # In[5]: 43 | 44 | 45 | nodes = set(temp).union(set(temp1)) 46 | 47 | 48 | # In[6]: 49 | 50 | 51 | nodes 52 | 53 | 54 | # In[17]: 55 | 56 | 57 | def A_star(graph, costs, open, closed, cur_node): 58 | if cur_node in open: 59 | open.remove(cur_node) 60 | closed.add(cur_node) 61 | for i in graph: 62 | print(costs) 63 | if(i[0] == cur_node and costs[i[0]]+i[2]+i[3] < costs[i[1]]): 64 | open.add(i[1]) 65 | costs[i[1]] = costs[i[0]]+i[2]+i[3] 66 | path[i[1]] = path[i[0]] + ' -> ' + i[1] 67 | costs[cur_node] = 999999 68 | small = min(costs, key=costs.get) 69 | if small not in closed: 70 | A_star(graph, costs, open,closed, small) 71 | costs = dict() 72 | temp_cost = dict() 73 | path = dict() 74 | for i in nodes: 75 | costs[i] = 999999 76 | path[i] = ' ' 77 | 78 | 79 | # In[18]: 80 | 81 | 82 | open = set() 83 | closed = set() 84 | start_node = input("Enter the Start Node: ") 85 | open.add(start_node) 86 | 87 | 88 | # In[19]: 89 | 90 | 91 | path[start_node] = start_node 92 | costs[start_node] = 0 93 | 94 | 95 | # In[20]: 96 | 97 | 98 | A_star(graph, costs, open, closed, start_node) 99 | goal_node = input("Enter the Goal Node: ") 100 | print("Path with least cost is: ",path[goal_node]) 101 | 102 | 103 | # In[ ]: 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /Diabetes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[3]: 5 | 6 | 7 | import pandas as pd 8 | 9 | 10 | # In[2]: 11 | 12 | 13 | pip install pandas 14 | 15 | 16 | # In[4]: 17 | 18 | 19 | df=pd.read_csv("diabetes.csv") 20 | 21 | 22 | # In[5]: 23 | 24 | 25 | df.head() 26 | 27 | 28 | # In[6]: 29 | 30 | 31 | df.shape 32 | 33 | 34 | # In[7]: 35 | 36 | 37 | df.isnull().sum() 38 | 39 | 40 | # In[8]: 41 | 42 | 43 | X=df.iloc[:,:-1].to_numpy() 44 | y=df.iloc[:,-1].to_numpy() 45 | 46 | 47 | # In[9]: 48 | 49 | 50 | df.iloc[:,:-1] 51 | 52 | 53 | # In[10]: 54 | 55 | 56 | X 57 | 58 | 59 | # In[11]: 60 | 61 | 62 | y 63 | 64 | 65 | # In[12]: 66 | 67 | 68 | X=df.iloc[:,:-1].to_numpy() 69 | y=df.iloc[:,-1].to_numpy() 70 | 71 | 72 | # In[18]: 73 | 74 | 75 | from sklearn.model_selection import train_test_split 76 | X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,random_state=0) 77 | 78 | 79 | # In[19]: 80 | 81 | 82 | from sklearn.tree import DecisionTreeClassifier 83 | clf=DecisionTreeClassifier(criterion="entropy",random_state=0) 84 | clf.fit(X_train,y_train) 85 | 86 | 87 | # In[24]: 88 | 89 | 90 | import matplotlib.pyplot as plt 91 | get_ipython().run_line_magic('matplotlib', 'inline') 92 | from sklearn.tree import plot_tree 93 | plt.figure(figsize=(20,10)) 94 | 95 | 96 | # In[21]: 97 | 98 | 99 | pip install matplotlib 100 | 101 | 102 | # In[32]: 103 | 104 | 105 | plot_tree(clf,feature_names=['Glucose','BMI'],class_names=['No','Yes']) 106 | plt.show() 107 | 108 | 109 | # In[36]: 110 | 111 | 112 | clf.set_params(max_depth=2) 113 | 114 | 115 | # In[33]: 116 | 117 | 118 | clf.fit(X_train,y_train) 119 | 120 | 121 | # In[37]: 122 | 123 | 124 | clf.fit(X_train,y_train) 125 | plt.figure(figsize=(20,10)) 126 | plot_tree(clf,feature_names=['Glucose','BMI'],class_names=['No','Yes']) 127 | plt.show() 128 | 129 | 130 | # In[38]: 131 | 132 | 133 | predictions=clf.predict(X_test) 134 | 135 | 136 | # In[39]: 137 | 138 | 139 | predictions 140 | 141 | 142 | # In[40]: 143 | 144 | 145 | clf.predict([[23,20],[200,35]]) 146 | 147 | 148 | # In[42]: 149 | 150 | 151 | from sklearn import metrics 152 | cf=metrics.confusion_matrix(y_test,predictions) 153 | cf 154 | 155 | 156 | # In[ ]: 157 | 158 | 159 | 160 | 161 | -------------------------------------------------------------------------------- /MLPClassifier.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[4]: 5 | 6 | 7 | import numpy as np 8 | import pandas as pd 9 | data=pd.read_csv('HR_comma_sep.csv') 10 | data.head() 11 | 12 | 13 | # In[3]: 14 | 15 | 16 | get_ipython().system('pip install pandas') 17 | 18 | 19 | # In[2]: 20 | 21 | 22 | data.info() 23 | 24 | 25 | # In[6]: 26 | 27 | 28 | data['Departments'].value_counts() 29 | 30 | 31 | # In[7]: 32 | 33 | 34 | data['Departments'].unique() 35 | 36 | 37 | # In[8]: 38 | 39 | 40 | data['salary'].unique() 41 | 42 | 43 | # In[12]: 44 | 45 | 46 | from sklearn import preprocessing 47 | le = preprocessing.LabelEncoder() 48 | print(le) 49 | data['salary']=le.fit_transform(data['salary']) 50 | data['Departments']=le.fit_transform(data['Departments']) 51 | 52 | 53 | # In[13]: 54 | 55 | 56 | data['salary'].unique() 57 | 58 | 59 | # In[14]: 60 | 61 | 62 | X=data.iloc[:,:-1] 63 | X 64 | 65 | 66 | # In[19]: 67 | 68 | 69 | X=data[['satisfaction_level', 'last_evaluation', 'number_project', 'average_montly_hours', 'time_spend_company', 'Work_accident', 'promotion_last_5years', 'Departments', 'salary']] 70 | y=data['left'] 71 | from sklearn.model_selection import train_test_split 72 | X_train, X_test, y_train, y_test=train_test_split(X, y, test_size=0.3, random_state=42) 73 | 74 | 75 | # In[20]: 76 | 77 | 78 | X_train 79 | 80 | 81 | # In[21]: 82 | 83 | 84 | y_train 85 | 86 | 87 | # In[22]: 88 | 89 | 90 | from sklearn.neural_network import MLPClassifier 91 | 92 | clf = MLPClassifier(hidden_layer_sizes=(6,5), 93 | random_state=5, 94 | verbose=True, 95 | learning_rate_init=0.01) 96 | 97 | clf.fit(X_train,y_train) 98 | 99 | 100 | # In[23]: 101 | 102 | 103 | ypred=clf.predict(X_test) 104 | from sklearn.metrics import accuracy_score 105 | accuracy_score(y_test,ypred) 106 | 107 | 108 | # In[24]: 109 | 110 | 111 | X_test.shape 112 | 113 | 114 | # In[25]: 115 | 116 | 117 | from sklearn.metrics import classification_report 118 | print(classification_report(y_test, ypred)) 119 | 120 | 121 | # In[27]: 122 | 123 | 124 | X_test.shape 125 | 126 | 127 | # In[28]: 128 | 129 | 130 | n=pd.DataFrame({ 131 | 'satisfaction_level': [0.78], 132 | 'last_evaluation': [0.53], 133 | 'number_project': [2], 134 | 'average_montly_hours': [157], 135 | 'time_spend_company': [3], 136 | 'Work_accident': [0], 137 | 'promotion_last_5years': [0], 138 | 'Departments': [1], 139 | 'salary': [1] 140 | }) 141 | 142 | 143 | # In[29]: 144 | 145 | 146 | new_data=clf.predict(n) 147 | print(new_data) 148 | 149 | 150 | # In[30]: 151 | 152 | 153 | from sklearn.metrics import classification_report 154 | print(classification_report(y_test, ypred)) 155 | 156 | 157 | # In[31]: 158 | 159 | 160 | from sklearn.metrics import confusion_matrix 161 | y_pred = clf.predict(X_test) 162 | print(confusion_matrix(y_test, y_pred)) 163 | 164 | 165 | # In[32]: 166 | 167 | 168 | print(y_train.value_counts()) 169 | print(y_test.value_counts()) 170 | 171 | 172 | # In[ ]: 173 | 174 | 175 | 176 | 177 | -------------------------------------------------------------------------------- /Uninformed_BFS.py: -------------------------------------------------------------------------------- 1 | graph={ 2 | 'P':['Q','R','S'], 3 | 'Q':['P','R'], 4 | 'R':['P','Q','T'], 5 | 'T':['R'], 6 | 'S':['P'] 7 | } 8 | visited=[] 9 | queue=[] 10 | def bfs(visited,graph,node): 11 | visited.append(node) 12 | queue.append(node) 13 | while queue: 14 | m=queue.pop(0) 15 | print(m,end=" ") 16 | for neighbour in graph[m]: 17 | if neighbour not in visited: 18 | visited.append(neighbour) 19 | queue.append(neighbour) 20 | print("following is the breadth first search") 21 | bfs(visited,graph,'P') 22 | -------------------------------------------------------------------------------- /WaterjugBFS.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import deque 3 | 4 | a = int(input("Enter Jug A Capacity: ")) 5 | b = int(input("Enter Jug B Capacity: ")) 6 | ai = int(input("Initially Water in Jug A: ")) 7 | bi = int(input("Initially Water in Jug B: ")) 8 | af = int(input("Final State of Jug A: ")) 9 | bf = int(input("Final State of Jug B: ")) 10 | 11 | 12 | if a <= 0 or b <= 0: 13 | print("Jug capacities must be positive.") 14 | exit(1) 15 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 16 | print("Negative values are not allowed.") 17 | exit(1) 18 | if ai==af and bi==bf: 19 | print(f"initial state is already the final state: juga{ai} and jugb={bi}") 20 | exit() 21 | 22 | def bfs_wjug(a, b, ai, bi, af, bf): 23 | visited = set() 24 | queue = deque([(ai, bi, [])]) 25 | 26 | while queue: 27 | curr_ai, curr_bi, operations = queue.popleft() 28 | 29 | if (curr_ai, curr_bi) in visited: 30 | continue 31 | visited.add((curr_ai, curr_bi)) 32 | 33 | 34 | if curr_ai == af and curr_bi == bf: 35 | for i, op in enumerate(operations): 36 | print(f"Step {i + 1}: {op}") 37 | print(f"Final State Reached: Jug A = {curr_ai}, Jug B = {curr_bi}") 38 | return 39 | 40 | 41 | possible_operations = [ 42 | (a, curr_bi, "Fill Jug A"), 43 | (curr_ai, b, "Fill Jug B"), 44 | (0, curr_bi, "Empty Jug A"), 45 | (curr_ai, 0, "Empty Jug B"), 46 | (curr_ai - min(curr_ai, b - curr_bi), curr_bi + min(curr_ai, b - curr_bi), "Pour from A to B"), 47 | (curr_ai + min(curr_bi, a - curr_ai), curr_bi - min(curr_bi, a - curr_ai), "Pour from B to A"), 48 | ] 49 | 50 | for next_ai, next_bi, op in possible_operations: 51 | if (next_ai, next_bi) not in visited: 52 | queue.append((next_ai, next_bi, operations + [op])) 53 | 54 | print("No solution found.") 55 | return 56 | 57 | gcd = math.gcd(a, b) 58 | 59 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 60 | bfs_wjug(a, b, ai, bi, af, bf) 61 | else: 62 | print("The final state is not achievable with the given capacities.") 63 | exit() 64 | -------------------------------------------------------------------------------- /hangman.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | from hangman_words import word_list 4 | from hangman_art import stages, logo 5 | chosen_word=random.choice(word_list) 6 | lives=6 7 | print(f'Psst, the solution is {chosen_word}.') 8 | print(logo) 9 | print(len(stages)) 10 | display=[] 11 | guesses=[] 12 | for i in range(len(chosen_word)): 13 | display.append("_") 14 | while "_" in display and lives >0: 15 | guess=input("Guess a letter:").lower() 16 | if len(guess)==1 and guess.isalpha(): 17 | if guess in guessed: 18 | print("letter already printed") 19 | continue 20 | guessed.append(guess) 21 | for i in range(len(chosen_word)): 22 | letter = chosen_word[i] 23 | if letter==guess: 24 | display[i]=letter 25 | if guess not in chosen_word: 26 | if lives>0: 27 | lives = lives-1 28 | if lives==0: 29 | print(stages[lives]) 30 | print(f'the solution is {chosen_word}.') 31 | print("You loose") 32 | exit(1) 33 | print(f"{' '.join(display)}") 34 | print(lives) 35 | print(stages[lives]) 36 | else: 37 | print("guess should be a character rather than a word") 38 | else: 39 | print("you have won") 40 | -------------------------------------------------------------------------------- /hangman_art.py: -------------------------------------------------------------------------------- 1 | stages = [''' 2 | +---+ 3 | | | 4 | O | 5 | /|\ | 6 | / \ | 7 | | 8 | ========= 9 | ''', ''' 10 | +---+ 11 | | | 12 | O | 13 | /|\ | 14 | / | 15 | | 16 | ========= 17 | ''', ''' 18 | +---+ 19 | | | 20 | O | 21 | /|\ | 22 | | 23 | | 24 | ========= 25 | ''', ''' 26 | +---+ 27 | | | 28 | O | 29 | /| | 30 | | 31 | | 32 | =========''', ''' 33 | +---+ 34 | | | 35 | O | 36 | | | 37 | | 38 | | 39 | ========= 40 | ''', ''' 41 | +---+ 42 | | | 43 | O | 44 | | 45 | | 46 | | 47 | ========= 48 | ''', ''' 49 | +---+ 50 | | | 51 | | 52 | | 53 | | 54 | | 55 | ========= 56 | '''] 57 | 58 | logo = ''' 59 | _ 60 | | | 61 | | |__ __ _ _ __ __ _ _ __ ___ __ _ _ __ 62 | | '_ \ / _` | '_ \ / _` | '_ ` _ \ / _` | '_ \ 63 | | | | | (_| | | | | (_| | | | | | | (_| | | | | 64 | |_| |_|\__,_|_| |_|\__, |_| |_| |_|\__,_|_| |_| 65 | __/ | 66 | |___/ ''' 67 | 68 | 69 | -------------------------------------------------------------------------------- /hangman_words.py: -------------------------------------------------------------------------------- 1 | word_list = [ 2 | 'abruptly', 3 | 'absurd', 4 | 'abyss', 5 | 'affix', 6 | 'askew', 7 | 'avenue', 8 | 'awkward', 9 | 'axiom', 10 | 'azure', 11 | 'bagpipes', 12 | 'bandwagon', 13 | 'banjo', 14 | 'bayou', 15 | 'beekeeper', 16 | 'blitz', 17 | 'blizzard', 18 | 'boggle', 19 | 'bookworm', 20 | 'boxcar', 21 | 'boxful', 22 | 'buckaroo', 23 | 'buffalo', 24 | 'buffoon', 25 | 'buxom', 26 | 'buzzard', 27 | 'buzzing', 28 | 'buzzwords', 29 | 'caliph', 30 | 'cobweb', 31 | 'cockiness', 32 | 'croquet', 33 | 'crypt', 34 | 'curacao', 35 | 'cycle', 36 | 'daiquiri', 37 | 'dirndl', 38 | 'disavow', 39 | 'dizzying', 40 | 'duplex', 41 | 'dwarves', 42 | 'embezzle', 43 | 'equip', 44 | 'espionage', 45 | 'euouae', 46 | 'exodus', 47 | 'faking', 48 | 'fishhook', 49 | 'fixable', 50 | 'fjord', 51 | 'flapjack', 52 | 'flopping', 53 | 'fluffiness', 54 | 'flyby', 55 | 'foxglove', 56 | 'frazzled', 57 | 'frizzled', 58 | 'fuchsia', 59 | 'funny', 60 | 'gabby', 61 | 'galaxy', 62 | 'galvanize', 63 | 'gazebo', 64 | 'giaour', 65 | 'gizmo', 66 | 'glowworm', 67 | 'glyph', 68 | 'gnarly', 69 | 'gnostic', 70 | 'gossip', 71 | 'grogginess', 72 | 'haiku', 73 | 'haphazard', 74 | 'hyphen', 75 | 'iatrogenic', 76 | 'icebox', 77 | 'injury', 78 | 'ivory', 79 | 'ivy', 80 | 'jackpot', 81 | 'jaundice', 82 | 'jawbreaker', 83 | 'jaywalk', 84 | 'jazziest', 85 | 'jazzy', 86 | 'jelly', 87 | 'jigsaw', 88 | 'jinx', 89 | 'jiujitsu', 90 | 'jockey', 91 | 'jogging', 92 | 'joking', 93 | 'jovial', 94 | 'joyful', 95 | 'juicy', 96 | 'jukebox', 97 | 'jumbo', 98 | 'kayak', 99 | 'kazoo', 100 | 'keyhole', 101 | 'khaki', 102 | 'kilobyte', 103 | 'kiosk', 104 | 'kitsch', 105 | 'kiwifruit', 106 | 'klutz', 107 | 'knapsack', 108 | 'larynx', 109 | 'lengths', 110 | 'lucky', 111 | 'luxury', 112 | 'lymph', 113 | 'marquis', 114 | 'matrix', 115 | 'megahertz', 116 | 'microwave', 117 | 'mnemonic', 118 | 'mystify', 119 | 'naphtha', 120 | 'nightclub', 121 | 'nowadays', 122 | 'numbskull', 123 | 'nymph', 124 | 'onyx', 125 | 'ovary', 126 | 'oxidize', 127 | 'oxygen', 128 | 'pajama', 129 | 'peekaboo', 130 | 'phlegm', 131 | 'pixel', 132 | 'pizazz', 133 | 'pneumonia', 134 | 'polka', 135 | 'pshaw', 136 | 'psyche', 137 | 'puppy', 138 | 'puzzling', 139 | 'quartz', 140 | 'queue', 141 | 'quips', 142 | 'quixotic', 143 | 'quiz', 144 | 'quizzes', 145 | 'quorum', 146 | 'razzmatazz', 147 | 'rhubarb', 148 | 'rhythm', 149 | 'rickshaw', 150 | 'schnapps', 151 | 'scratch', 152 | 'shiv', 153 | 'snazzy', 154 | 'sphinx', 155 | 'spritz', 156 | 'squawk', 157 | 'staff', 158 | 'strength', 159 | 'strengths', 160 | 'stretch', 161 | 'stronghold', 162 | 'stymied', 163 | 'subway', 164 | 'swivel', 165 | 'syndrome', 166 | 'thriftless', 167 | 'thumbscrew', 168 | 'topaz', 169 | 'transcript', 170 | 'transgress', 171 | 'transplant', 172 | 'triphthong', 173 | 'twelfth', 174 | 'twelfths', 175 | 'unknown', 176 | 'unworthy', 177 | 'unzip', 178 | 'uptown', 179 | 'vaporize', 180 | 'vixen', 181 | 'vodka', 182 | 'voodoo', 183 | 'vortex', 184 | 'voyeurism', 185 | 'walkway', 186 | 'waltz', 187 | 'wave', 188 | 'wavy', 189 | 'waxy', 190 | 'wellspring', 191 | 'wheezy', 192 | 'whiskey', 193 | 'whizzing', 194 | 'whomever', 195 | 'wimpy', 196 | 'witchcraft', 197 | 'wizard', 198 | 'woozy', 199 | 'wristwatch', 200 | 'wyvern', 201 | 'xylophone', 202 | 'yachtsman', 203 | 'yippee', 204 | 'yoked', 205 | 'youthful', 206 | 'yummy', 207 | 'zephyr', 208 | 'zigzag', 209 | 'zigzagging', 210 | 'zilch', 211 | 'zipper', 212 | 'zodiac', 213 | 'zombie', 214 | ] 215 | -------------------------------------------------------------------------------- /nqueens.py: -------------------------------------------------------------------------------- 1 | class solution: 2 | def __init__(self): 3 | self.MAX = 20 4 | self.A = [0]*self.MAX 5 | 6 | def placement(self,i,j): 7 | for k in range(1,i): 8 | if (self.A[k] == j) or abs(self.A[k] - j) == abs(k - i): 9 | return False 10 | print(self.A) 11 | return True 12 | 13 | def printplacedqueen(self,N): 14 | print('Arrangement--->') 15 | print() 16 | 17 | for i in range(1,N+1): 18 | for j in range(1,N+1): 19 | if self.A[i] != j: 20 | print('\t_',end =' ') 21 | else: 22 | print('\tQ',end =' ') 23 | print() 24 | print() 25 | 26 | def N_Queens(self,i,j): 27 | for k in range(1,N+1): 28 | if self.placement(i,k): 29 | self.A[i] = k 30 | if i == N: 31 | self.printplacedqueen(N) 32 | else: 33 | self.N_Queens(i+1,N) 34 | 35 | 36 | N = int(input("enter the queens value")) 37 | obj = solution() 38 | obj.N_Queens(1,N) 39 | 40 | -------------------------------------------------------------------------------- /waterjug.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # Input capacities and initial/final states for jugs 4 | a = int(input("Enter Jug A Capacity: ")) 5 | b = int(input("Enter Jug B Capacity: ")) 6 | ai = int(input("Initially Water in Jug A: ")) 7 | bi = int(input("Initially Water in Jug B: ")) 8 | af = int(input("Final State of Jug A: ")) 9 | bf = int(input("Final State of Jug B: ")) 10 | 11 | # Check for negative values 12 | if a <= 0 or b <= 0: 13 | print("Jug capacities must be positive.") 14 | exit(1) 15 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 16 | print("Negative values are not allowed.") 17 | exit(1) 18 | 19 | # Define the water jug solver function 20 | def wjug(a, b, ai, bi, af, bf): 21 | print("List Of Operations You Can Do:\n") 22 | print("1. Fill Jug A Completely") 23 | print("2. Fill Jug B Completely") 24 | print("3. Empty Jug A Completely") 25 | print("4. Empty Jug B Completely") 26 | print("5. Pour From Jug A till Jug B is full or A becomes empty") 27 | print("6. Pour From Jug B till Jug A is full or B becomes empty") 28 | print("7. Pour all from Jug B to Jug A") 29 | print("8. Pour all from Jug A to Jug B") 30 | 31 | # Loop until the final state is reached 32 | while ai != af or bi != bf: 33 | op = int(input("Enter the Operation (1-8): ")) 34 | 35 | if op == 1: # Fill Jug A completely 36 | ai = a 37 | elif op == 2: # Fill Jug B completely 38 | bi = b 39 | elif op == 3: # Empty Jug A 40 | ai = 0 41 | elif op == 4: # Empty Jug B 42 | bi = 0 43 | elif op == 5: # Pour from A to B 44 | pour_amount = min(ai, b - bi) 45 | ai -= pour_amount 46 | bi += pour_amount 47 | elif op == 6: # Pour from B to A 48 | pour_amount = min(bi, a - ai) 49 | bi -= pour_amount 50 | ai += pour_amount 51 | elif op == 7: # Pour all from B to A 52 | pour_amount = min(bi, a - ai) 53 | ai += pour_amount 54 | bi -= pour_amount 55 | elif op == 8: # Pour all from A to B 56 | pour_amount = min(ai, b - bi) 57 | bi += pour_amount 58 | ai -= pour_amount 59 | else: 60 | print("Invalid operation. Please choose a number between 1 and 8.") 61 | continue 62 | 63 | print(f"Jug A: {ai}, Jug B: {bi}") 64 | 65 | if ai == af and bi == bf: 66 | print("Final State Reached: Jug A =", ai, ", Jug B =", bi) 67 | return 68 | 69 | 70 | 71 | print("Final State Reached: Jug A =", ai, ", Jug B =", bi) 72 | 73 | # Check if the final state is achievable using GCD 74 | gcd = math.gcd(a, b) 75 | 76 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 77 | wjug(a, b, ai, bi, af, bf) 78 | else: 79 | print("The final state is not achievable with the given capacities.") 80 | exit(1) 81 | --------------------------------------------------------------------------------