Question: I am trying to get this code to work based on Word Ladder for six-letter words (would prefer suggestions because I want to solve it

I am trying to get this code to work based on Word Ladder for six-letter words (would prefer suggestions because I want to solve it myself as well). No method headers can be changed. import math, random, time, heapq class PriorityQueue(): """Implementation of a priority queue  to store nodes during search."""   # TODO 1 : finish this class   # HINT look up/use the module heapq. def __init__(self): self.queue = [] self.current = 0 def next(self): if self.current >= len(self.queue): self.current raise StopIteration out = self.queue[self.current] self.current += 1 return out def pop(self): a = heapq.heappop(self.heap) return a def remove(self, nodeId): self[nodeId] = heapq.heappop(nodeId) return self def __iter__(self): return self def __str__(self): return 'PQ:[%s]' % (', '.join([str(i) for i in self.queue])) def append(self, node): self = Node(node) print(self) def __contains__(self, key): self.current = 0 return key in [n for v, n in self.queue] def __eq__(self, other): return self == other def size(self): return len(self.queue) def clear(self): self.queue = [] def top(self): return self.queue[0] __next__ = next def check_pq(): ''' check_pq is checking if your PriorityQueue  is completed or not'''  pq = PriorityQueue() temp_list = [] for i in range(10): a = random.randint(0, 10000) pq.append((a, 'a')) temp_list.append(a) temp_list = sorted(temp_list) for i in temp_list: j = pq.pop() if not i == j[0]: return False return True def generate_adjacents(current, word_list): ''' word_list is a set which has all words.  By comparing current and words in the word_list,  generate adjacents set and return it'''  adj_set = set() visited = set() for i in range(len(word_list)): for char in current.alpha: w = word_list[:i] + char + word_list[(i + 1):] if w not in visited and w in current.wordSet: adj_set.add(w) visited.add(w) return adj_set def dist_heuristic(v, goal): ''' v is the current node. Calculate the heuristic function  and then return a numeric value'''  # TODO 3: heuristic  count = 0 for i in range(0, len(v)): if v[i] != goal[i]: count += 1 return count def a_star(word_list, start, goal, heuristic = dist_heuristic): '''A* algorithm use the sum of cumulative path cost and the heuristic value for each loop  Update the cost and path if you find the lower-cost path in your process.  You may start from your BFS algorithm and expand to keep up the total cost while moving node to node.  '''  frontier = PriorityQueue() frontier.put(start, 0) original = {} currentcost = {} original[start] = None currentcost[start] = 0 while not frontier.empty(): start = frontier.get() if start == goal: break for next in generate_adjacents(start, word_list): new_cost = currentcost[start] + heuristic(start, next) if next not in currentcost or new_cost < currentcost[next]: currentcost[next] = new_cost priority = new_cost + heuristic(goal, next) frontier.put(next, priority) original[next] = start return original, currentcost def main(): word_list = set() file = open("words_6_longer.txt", "r") for word in file.readlines(): word_list.add(word.rstrip(' ')) file.close() initial = input("Type the starting word: ") goal = input("Type the goal word: ") cur_time = time.time() path_and_steps = (a_star(word_list, initial, goal)) if path_and_steps != None: print(path_and_steps) print("steps: ", len(path_and_steps)) print("Duration: ", time.time() - cur_time) else: print("There's no path") if __name__ == '__main__': main() '''Sample output 1 Type the starting word: listen Type the goal word: beaker ['listen', 'lister', 'bister', 'bitter', 'better', 'beater', 'beaker'] steps: 7 Duration: 0.000997304916381836 '''

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!