def test_getVertex_not_found(self): vertices = [1,2,3] graph = ArrayGraph(vertices) assert len(graph.vertices) is 3 assert graph.size is 3 assert graph.getVertex(2) == graph.vertices[1] assert graph.getVertex(4) == None
def test_addVertex(self): vertices = [] graph = ArrayGraph() graph.addVertex("1") graph.addVertex("2") assert graph.size is 2 assert len(graph.vertices) == 2
def test_get_neighbors(self): vertices = [1,2,3] graph = ArrayGraph(vertices) graph.addEdge(1,2,3) graph.addEdge(2,3,1) assert graph.size is 3 assert graph.vertices[0].edges[0] == (graph.vertices[1], 3) assert graph.getNeighbors(1) == [(graph.vertices[1], 3)]
def test_addEdge(self): vertices = [1,2,3] graph = ArrayGraph(vertices) graph.addEdge(1,2,3) graph.addEdge(2,3,1) assert len(graph.vertices[0].edges) is 1 assert len(graph.vertices[1].edges) is 1 assert len(graph.vertices[2].edges) is 0 assert graph.vertices[0].edges[0] == (graph.vertices[1], 3) assert graph.vertices[1].edges[0] == (graph.vertices[2], 1)
def test_clique(self): graph = ArrayGraph([1,2,3,4]) graph.addEdge(1,2) graph.addEdge(1,3) graph.addEdge(2,3) graph.addEdge(3,4) assert len(graph.clique(1)) == 3 assert graph.vertices[0] in graph.clique(1) assert graph.vertices[1] in graph.clique(1) assert graph.vertices[2] in graph.clique(1) assert graph.vertices[3] not in graph.clique(1)
def test_find_path_broken(self): vertices = [1,2,3] graph = ArrayGraph(vertices) graph.addEdge(1,2) graph.addEdge(2,3,1) assert graph.find_path(1, 3) == [graph.vertices[0],graph.vertices[1],graph.vertices[2]] with self.assertRaises(ValueError): graph.addEdge(3,4)
def read_text(file): file = open(file, "r") counter = 0 data_struct = None temp_verices = [] temp_edges = [] for line in file: if counter is 0: # First line if line.strip().upper() == "G" or line.strip().upper() == "D": data_struct = ArrayGraph() # elif line.upper() == "D": elif counter is 1: # Second line temp_verices = line.strip().split(',') for vert in temp_verices: data_struct.addVertex(vert) else: # All the edges tuple = eval(line.strip()) temp_edges.append(tuple) if len(tuple) == 3: # If it has weight data_struct.addEdge(str(tuple[0]), str(tuple[1]), tuple[2]) else: data_struct.addEdge(str(tuple[0]), str(tuple[1])) counter += 1 return data_struct
def test_find_shortest_path(self): vertices = [1,2,3,4,5] graph = ArrayGraph(vertices) graph.addEdge(1,2) graph.addEdge(1,4) graph.addEdge(2,3) graph.addEdge(2,4) graph.addEdge(2,5) graph.addEdge(3,5) path = graph.find_shortest_path(1, 5) assert path == [graph.vertices[0], graph.vertices[1], graph.vertices[4]] assert len(path) == 3
def __init__(self, champ_file, class_file): """Initializing the Team Fight Tactics Team Picker based on the game data stored in JSON files.""" self.champions = self.create_champ_list(champ_file) # O(n) self.classes, self.class_bonus, self.max_bonus, self.classes_dict = self.create_classes_list(class_file) # O(n) * O(3) self.champions_in_class = class_dict = {'demon': ['aatrox', 'brand', 'elise', 'evelynn', 'morgana', 'swain', 'varus'], 'dragon': ['aurelionsol', 'shyvana'], 'exile': ['yasuo'], 'glacial': ['anivia', 'ashe', 'braum', 'lissandra', 'sejuani', 'volibear'], 'imperial': ['darius', 'draven', 'katarina', 'swain'], 'noble': ['fiora', 'garen', 'kayle', 'leona', 'lucian', 'vayne'], 'ninja': ['akali', 'kennen', 'shen', 'zed'], 'pirate': ['gangplank', 'graves', 'missfortune', 'pyke', 'twistedfate'], 'phantom': ['karthus', 'kindred', 'mordekaiser'], 'robot': ['blitzcrank'], 'void': ['chogath', 'kassadin', 'khazix', 'reksai'], 'wild': ['ahri', 'gnar', 'nidalee', 'rengar', 'warwick'], 'yordle': ['gnar', 'kennen', 'lulu', 'poppy', 'tristana', 'veigar'], 'assassin': ['akali', 'evelynn', 'katarina', 'khazix', 'pyke', 'rengar', 'zed'], 'blademaster': ['aatrox', 'draven', 'fiora', 'gangplank', 'shen', 'yasuo'], 'brawler': ['blitzcrank', 'chogath', 'reksai', 'volibear', 'warwick'], 'elementalist': ['anivia', 'brand', 'kennen', 'lissandra'], 'guardian': ['braum', 'leona'], 'gunslinger': ['gangplank', 'graves', 'lucian', 'missfortune', 'tristana'], 'knight': ['darius', 'garen', 'kayle', 'mordekaiser', 'poppy', 'sejuani'], 'ranger': ['ashe', 'kindred', 'varus', 'vayne'], 'shapeshifter': ['elise', 'gnar', 'nidalee', 'shyvana', 'swain'], 'sorcerer': ['ahri', 'aurelionsol', 'karthus', 'kassadin', 'lulu', 'morgana', 'twistedfate', 'veigar']} self.graph = ArrayGraph(self.champions) self._add_edges() self.first_champ = input("First Champ:").lower() # User inputs key for first champion self.first_champ_class_selection = self.graph.find_all_champs_same_class_as(self.first_champ) # O(6273) { 'yordle': set('kennen', ...), ...} self.extract_data_from_(self.first_champ_class_selection) # O(441) Prints data. self.graph.find_important_data(self.first_champ)
def test_addEdge_to_non_existant_vertex(self): vertices = [1,2] graph = ArrayGraph(vertices) with self.assertRaises(ValueError): graph.addEdge(1,3) with self.assertRaises(ValueError): graph.addEdge(2,3) graph.addEdge(1,2) assert len(graph.vertices) is 2 assert len(graph.vertices[0].edges) is 1 assert graph.vertices[0].edges[0] == (graph.vertices[1], 0)
def test_addVertex_duplicate(self): vertices = [] graph = ArrayGraph() graph.addVertex("1") assert graph.size is 1 with self.assertRaises(ValueError): # Can't add if already present. graph.addVertex("1") assert len(graph.vertices) is 1
def read_text(file): file = open(file, "r") counter = 0 data_struct = None temp_verices = [] temp_edges = [] for line in file: if counter is 0: # First line if line.strip().upper() == "G": data_struct = ArrayGraph() elif line.upper() == "D": pass elif counter is 1: # Second line temp_verices = line.strip().split(',') for vert in temp_verices: data_struct.addVertex(vert) else: # All the edges tuple = eval(line.strip()) temp_edges.append(tuple) if len(tuple) == 3: # If it has weight data_struct.addEdge(str(tuple[0]), str(tuple[1]), tuple[2]) else: data_struct.addEdge(str(tuple[0]), str(tuple[1])) counter += 1 return data_struct # Output code print("# Vertices:", len(data_struct.vertices)) all_edges = [] for vertex in data_struct.vertices: # O(n) for edges in vertex.edges: # O(m) all_edges.append((int(vertex.data), int(edges[0].data), edges[1])) print("# Edges:", len(all_edges)) for edge in all_edges: print(edge)
def test_empty_init(self): vertices = [] graph = ArrayGraph() assert graph.size is 0 assert graph.vertices == []
class TFT_team_picker(object): def __init__(self, champ_file, class_file): """Initializing the Team Fight Tactics Team Picker based on the game data stored in JSON files.""" self.champions = self.create_champ_list(champ_file) # O(n) self.classes, self.class_bonus, self.max_bonus, self.classes_dict = self.create_classes_list(class_file) # O(n) * O(3) self.champions_in_class = class_dict = {'demon': ['aatrox', 'brand', 'elise', 'evelynn', 'morgana', 'swain', 'varus'], 'dragon': ['aurelionsol', 'shyvana'], 'exile': ['yasuo'], 'glacial': ['anivia', 'ashe', 'braum', 'lissandra', 'sejuani', 'volibear'], 'imperial': ['darius', 'draven', 'katarina', 'swain'], 'noble': ['fiora', 'garen', 'kayle', 'leona', 'lucian', 'vayne'], 'ninja': ['akali', 'kennen', 'shen', 'zed'], 'pirate': ['gangplank', 'graves', 'missfortune', 'pyke', 'twistedfate'], 'phantom': ['karthus', 'kindred', 'mordekaiser'], 'robot': ['blitzcrank'], 'void': ['chogath', 'kassadin', 'khazix', 'reksai'], 'wild': ['ahri', 'gnar', 'nidalee', 'rengar', 'warwick'], 'yordle': ['gnar', 'kennen', 'lulu', 'poppy', 'tristana', 'veigar'], 'assassin': ['akali', 'evelynn', 'katarina', 'khazix', 'pyke', 'rengar', 'zed'], 'blademaster': ['aatrox', 'draven', 'fiora', 'gangplank', 'shen', 'yasuo'], 'brawler': ['blitzcrank', 'chogath', 'reksai', 'volibear', 'warwick'], 'elementalist': ['anivia', 'brand', 'kennen', 'lissandra'], 'guardian': ['braum', 'leona'], 'gunslinger': ['gangplank', 'graves', 'lucian', 'missfortune', 'tristana'], 'knight': ['darius', 'garen', 'kayle', 'mordekaiser', 'poppy', 'sejuani'], 'ranger': ['ashe', 'kindred', 'varus', 'vayne'], 'shapeshifter': ['elise', 'gnar', 'nidalee', 'shyvana', 'swain'], 'sorcerer': ['ahri', 'aurelionsol', 'karthus', 'kassadin', 'lulu', 'morgana', 'twistedfate', 'veigar']} self.graph = ArrayGraph(self.champions) self._add_edges() self.first_champ = input("First Champ:").lower() # User inputs key for first champion self.first_champ_class_selection = self.graph.find_all_champs_same_class_as(self.first_champ) # O(6273) { 'yordle': set('kennen', ...), ...} self.extract_data_from_(self.first_champ_class_selection) # O(441) Prints data. self.graph.find_important_data(self.first_champ) def create_champ_list(self, filename): """ Using the JSON file and the champions class, make a list containing all the champions. Runtime: O(n) number of champs. """ champions = [] # [Champion(key: 'aatrox', classes: ['demon', 'blademaster']), ...] with open(filename) as json_file: # Open JSON File data = json.load(json_file) # Get the JSON for champ_obj in data.items(): # O(n) ('aatrox' : { **aatrox data** } ) # Creating Champions Class Object new_champ = Champion(champ_obj[1]) champions.append(new_champ) return champions def create_classes_list(self, filename): """ Open JSON file and read the data for the Classes (and Origins). filename - the file name as a string. Runtime: O(n) * O(3) number of classes. """ classes = [] # [Class(key: 'exile'), Class(key: 'ninja'), ... ] class_bonus = { 1: [], 2: [], 3: [], 4 : [], 6 : []} # {1: [Class(key: 'exile'), Class(key: 'ninja'), ...], 2: [...], ... } max_bonus = { 1: [], 2: [], 3: [], 4 : [], 6 : []} # {1: [Class(key: 'exile'), Class(key: 'robot')], 2: [...], ... } classes_dict = {} # {'assassin': Class('assassin'), ...} with open(filename) as json_file: data = json.load(json_file) for class_obj in data.items(): # O(n) # Creating Classes Class Objects new_class = Champion_Class(class_obj[1]) classes.append(new_class) # All bonus Tracking bonuses = class_obj[1]['bonuses'] for bonus in bonuses: # O(3) worst case. class_bonus[bonus['needed']] += [new_class] # Max Bonus Tracking max_bonus[bonuses[-1]['needed']] += [new_class] # Creating a class dict. classes_dict.update({new_class.key: new_class}) return classes, class_bonus, max_bonus, classes_dict def _add_edges(self): """ Given the graph with all the champions, add edges to connect people of the same class. Runtime: O(n^3) Probably even worse because of graph.AddEdge. """ for class_ in self.champions_in_class.keys(): # For each class for champ in self.champions_in_class[class_]: # For each Champ of that class for champ_of_same_class in self.champions_in_class[class_]: # Loop to all the other champions of the same class. if champ != champ_of_same_class: # Don't connect to itself # print("Champ 1: {}, Champ 2: {}".format(champ,champ_of_same_class)) self.graph.addEdge(fromVert=champ, toVert=champ_of_same_class) # Connect Champ and all the champs of same class. def extract_data_from_(self, matches): """ Find champions that are repeated within the first_champ_class_selection dict. Runtime: O(441) = O(3) * O(7) * O(21) """ all_matching_class_champions = [] doubles = [] for class_ in matches.keys(): # O(3) Worse champs_in_class_ = [i.champ.key_ for i in matches[class_]] print("For {} class:{}\n".format(class_.upper(), champs_in_class_)) for champ in matches[class_]: # O(7) Worse if champ not in all_matching_class_champions: # O(21) Worse all_matching_class_champions.append(champ) else: if champ.champ.key_ != self.first_champ: doubles.append(champ) # print("All Mathcing Champs:", all_matching_class_champions) # print("Doubles:",doubles) if len(doubles) > 0: print("MUST GET:", [c.champ.key_ for c in doubles], "\n") def __repr__(self): """Return a string represenation of this Champion""" return 'TFT(Champion Count: {!r}, Classes Count: {})'.format(len(self.champions), len(self.classes))
def test_breadth_first_search(self): vertices = [1,2,3] graph = ArrayGraph(vertices) graph.addEdge(1,2) graph.addEdge(2,3) assert graph.breadth_first_search(1) == [graph.vertices[0], graph.vertices[1], graph.vertices[2]]
def test_find_path(self): vertices = [1,2,3] graph = ArrayGraph(vertices) graph.addEdge(1,2,3) graph.addEdge(2,3,1) assert graph.find_path(1, 3) == [graph.vertices[0],graph.vertices[1],graph.vertices[2]]
def test_depth_first_search(self): vertices = [1,2,3,4,5] graph = ArrayGraph(vertices) graph.addEdge(1,2,4) graph.addEdge(1,4,5) graph.addEdge(2,3,6) graph.addEdge(2,4,9) graph.addEdge(2,5,6) graph.addEdge(3,5,10) path = graph.depth_first_search(1) assert len(path) == 5 # It's a dictionary, so it's hard to test the contents of it.
def test_getVertices(self): vertices = [1,2,3] graph = ArrayGraph(vertices) assert len(graph.vertices) is 3 assert graph.getVertices() == graph.vertices
def test_dijkstra(self): vertices = [1,2,3,4,5] graph = ArrayGraph(vertices) graph.addEdge(1,2,4) graph.addEdge(1,4,5) graph.addEdge(2,3,6) graph.addEdge(2,4,9) graph.addEdge(2,5,6) graph.addEdge(3,5,10) path, weight = graph.dijkstra(1,5) assert len(path) == 3 assert weight == 10
def test_addEdge_to_itself(self): vertices = [1] graph = ArrayGraph(vertices) with self.assertRaises(ValueError): graph.addEdge(1,1)
def test_iterable_init(self): vertices = [1,2,3,4] graph = ArrayGraph(vertices) assert graph.size is 4 assert len(graph.vertices) is 4