import copy import itertools import numpy as np import queue morse_code = {".-": "A", "-...": "B", "-.-.": "C", "-..": "D", ".": "E", "..-.": "F", "--.": "G", "....": "H", "..": "I", ".---": "J", "-.-": "K", ".-..": "L", "--": "M", "-.": "N", "---": "O", ".--.": "P", "--.-": "Q", ".-.": "R", "...": "S", "-": "T", "..-": "U", "...-": "V", ".--": "W", "-..-": "X", "-.--": "Y", "--..": "Z"} def morseDecode(inputStringList): """ This method should take a list of strings as input. Each string is equivalent to one letter (i.e. one morse code string). The entire list of strings represents a word. This method should convert the strings from morse code into english, and return the word as a string. """ # Please complete this method to perform the above described function str = "" for string in inputStringList: string = morse_code[string] str = str + string return str def morsePartialDecode(inputStringList): """ This method should take a list of strings as input. Each string is equivalent to one letter (i.e. one morse code string). The entire list of strings represents a word. However, the first character of every morse code string is unknown (represented by an 'x' (lowercase)) For example, if the word was originally TEST, then the morse code list string would normally be: ['-','.','...','-'] However, with the first characters missing, I would receive: ['x','x','x..','x'] With the x unknown, this word could be TEST, but it could also be EESE or ETSE or ETST or EEDT or other permutations. We define a valid words as one that exists within the dictionary file provided on the website, dictionary.txt When using this file, please always use the location './dictionary.txt' and place it in the same directory as the python script. This function should find and return a list of strings of all possible VALID words. """ dictionaryFileLoc = './dictionary.txt' # we need to find all possible morse code possible_chars = [] for string in inputStringList: string = string[1:] possible_char = [] for key in morse_code.keys(): temp = key[1:] if temp == string: possible_char.append(morse_code[key]) possible_chars.append(possible_char) # we need to find all possible combination words and check if the words in the txt file with open(dictionaryFileLoc, 'r') as f: lines = f.readlines() words = [] for item in itertools.product(*possible_chars): strr = "".join(item) for line in lines: if line.lower().strip() == strr.lower(): words.append(line.lower().strip()) return words class Maze: def __init__(self): """ Constructor - You may modify this, but please do not add any extra parameters """ self.maze = {} self.x = 0 self.y = 0 self.maze_complete = [] def addCoordinate(self, x, y, blockType): """ Add information about a coordinate on the maze grid x is the x coordinate y is the y coordinate blockType should be 0 (for an open space) of 1 (for a wall) """ if y not in self.maze.keys(): self.maze.setdefault(y, []) self.maze[y].append(x) self.x = max(x,self.x) self.y = max(y,self.y) pass def printMaze(self): """ Print out an ascii representation of the maze. A * indicates a wall and a empty space indicates an open space in the maze """ imcomplete_maze =[] for i in range(self.y+1): row = [] row_open = [] if i in self.maze.keys(): row_open = self.maze[i] else: pass for j in range(self.x+1): if j in row_open: row.append(" ") else: row.append("*") imcomplete_maze.append(row) row_len = len(imcomplete_maze[0]) self.y = row_len row = ["*"]*row_len row_start_0 = imcomplete_maze[0].count(" ") row_end_0=imcomplete_maze[-1].count(" ") if row_start_0 > 1: imcomplete_maze.insert(0, row) if row_end_0 > 1: imcomplete_maze.insert(self.y, row) maze_array = np.array(imcomplete_maze) column_len = len(maze_array[:, 0]) self.x = column_len column = np.array([["*"]*column_len]) column_start = maze_array[:,0] column_start_0 = column_start.tolist().count(" ") column_end = maze_array[:,-1] column_end_0 = column_end.tolist().count(" ") if column_start_0 >1: imcomplete_maze = np.insert(imcomplete_maze, 0, values=column, axis=1) if column_end_0 > 1: imcomplete_maze = np.column_stack((imcomplete_maze,column.T)) imcomplete_maze = imcomplete_maze.tolist() self.maze_complete = imcomplete_maze print(np.array(self.maze_complete)) # Please complete this method to perform the above described function pass def check_possible_go(self, position): can_go = [] # up x = position[0] y = position[1] if y+1 >= self.y: pass else: up = self.maze_complete[x][y + 1] if up == " ": can_go.append((x, y + 1)) # down if y-1<0: pass else: down = self.maze_complete[x][y - 1] if down == " ": can_go.append((x, y - 1)) # left if x-1<0: pass else: left = self.maze_complete[x - 1][y] if left == " ": can_go.append((x - 1, y)) # right if x+1>= self.x: pass else: right = self.maze_complete[x + 1][y] if right == " ": can_go.append((x + 1, y)) return can_go def findRoute(self, x1, y1, x2, y2): """ This method should find a route, traversing open spaces, from the coordinates (x1,y1) to (x2,y2) It should return the list of traversed coordinates followed along this route as a list of tuples (x,y), in the order in which the coordinates must be followed If no route is found, return an empty list """ start = (y1, x1) end = (y2, x2) # use another list to record the previous path at every position path = copy.deepcopy(self.maze_complete) q = queue.Queue() q.put(start) while 1: if q.empty() is True: return [] top = q.get() # if this position have been visited, then we pretend it as wall x = top[0] y = top[1] self.maze_complete[x][y] = "*" if top == end: break possible_go = self.check_possible_go(top) for i in possible_go: x_i = i[0] y_i = i[1] path[x_i][y_i] = top q.put(i) x = end[0] y = end[1] path_list = [] while 1: path_list.insert(0, (x, y)) if x == start[0] and y == start[1]: break node = path[x][y] x = node[0] y = node[1] list = [k[::-1] for k in path_list] return list def morseCodeTest(): """ This test program passes the morse code as a list of strings for the word HELLO to the decode method. It should receive a string "HELLO" in return. This is provided as a simple test example, but by no means covers all possibilities, and you should fulfill the methods as described in their comments. """ hello = ['....', '.', '.-..', '.-..', '---'] print(morseDecode(hello)) def partialMorseCodeTest(): """ This test program passes the partial morse code as a list of strings to the morsePartialDecode method. This is provided as a simple test example, but by no means covers all possibilities, and you should fulfill the methods as described in their comments. """ # This is a partial representation of the word TEST, amongst other possible combinations test = ['x', 'x', 'x..', 'x'] print(morsePartialDecode(test)) # This is a partial representation of the word DANCE, amongst other possible combinations dance = ['x..', 'x-', 'x.', 'x.-.', 'x'] print(morsePartialDecode(dance)) def mazeTest(): """ This sets the open space coordinates for the example maze in the assignment. The remainder of coordinates within the max bounds of these specified coordinates are assumed to be walls """ myMaze = Maze() myMaze.addCoordinate(1, 0, 0) # Start index myMaze.addCoordinate(1, 1, 0) myMaze.addCoordinate(1, 3, 0) myMaze.addCoordinate(1, 4, 0) myMaze.addCoordinate(1, 5, 0) myMaze.addCoordinate(1, 6, 0) myMaze.addCoordinate(1, 7, 0) myMaze.addCoordinate(2, 1, 0) myMaze.addCoordinate(2, 2, 0) myMaze.addCoordinate(2, 3, 0) myMaze.addCoordinate(2, 6, 0) myMaze.addCoordinate(3, 1, 0) myMaze.addCoordinate(3, 3, 0) myMaze.addCoordinate(3, 4, 0) myMaze.addCoordinate(3, 5, 0) myMaze.addCoordinate(3, 7, 0) myMaze.addCoordinate(3, 8, 0) # End index myMaze.addCoordinate(4, 1, 0) myMaze.addCoordinate(4, 5, 0) myMaze.addCoordinate(4, 7, 0) myMaze.addCoordinate(5, 1, 0) myMaze.addCoordinate(5, 2, 0) myMaze.addCoordinate(5, 3, 0) myMaze.addCoordinate(5, 5, 0) myMaze.addCoordinate(5, 6, 0) myMaze.addCoordinate(5, 7, 0) myMaze.addCoordinate(6, 3, 0) myMaze.addCoordinate(6, 5, 0) myMaze.addCoordinate(6, 7, 0) myMaze.addCoordinate(7, 1, 0) myMaze.addCoordinate(7, 2, 0) myMaze.addCoordinate(7, 3, 0) myMaze.addCoordinate(7, 5, 0) myMaze.addCoordinate(7, 7, 0) myMaze.printMaze() print(myMaze.findRoute(1, 0,3, 8)) # TODO: Test your findRoute method def main(): morseCodeTest() partialMorseCodeTest() mazeTest() if __name__ == "__main__": main()