├── dfs.py ├── bfs.py ├── n queens2.py ├── hangman_art.py ├── hangman.py ├── nlp.py ├── bfs wjug2.py ├── wjug1.py └── hangman_words.py /dfs.py: -------------------------------------------------------------------------------- 1 | graph = { 2 | 'A' : ['B','C'], 3 | 'B' : ['D','E'], 4 | 'C' : ['F'], 5 | 'D' : [], 6 | 'E' : ['F'], 7 | 'F' : [], 8 | } 9 | visited = set() # set to keep track of visited nodes of graph. 10 | def dfs(visted, graph, node): 11 | #print(visited) #function for dfs 12 | if node not in visited: 13 | print (node) 14 | visited.add(node) 15 | for neighbour in graph[node]: 16 | dfs(visited,graph,neighbour) 17 | print("following is the Depth-First Search") 18 | dfs(visited, graph, 'A') 19 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /n queens2.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 | def N_Queens(self,i,j): 26 | for k in range(1,N+1): 27 | if self.placement(i,k): 28 | self.A[i] = k 29 | if i == N: 30 | self.printplacedqueen(N) 31 | else: 32 | self.N_Queens(i+1,N) 33 | 34 | N = int(input("enter the queens value")) 35 | obj = solution() 36 | obj.N_Queens(1,N) 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /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.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 if len(chosen_word) >=6 else 6 7 | lives=6 8 | print(f'pssst, the solution is {chosen_word}.') 9 | print(logo) 10 | print(len(stages)) 11 | display=[] 12 | guessed=[] 13 | for i in range(len(chosen_word)): 14 | 15 | display.append("_") 16 | while "_" in display and lives >=0: 17 | guess=input("guess a letter:").lower() 18 | if len(guess)==1 and guess.isalpha(): 19 | if guess in guessed: 20 | print("letter already present") 21 | continue 22 | guessed.append(guess) 23 | for i in range(len(chosen_word)): 24 | 25 | letter=chosen_word[i] 26 | 27 | if letter==guess: 28 | display[i]=letter 29 | if guess not in chosen_word: 30 | if lives > 0: 31 | lives=lives-1 32 | if lives==0: 33 | print(stages[lives]) 34 | print(f'the solution is {chosen_word}') 35 | print("you loose") 36 | exit(1) 37 | print(f"{' '.join(display)}") 38 | print(lives) 39 | print(stages[lives]) 40 | else: 41 | print("guess should be a character rather than a word") 42 | else: 43 | print("we have won") 44 | 45 | 46 | -------------------------------------------------------------------------------- /nlp.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | 4 | # In[2]: 5 | 6 | 7 | import nltk 8 | 9 | 10 | # In[31]: 11 | 12 | 13 | nltk.download('wordnet') 14 | nltk.download('averaged_perceptron_tagger_eng') 15 | nltk.download('stopwords') 16 | nltk.download('omw-1.4') 17 | nltk.download('punkt') 18 | text="""Hello Mr.Smith, How are you doing today? The weather is great and city is awesome.the sky is pinkish-blue. You should'nt eat cardboard""" 19 | 20 | 21 | # In[6]: 22 | 23 | 24 | sent=nltk.sent_tokenize(text) 25 | print(sent) 26 | 27 | 28 | # In[7]: 29 | 30 | 31 | import re 32 | #text="saaaaMMM" 33 | print(text.lower()) 34 | text = re.sub(r"[^a-z\'A-Z]", " ",text.lower()) 35 | text 36 | 37 | 38 | # In[9]: 39 | 40 | 41 | words= text.split() 42 | print(words) 43 | 44 | 45 | # In[11]: 46 | 47 | 48 | from nltk.corpus import stopwords 49 | print(stopwords.words("english")) 50 | 51 | 52 | # In[20]: 53 | 54 | 55 | words = [w for w in words if w not in stopwords.words("english")] 56 | print (words) 57 | 58 | 59 | # In[22]: 60 | 61 | 62 | from nltk.stem.porter import PorterStemmer 63 | stemmed = [PorterStemmer().stem(w) for w in words] 64 | print(stemmed) 65 | 66 | 67 | # In[25]: 68 | 69 | 70 | from nltk.stem.wordnet import WordNetLemmatizer 71 | lemmed = [WordNetLemmatizer().lemmatize(w) for w in words] 72 | print(lemmed) 73 | 74 | 75 | # In[37]: 76 | 77 | 78 | from nltk import pos_tag,RegexpParser 79 | 80 | tagged=pos_tag(lemmed) 81 | print(tagged) 82 | for i,(word, tag) in enumerate(tagged): 83 | if word == "weather": 84 | tagged[i] = (word, "NN") 85 | if word =="eat": 86 | tagged[i] = (word, "VB") 87 | chunker=RegexpParser(""" 88 | NP:{} 89 | P:{} 90 | V:{} 91 | PP:{} 92 | VP:{} 93 | """) 94 | output = chunker.parse(tagged) 95 | print("After extracting",output) 96 | 97 | 98 | # In[36]: 99 | 100 | 101 | nltk.download('averaged_perceptron_tagger') 102 | 103 | 104 | # In[ ]: 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /bfs wjug2.py: -------------------------------------------------------------------------------- 1 | import math 2 | from collections import deque 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 | if a<=0 or b <= 0: 11 | print("jug capacities must be positive.") 12 | exit(1) 13 | if ai < 0 or bi < 0 or af < 0 or bf < 0: 14 | print("negative values are not allowed.") 15 | exit(1) 16 | if ai==af and bi==bf: 17 | print(f"initial state is already the final state: juga{ai} and jugb={bi}") 18 | exit() 19 | def bfs_wjug(a,b,ai,bi,af,bf): 20 | visited = set() 21 | queue =deque([(ai,bi,[])]) 22 | while queue: 23 | curr_ai,curr_bi,operations = queue.popleft() 24 | if(curr_ai,curr_bi) in visited: 25 | continue 26 | visited.add((curr_ai,curr_bi)) 27 | if curr_ai == af and curr_bi == bf: 28 | for i,op in enumerate(operations): 29 | print(f"step{i+1}:{op}") 30 | print(f"final state reached: jug A = {curr_ai},jug B = {curr_bi}") 31 | return 32 | possible_operations = [ 33 | (a,curr_bi,"fill jug A"), 34 | (curr_ai, b,"fill jug B"), 35 | (0, curr_bi,"empty jug A"), 36 | (curr_ai, 0,"empty jug B"), 37 | (curr_ai - min(curr_ai,b - curr_bi),curr_bi + min(curr_ai, b- curr_bi),"pour from A to B"), 38 | (curr_ai + min(curr_bi, a - curr_ai),curr_bi - min(curr_bi, a - curr_ai),"pour from B to A"), 39 | ] 40 | for next_ai,next_bi,op in possible_operations: 41 | if (next_ai,next_bi) not in visited: 42 | queue.append((next_ai, next_bi, operations + [op])) 43 | print("no solution found.") 44 | return 45 | gcd = math.gcd(a,b) 46 | if (af <= a and bf <= b) and (af % gcd == bf % gcd == 0): 47 | bfs_wjug(a,b,ai,bi,af,bf) 48 | else: 49 | print("the final state is not achievable with the given capacities.") 50 | exit() 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /wjug1.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 | 82 | 83 | -------------------------------------------------------------------------------- /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 | --------------------------------------------------------------------------------