From 32b9842521dbb8c61f59d6520f61f2c69aed9cf4 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Mon, 10 Aug 2020 23:52:36 -0400 Subject: [PATCH 1/9] partially completes daily project --- projects/graph/graph.py | 75 ++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index 59fecae4b..7d84246ce 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -13,33 +13,52 @@ def add_vertex(self, vertex_id): """ Add a vertex to the graph. """ - pass # TODO + self.vertices[vertex_id] = set() def add_edge(self, v1, v2): """ Add a directed edge to the graph. """ - pass # TODO + self.vertices[v1].add(v2) def get_neighbors(self, vertex_id): """ Get all neighbors (edges) of a vertex. """ - pass # TODO + return self.vertices[vertex_id] def bft(self, starting_vertex): """ Print each vertex in breadth-first order beginning from starting_vertex. """ - pass # TODO + visited = set() + queue = Queue() + queue.enqueue(starting_vertex) + while queue.size() > 0: + current = queue.dequeue() + print(current) + visited.add(current) + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + queue.enqueue(neighbor) def dft(self, starting_vertex): """ Print each vertex in depth-first order beginning from starting_vertex. """ - pass # TODO + visited = set() + stack = Stack() + stack.push(starting_vertex) + while stack.size() > 0: + current = stack.pop() + if current not in visited: + print(current) + visited.add(current) + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + stack.push(neighbor) def dft_recursive(self, starting_vertex): """ @@ -48,7 +67,7 @@ def dft_recursive(self, starting_vertex): This should be done using recursion. """ - pass # TODO + pass def bfs(self, starting_vertex, destination_vertex): """ @@ -56,7 +75,18 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ - pass # TODO + visited = [] + queue = Queue() + queue.enqueue(starting_vertex) + while queue.size() > 0: + current = queue.dequeue() + visited.append(current) + if current == destination_vertex: + return visited + else: + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + queue.enqueue(neighbor) def dfs(self, starting_vertex, destination_vertex): """ @@ -64,7 +94,18 @@ def dfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in depth-first order. """ - pass # TODO + visited = [] + stack = Stack() + stack.push(starting_vertex) + while stack.size() > 0: + current = stack.pop() + if current not in visited: + visited.append(current) + if current == destination_vertex: + return visited + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + stack.push(neighbor) def dfs_recursive(self, starting_vertex, destination_vertex): """ @@ -74,7 +115,15 @@ def dfs_recursive(self, starting_vertex, destination_vertex): This should be done using recursion. """ - pass # TODO + visited = [] + if starting_vertex == destination_vertex: + return starting_vertex + else: + for neighbor in self.get_neighbors(starting_vertex): + if neighbor not in visited: + visited.append(self.dfs_recursive(neighbor, destination_vertex)) + visited.prepend(starting_vertex) + return visited if __name__ == '__main__': graph = Graph() # Instantiate your graph @@ -118,7 +167,7 @@ def dfs_recursive(self, starting_vertex, destination_vertex): 1, 2, 4, 3, 7, 6, 5 1, 2, 4, 3, 7, 5, 6 ''' - graph.bft(1) + #graph.bft(1) ''' Valid DFT paths: @@ -127,14 +176,14 @@ def dfs_recursive(self, starting_vertex, destination_vertex): 1, 2, 4, 7, 6, 3, 5 1, 2, 4, 6, 3, 5, 7 ''' - graph.dft(1) - graph.dft_recursive(1) + # graph.dft(1) + # graph.dft_recursive(1) ''' Valid BFS path: [1, 2, 4, 6] ''' - print(graph.bfs(1, 6)) + # print(graph.bfs(1, 6)) ''' Valid DFS paths: From 584dbf491fdd624486a16227cf32747be67af6d2 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Tue, 11 Aug 2020 17:46:50 -0400 Subject: [PATCH 2/9] completes day 1 mvp --- projects/graph/graph.py | 57 ++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/projects/graph/graph.py b/projects/graph/graph.py index 7d84246ce..f02c13089 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -67,7 +67,12 @@ def dft_recursive(self, starting_vertex): This should be done using recursion. """ - pass + print(starting_vertex) + self.vertices[starting_vertex].add("Visited") + for neighbor in self.get_neighbors(starting_vertex): + if (type(neighbor) is int) and ("Visited" not in self.get_neighbors(neighbor)): + self.dft_recursive(neighbor) + def bfs(self, starting_vertex, destination_vertex): """ @@ -75,18 +80,21 @@ def bfs(self, starting_vertex, destination_vertex): starting_vertex to destination_vertex in breath-first order. """ - visited = [] + visited = set() queue = Queue() - queue.enqueue(starting_vertex) + queue.enqueue([starting_vertex]) while queue.size() > 0: - current = queue.dequeue() - visited.append(current) - if current == destination_vertex: - return visited - else: - for neighbor in self.get_neighbors(current): - if neighbor not in visited: - queue.enqueue(neighbor) + current_path = queue.dequeue() + last_vertex = current_path[-1] + if last_vertex not in visited: + if last_vertex == destination_vertex: + return current_path + else: + visited.add(last_vertex) + for neighbor in self.get_neighbors(last_vertex): + path_copy = list(current_path) + path_copy.append(neighbor) + queue.enqueue(path_copy) def dfs(self, starting_vertex, destination_vertex): """ @@ -107,7 +115,7 @@ def dfs(self, starting_vertex, destination_vertex): if neighbor not in visited: stack.push(neighbor) - def dfs_recursive(self, starting_vertex, destination_vertex): + def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path=[]): # Solved by messing around w/ it. Don't FULLY understand why it works, but it works. """ Return a list containing a path from starting_vertex to destination_vertex in @@ -115,15 +123,28 @@ def dfs_recursive(self, starting_vertex, destination_vertex): This should be done using recursion. """ - visited = [] + if visited is None: + visited = set() + path.append(starting_vertex) + visited.add(starting_vertex) + # current_vertex = path[-1] if starting_vertex == destination_vertex: - return starting_vertex + return path else: for neighbor in self.get_neighbors(starting_vertex): if neighbor not in visited: - visited.append(self.dfs_recursive(neighbor, destination_vertex)) - visited.prepend(starting_vertex) - return visited + path_copy = list(path) + path_copy.append(neighbor) + if neighbor == destination_vertex: + return path_copy + # return self.dfs_recursive(neighbor, destination_vertex, visited, path_copy) + else: + final_path = self.dfs_recursive(neighbor, destination_vertex, visited, path_copy) + final_vertex = final_path[-1] + if final_vertex == destination_vertex: + return final_path + return path + if __name__ == '__main__': graph = Graph() # Instantiate your graph @@ -190,5 +211,5 @@ def dfs_recursive(self, starting_vertex, destination_vertex): [1, 2, 4, 6] [1, 2, 4, 7, 6] ''' - print(graph.dfs(1, 6)) + # print(graph.dfs(1, 6)) print(graph.dfs_recursive(1, 6)) From e7197172dddcf9084ee16f76fdfdd4dc4c95e827 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Tue, 11 Aug 2020 19:31:52 -0400 Subject: [PATCH 3/9] completes day 2 mvp --- projects/ancestor/ancestor.py | 204 ++++++++++++++++++++++++++++- projects/ancestor/test_ancestor.py | 12 +- projects/graph/graph.py | 2 +- 3 files changed, 209 insertions(+), 9 deletions(-) diff --git a/projects/ancestor/ancestor.py b/projects/ancestor/ancestor.py index 3bd003098..3860871b8 100644 --- a/projects/ancestor/ancestor.py +++ b/projects/ancestor/ancestor.py @@ -1,3 +1,203 @@ -def earliest_ancestor(ancestors, starting_node): - pass \ No newline at end of file +def earliest_ancestor(ancestors, starting_node): # ([(parent, child), (parent, child), (parent, child)], 6) + graph = Graph() + # initialize graph, inverting parents and children + for pair in ancestors: + if pair[1] not in graph.vertices.keys(): + graph.add_vertex(pair[1]) + graph.add_edge(pair[1], pair[0]) + else: + graph.add_edge(pair[1], pair[0]) + if pair[0] not in graph.vertices.keys(): + graph.add_vertex(pair[0]) + earliest_ancestor = graph.dft(starting_node) + # last = graph.dfs(starting_node, earliest_ancestor)[-1] + if starting_node != earliest_ancestor: + # return last + return earliest_ancestor + else: + return -1 + +""" +Helper Classes +""" +class Queue(): + def __init__(self): + self.queue = [] + def enqueue(self, value): + self.queue.append(value) + def dequeue(self): + if self.size() > 0: + return self.queue.pop(0) + else: + return None + def size(self): + return len(self.queue) + +class Stack(): + def __init__(self): + self.stack = [] + def push(self, value): + self.stack.append(value) + def pop(self): + if self.size() > 0: + return self.stack.pop() + else: + return None + def size(self): + return len(self.stack) + +class Graph: + + """Represent a graph as a dictionary of vertices mapping labels to edges.""" + def __init__(self): + self.vertices = {} + + def add_vertex(self, vertex_id): + """ + Add a vertex to the graph. + """ + self.vertices[vertex_id] = set() + + def add_edge(self, v1, v2): + """ + Add a directed edge to the graph. + """ + self.vertices[v1].add(v2) + + def get_neighbors(self, vertex_id): + """ + Get all neighbors (edges) of a vertex. + """ + return self.vertices[vertex_id] + + def bft(self, starting_vertex): + """ + Print each vertex in breadth-first order + beginning from starting_vertex. + """ + visited = set() + queue = Queue() + queue.enqueue(starting_vertex) + while queue.size() > 0: + current = queue.dequeue() + print(current) + visited.add(current) + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + queue.enqueue(neighbor) + + def dft(self, starting_vertex): + """ + Print each vertex in depth-first order + beginning from starting_vertex. + """ + visited = set() + stack = Stack() + stack.push(starting_vertex) + current = None + while stack.size() > 0: + only_ancestors = True + sorted_neighbors = [] + current = stack.pop() + if current not in visited: + print(current) + visited.add(current) + for neighbor in self.get_neighbors(current): + sorted_neighbors.append(neighbor) + if len(self.get_neighbors(neighbor)) > 0: + only_ancestors = False + sorted(sorted_neighbors) + for i in range(len(sorted_neighbors)): + if only_ancestors: + neighbor = sorted_neighbors[len(sorted_neighbors)-(i+1)] + else: + neighbor = sorted_neighbors[i] + if neighbor not in visited: + stack.push(neighbor) + return current + + def dft_recursive(self, starting_vertex): + """ + Print each vertex in depth-first order + beginning from starting_vertex. + + This should be done using recursion. + """ + print(starting_vertex) + self.vertices[starting_vertex].add("Visited") + for neighbor in self.get_neighbors(starting_vertex): + if (type(neighbor) is int) and ("Visited" not in self.get_neighbors(neighbor)): + self.dft_recursive(neighbor) + + + def bfs(self, starting_vertex, destination_vertex): + """ + Return a list containing the shortest path from + starting_vertex to destination_vertex in + breath-first order. + """ + visited = set() + queue = Queue() + queue.enqueue([starting_vertex]) + while queue.size() > 0: + current_path = queue.dequeue() + last_vertex = current_path[-1] + if last_vertex not in visited: + if last_vertex == destination_vertex: + return current_path + else: + visited.add(last_vertex) + for neighbor in self.get_neighbors(last_vertex): + path_copy = list(current_path) + path_copy.append(neighbor) + queue.enqueue(path_copy) + + def dfs(self, starting_vertex, destination_vertex): + """ + Return a list containing a path from + starting_vertex to destination_vertex in + depth-first order. + """ + visited = [] + stack = Stack() + stack.push(starting_vertex) + while stack.size() > 0: + current = stack.pop() + if current not in visited: + visited.append(current) + if current == destination_vertex: + return visited + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + stack.push(neighbor) + + def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path=[]): # Walked through it in the debugger and I get it, but it's dizzyingly complicated. + """ + Return a list containing a path from + starting_vertex to destination_vertex in + depth-first order. + + This should be done using recursion. + """ + if visited is None: + visited = set() + path.append(starting_vertex) + visited.add(starting_vertex) + # current_vertex = path[-1] + if starting_vertex == destination_vertex: + return path + else: + for neighbor in self.get_neighbors(starting_vertex): + if neighbor not in visited: + path_copy = list(path) + path_copy.append(neighbor) + if neighbor == destination_vertex: + return path_copy + # return self.dfs_recursive(neighbor, destination_vertex, visited, path_copy) + else: + final_path = self.dfs_recursive(neighbor, destination_vertex, visited, path_copy) + final_vertex = final_path[-1] + if final_vertex == destination_vertex: + return final_path + return path \ No newline at end of file diff --git a/projects/ancestor/test_ancestor.py b/projects/ancestor/test_ancestor.py index 4d785f11e..c2e95e81c 100644 --- a/projects/ancestor/test_ancestor.py +++ b/projects/ancestor/test_ancestor.py @@ -15,16 +15,16 @@ class Test(unittest.TestCase): def test_earliest_ancestor(self): test_ancestors = [(1, 3), (2, 3), (3, 6), (5, 6), (5, 7), (4, 5), (4, 8), (8, 9), (11, 8), (10, 1)] self.assertEqual(earliest_ancestor(test_ancestors, 1), 10) - self.assertEqual(earliest_ancestor(test_ancestors, 2), -1) + self.assertEqual(earliest_ancestor(test_ancestors, 2), -1) # no ancestor self.assertEqual(earliest_ancestor(test_ancestors, 3), 10) - self.assertEqual(earliest_ancestor(test_ancestors, 4), -1) + self.assertEqual(earliest_ancestor(test_ancestors, 4), -1) # no ancestor self.assertEqual(earliest_ancestor(test_ancestors, 5), 4) self.assertEqual(earliest_ancestor(test_ancestors, 6), 10) self.assertEqual(earliest_ancestor(test_ancestors, 7), 4) - self.assertEqual(earliest_ancestor(test_ancestors, 8), 4) - self.assertEqual(earliest_ancestor(test_ancestors, 9), 4) - self.assertEqual(earliest_ancestor(test_ancestors, 10), -1) - self.assertEqual(earliest_ancestor(test_ancestors, 11), -1) + self.assertEqual(earliest_ancestor(test_ancestors, 8), 4) # goes to larger ancestor + self.assertEqual(earliest_ancestor(test_ancestors, 9), 4) # goes to larger ancestor + self.assertEqual(earliest_ancestor(test_ancestors, 10), -1) # no ancestor + self.assertEqual(earliest_ancestor(test_ancestors, 11), -1) # no ancestor if __name__ == '__main__': unittest.main() diff --git a/projects/graph/graph.py b/projects/graph/graph.py index f02c13089..b5c9197c1 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -115,7 +115,7 @@ def dfs(self, starting_vertex, destination_vertex): if neighbor not in visited: stack.push(neighbor) - def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path=[]): # Solved by messing around w/ it. Don't FULLY understand why it works, but it works. + def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path=[]): # Walked through it in the debugger and I get it, but it's dizzyingly complicated. """ Return a list containing a path from starting_vertex to destination_vertex in From 0bc8d3f02e66c413dab8b7f14f8bd2c1cc439a9b Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Wed, 12 Aug 2020 20:52:26 -0400 Subject: [PATCH 4/9] completes day 3 work --- projects/social/social.py | 230 +++++++++++++++++++++++++++++++++++++- 1 file changed, 229 insertions(+), 1 deletion(-) diff --git a/projects/social/social.py b/projects/social/social.py index 8609d8800..fc27c2636 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -1,3 +1,190 @@ +import random + +""" +Helper Classes +""" +class Queue(): + def __init__(self): + self.queue = [] + def enqueue(self, value): + self.queue.append(value) + def dequeue(self): + if self.size() > 0: + return self.queue.pop(0) + else: + return None + def size(self): + return len(self.queue) + +class Stack(): + def __init__(self): + self.stack = [] + def push(self, value): + self.stack.append(value) + def pop(self): + if self.size() > 0: + return self.stack.pop() + else: + return None + def size(self): + return len(self.stack) + +class Graph: + + """Represent a graph as a dictionary of vertices mapping labels to edges.""" + def __init__(self): + self.vertices = {} + + def add_vertex(self, vertex_id): + """ + Add a vertex to the graph. + """ + self.vertices[vertex_id] = set() + + def add_edge(self, v1, v2): + """ + Add a directed edge to the graph. + """ + self.vertices[v1].add(v2) + + def get_neighbors(self, vertex_id): + """ + Get all neighbors (edges) of a vertex. + """ + return self.vertices[vertex_id] + + def bft(self, starting_vertex): + """ + Print each vertex in breadth-first order + beginning from starting_vertex. + """ + visited = set() + queue = Queue() + queue.enqueue(starting_vertex) + while queue.size() > 0: + current = queue.dequeue() + print(current) + visited.add(current) + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + queue.enqueue(neighbor) + + def dft(self, starting_vertex): + """ + Print each vertex in depth-first order + beginning from starting_vertex. + """ + visited = set() + stack = Stack() + stack.push(starting_vertex) + current = None + while stack.size() > 0: + only_ancestors = True + sorted_neighbors = [] + current = stack.pop() + if current not in visited: + print(current) + visited.add(current) + for neighbor in self.get_neighbors(current): + sorted_neighbors.append(neighbor) + if len(self.get_neighbors(neighbor)) > 0: + only_ancestors = False + sorted(sorted_neighbors) + for i in range(len(sorted_neighbors)): + if only_ancestors: + neighbor = sorted_neighbors[len(sorted_neighbors)-(i+1)] + else: + neighbor = sorted_neighbors[i] + if neighbor not in visited: + stack.push(neighbor) + return current + + def dft_recursive(self, starting_vertex): + """ + Print each vertex in depth-first order + beginning from starting_vertex. + + This should be done using recursion. + """ + print(starting_vertex) + self.vertices[starting_vertex].add("Visited") + for neighbor in self.get_neighbors(starting_vertex): + if (type(neighbor) is int) and ("Visited" not in self.get_neighbors(neighbor)): + self.dft_recursive(neighbor) + + + def bfs(self, starting_vertex, destination_vertex): + """ + Return a list containing the shortest path from + starting_vertex to destination_vertex in + breath-first order. + """ + visited = set() + queue = Queue() + queue.enqueue([starting_vertex]) + while queue.size() > 0: + current_path = queue.dequeue() + last_vertex = current_path[-1] + if last_vertex not in visited: + if last_vertex == destination_vertex: + return current_path + else: + visited.add(last_vertex) + for neighbor in self.get_neighbors(last_vertex): + path_copy = list(current_path) + path_copy.append(neighbor) + queue.enqueue(path_copy) + + def dfs(self, starting_vertex, destination_vertex): + """ + Return a list containing a path from + starting_vertex to destination_vertex in + depth-first order. + """ + visited = [] + stack = Stack() + stack.push(starting_vertex) + while stack.size() > 0: + current = stack.pop() + if current not in visited: + visited.append(current) + if current == destination_vertex: + return visited + for neighbor in self.get_neighbors(current): + if neighbor not in visited: + stack.push(neighbor) + + def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path=[]): # Walked through it in the debugger and I get it, but it's dizzyingly complicated. + """ + Return a list containing a path from + starting_vertex to destination_vertex in + depth-first order. + + This should be done using recursion. + """ + if visited is None: + visited = set() + path.append(starting_vertex) + visited.add(starting_vertex) + # current_vertex = path[-1] + if starting_vertex == destination_vertex: + return path + else: + for neighbor in self.get_neighbors(starting_vertex): + if neighbor not in visited: + path_copy = list(path) + path_copy.append(neighbor) + if neighbor == destination_vertex: + return path_copy + # return self.dfs_recursive(neighbor, destination_vertex, visited, path_copy) + else: + final_path = self.dfs_recursive(neighbor, destination_vertex, visited, path_copy) + final_vertex = final_path[-1] + if final_vertex == destination_vertex: + return final_path + return path + + class User: def __init__(self, name): self.name = name @@ -43,10 +230,39 @@ def populate_graph(self, num_users, avg_friendships): self.users = {} self.friendships = {} # !!!! IMPLEMENT ME - + if avg_friendships >= num_users: + print("The average number of friendships must be less than the number of users.") + return + elif num_users <= 1: + print("The number of users must be above 1.") + return + else: # Add users + for i in range(num_users): + # name = input("Enter a name: ") + # self.add_user(name) + self.add_user("same_name") # Create friendships + friendships_max = num_users * avg_friendships + friendships_count = 0 + + #randomize the distribution + random_order_users = [] + for i in range(1, num_users+1): + random_order_users.append(i) + random.shuffle(random_order_users) + + for i in random_order_users: + num_friends = None + while (num_friends is None) or (((num_friends * 2) + friendships_count) > friendships_max): + num_friends = random.randint(0, num_users-1) + for j in range(num_friends): + friend_id = 0 + while friend_id == 0 or friend_id == i or friend_id in self.friendships[i]: + friend_id = random.randint(1, num_users) + self.add_friendship(i, friend_id) + friendships_count += num_friends * 2 def get_all_social_paths(self, user_id): """ @@ -59,8 +275,20 @@ def get_all_social_paths(self, user_id): """ visited = {} # Note that this is a dictionary, not a set # !!!! IMPLEMENT ME + visited[user_id] = [user_id] + graph = Graph() + for key in self.users.keys(): + graph.add_vertex(key) + for friend in self.friendships[key]: + graph.add_edge(key, friend) + self.get_neighbors_recursively(user_id, user_id, visited, graph) return visited + def get_neighbors_recursively(self, original_vertex, vertex, visited, graph=Graph()): + for neighbor in graph.get_neighbors(vertex): + if neighbor not in visited.keys(): + visited[neighbor] = graph.bfs(original_vertex, neighbor) + self.get_neighbors_recursively(original_vertex, neighbor, visited, graph) if __name__ == '__main__': sg = SocialGraph() From 566cc9a0fc48b25d875473a7495c01097f12e989 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Wed, 12 Aug 2020 23:43:00 -0400 Subject: [PATCH 5/9] completes part 3 of day 3 MVP --- projects/social/social.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/projects/social/social.py b/projects/social/social.py index fc27c2636..8df6fac2f 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -296,3 +296,11 @@ def get_neighbors_recursively(self, original_vertex, vertex, visited, graph=Grap print(sg.friendships) connections = sg.get_all_social_paths(1) print(connections) + +""" +1. To create 100 users with an average of 10 friends each, I would need to call add_friendship() 500 times because the number of friendships that would be created would be +100 * 10, which is 1000. And because each add_friendship creates 2 friendships (one in each direction), the number of calls would be quotient of 1000 / 2. + +2. The average degree of seperation between a user and an extended friend is 2. +Logic found in prob.py. +""" \ No newline at end of file From 51ec9a925a6547754d6011b47b6f64114ec10951 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Wed, 12 Aug 2020 23:43:37 -0400 Subject: [PATCH 6/9] adds prob.py --- prob.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 prob.py diff --git a/prob.py b/prob.py new file mode 100644 index 000000000..b803585f7 --- /dev/null +++ b/prob.py @@ -0,0 +1,47 @@ +import math + +probability = 1 +total = 1000 +numerator = 994 +denominator = 998 +count = 1 +greatest_chance = 0 +print() +print("1 Degree Away:") +for i in range(5): + for j in range(4): + probability = probability * (numerator/denominator) + numerator -= 1 + denominator -= 1 + print("Probability of", str(count), "User(s):", str(round(probability * 100, 2))+"%", "\tAverage Users:", math.ceil(count*probability)) + count += 1 +count_2 = 1 +print() +print("2 Degrees Away:") +for i in range(20): + for j in range(4): + probability = probability * (numerator/denominator) + numerator -= 1 + denominator -= 1 + print("Probability of", str(count_2), "User(s):", str(round(probability * 100, 2))+"%", "\tAverage Users:", math.ceil(count_2*probability)) + count_2 += 1 +count_3 = 1 +print() +print("3 Degrees Away:") +for i in range(80): + for j in range(4): + probability = probability * (numerator/denominator) + numerator -= 1 + denominator -= 1 + print("Probability of", str(count_3), "User(s):", str(round(probability * 100, 2))+"%", "\tAverage Users:", math.ceil(count_3*probability)) + count_3 += 1 +# count_4 = 1 +# print() +# print("4 Degrees Away:") +# for i in range(320): +# for j in range(4): +# probability = probability * (numerator/denominator) +# numerator -= 1 +# denominator -= 1 +# print(str(count_4)+": Probability:", probability * 100, "Users:", count_4*probability) +# count_4 += 1 \ No newline at end of file From 0f875140888d73cd8fe06a66feff341df1967e2e Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Thu, 13 Aug 2020 23:38:01 -0400 Subject: [PATCH 7/9] completes a portion of the sprint --- projects/adventure/adv.py | 135 ++++++++++++++++++++++++++++++++++---- projects/social/social.py | 41 ++++++++++-- 2 files changed, 160 insertions(+), 16 deletions(-) diff --git a/projects/adventure/adv.py b/projects/adventure/adv.py index 8bc540b5e..263aef789 100644 --- a/projects/adventure/adv.py +++ b/projects/adventure/adv.py @@ -4,17 +4,125 @@ import random from ast import literal_eval - +""" +Graph, Stack, and Queue Classes +""" + +class Queue(): + def __init__(self): + self.queue = [] + def enqueue(self, value): + self.queue.append(value) + def dequeue(self): + if self.size() > 0: + return self.queue.pop(0) + else: + return None + def size(self): + return len(self.queue) + +class Stack(): + def __init__(self): + self.stack = [] + def push(self, value): + self.stack.append(value) + def pop(self): + if self.size() > 0: + return self.stack.pop() + else: + return None + def size(self): + return len(self.stack) + +class RoomGraph: + + """Represent a graph as a dictionary of vertices mapping labels to edges.""" + def __init__(self): + self.rooms = {} + + def add_room(self, room_id, exits): #int, list + """ + Add a room to the graph along with its exits. + """ + if room_id not in self.rooms: + self.rooms[room_id] = {} + for r_exit in exits: + self.rooms[room_id][r_exit] = '?' + return True + else: + return False + + + def add_exit_id(self, room1, direction, room2): + """ + Add a directed edge to the graph. + """ + if room1 == room2: + return False + else: + reverse_direction = '' + if direction == 'n': + reverse_direction = 's' + elif direction == 's': + reverse_direction = 'n' + elif direction == 'w': + reverse_direction = 'e' + else: # direction == 'e' + reverse_direction = 'w' + self.rooms[room1][direction] = room2 + self.rooms[room2][reverse_direction] = room1 + return True + + def get_neighbors(self, room_id): + """ + Get all adjacent rooms to a given room. + """ + return self.rooms[room_id] # e.g. {'n': ?, 's': ?} + + def dft(self, starting_room_id): + """ + Traverse through all rooms from a given a starting room, depth first. + """ + visited = set() + # picks a random unexplored direction + exit_direction_list = self.get_neighbors(starting_room_id) + stack = Stack() + next_direction = '' + for direction in exit_direction_list: + if exit_direction_list[direction] == '?': + next_direction = direction + break + stack.push([next_direction]) + while stack.size() > 0: + current_path = stack.pop() + last_direction = current_path[-1] + player.travel(last_direction) + last_room = player.current_room.id + if last_room not in visited: + visited.add(last_room) + for direction in player.current_room.get_exits(): + path_copy = list(current_path) + path_copy.append(direction) + stack.push(path_copy) + return current_path + + # travels + # logs that direction + # loops + + +""" +""" # Load world world = World() # You may uncomment the smaller graphs for development and testing purposes. -# map_file = "maps/test_line.txt" +map_file = "maps/test_line.txt" # map_file = "maps/test_cross.txt" # map_file = "maps/test_loop.txt" # map_file = "maps/test_loop_fork.txt" -map_file = "maps/main_maze.txt" +# map_file = "maps/main_maze.txt" # Loads the map into a dictionary room_graph=literal_eval(open(map_file, "r").read()) @@ -28,6 +136,7 @@ # Fill this out with directions to walk # traversal_path = ['n', 'n'] traversal_path = [] +graph = RoomGraph() @@ -40,6 +149,8 @@ player.travel(move) visited_rooms.add(player.current_room) +print("Room:::", player.current_room.id) +print("Exit:::", player.current_room.get_exits()) if len(visited_rooms) == len(room_graph): print(f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited") else: @@ -51,12 +162,12 @@ ####### # UNCOMMENT TO WALK AROUND ####### -player.current_room.print_room_description(player) -while True: - cmds = input("-> ").lower().split(" ") - if cmds[0] in ["n", "s", "e", "w"]: - player.travel(cmds[0], True) - elif cmds[0] == "q": - break - else: - print("I did not understand that command.") +# player.current_room.print_room_description(player) +# while True: +# cmds = input("-> ").lower().split(" ") +# if cmds[0] in ["n", "s", "e", "w"]: +# player.travel(cmds[0], True) +# elif cmds[0] == "q": +# break +# else: +# print("I did not understand that command.") diff --git a/projects/social/social.py b/projects/social/social.py index 8df6fac2f..8eafd9e51 100644 --- a/projects/social/social.py +++ b/projects/social/social.py @@ -1,4 +1,5 @@ import random +import time """ Helper Classes @@ -264,6 +265,21 @@ def populate_graph(self, num_users, avg_friendships): self.add_friendship(i, friend_id) friendships_count += num_friends * 2 + def get_all_social_paths_given(self, user_id): + queue = Queue() + visited = {} + queue.enqueue([user_id]) + while queue.size() > 0: + path = queue.dequeue() + last_vertex = path[-1] + if last_vertex not in visited: + visited[last_vertex] = path + for friend in self.friendships[last_vertex]: + path_copy = path.copy() + path_copy.append(friend) + queue.enqueue(path_copy) + return visited + def get_all_social_paths(self, user_id): """ Takes a user's user_id as an argument @@ -284,23 +300,40 @@ def get_all_social_paths(self, user_id): self.get_neighbors_recursively(user_id, user_id, visited, graph) return visited + def get_neighbors_recursively(self, original_vertex, vertex, visited, graph=Graph()): for neighbor in graph.get_neighbors(vertex): if neighbor not in visited.keys(): visited[neighbor] = graph.bfs(original_vertex, neighbor) self.get_neighbors_recursively(original_vertex, neighbor, visited, graph) +# if __name__ == '__main__': +# sg = SocialGraph() +# sg.populate_graph(10, 2) +# print(sg.friendships) +# connections = sg.get_all_social_paths(1) +# print(connections) + if __name__ == '__main__': sg = SocialGraph() - sg.populate_graph(10, 2) - print(sg.friendships) + sg.populate_graph(500, 5) + # print(sg.friendships) + start_time = time.time() connections = sg.get_all_social_paths(1) - print(connections) + end_time = time.time() + print("My Function's Runtime:", (end_time - start_time)) + start_time = time.time() + connections = sg.get_all_social_paths_given(1) + end_time = time.time() + print("Given Function's Runtime:", (end_time - start_time)) + # print(connections) """ 1. To create 100 users with an average of 10 friends each, I would need to call add_friendship() 500 times because the number of friendships that would be created would be 100 * 10, which is 1000. And because each add_friendship creates 2 friendships (one in each direction), the number of calls would be quotient of 1000 / 2. -2. The average degree of seperation between a user and an extended friend is 2. +2. +15.8% of other users will be in a particular user's extended social network over 50% of the time. +The average degree of seperation between a user and an extended friend is 2. Logic found in prob.py. """ \ No newline at end of file From 59704b88c6f8635936eb47c38820f3dd0dc27723 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Fri, 14 Aug 2020 23:17:35 -0400 Subject: [PATCH 8/9] spaghetti code attempt at sprint --- projects/adventure/adv.py | 151 ++++++++++++++++++++++++++++++-------- projects/graph/graph.py | 19 ++++- 2 files changed, 136 insertions(+), 34 deletions(-) diff --git a/projects/adventure/adv.py b/projects/adventure/adv.py index 263aef789..f47d0eea3 100644 --- a/projects/adventure/adv.py +++ b/projects/adventure/adv.py @@ -4,6 +4,9 @@ import random from ast import literal_eval + +import os +dirpath = os.path.dirname(os.path.abspath(__file__)) """ Graph, Stack, and Queue Classes """ @@ -34,7 +37,7 @@ def pop(self): def size(self): return len(self.stack) -class RoomGraph: +class RoomGraph(): """Represent a graph as a dictionary of vertices mapping labels to edges.""" def __init__(self): @@ -79,32 +82,109 @@ def get_neighbors(self, room_id): """ return self.rooms[room_id] # e.g. {'n': ?, 's': ?} - def dft(self, starting_room_id): + def dft(self, starting_room_id, graph): """ Traverse through all rooms from a given a starting room, depth first. """ - visited = set() # picks a random unexplored direction - exit_direction_list = self.get_neighbors(starting_room_id) - stack = Stack() - next_direction = '' + visited = set() + current_room = starting_room_id + start = Stack() + reverse = Stack() + path = [] + all_visited = True + exit_direction_list = list(self.get_neighbors(current_room).keys()) for direction in exit_direction_list: - if exit_direction_list[direction] == '?': - next_direction = direction - break - stack.push([next_direction]) - while stack.size() > 0: - current_path = stack.pop() - last_direction = current_path[-1] - player.travel(last_direction) - last_room = player.current_room.id - if last_room not in visited: - visited.add(last_room) - for direction in player.current_room.get_exits(): - path_copy = list(current_path) - path_copy.append(direction) - stack.push(path_copy) - return current_path + if self.get_neighbors(current_room)[direction] == '?': + all_visited = False + start.push(direction) + if direction == 'n': + reverse.push('s') + elif direction == 's': + reverse.push('n') + elif direction == 'w': + reverse.push('e') + else: # direction == 'e' + reverse.push('w') + visited.add(current_room) + + while start.size() > 0: + print(len(path), ":", current_room) + if all_visited: + next_direction = reverse.pop() #n + path.append(next_direction) #n + if len(path) == 561: + print("debug") + try_again = True + while try_again: + try: + next_room = graph[current_room][1][next_direction] # next_room = room number (id) + try_again = False + except: + next_direction = reverse.pop() + if next_direction is not None: + try_again = True + current_room = next_room + exit_direction_list = list(self.get_neighbors(current_room).keys()) #['n', 's'] + for direction in exit_direction_list: + if self.get_neighbors(current_room)[direction] == '?': + if current_room in visited: + all_visited = False + break + else: + all_visited = False + start.push(direction) + if direction == 'n': + reverse.push('s') + elif direction == 's': + reverse.push('n') + elif direction == 'w': + reverse.push('e') + else: # direction == 'e' + reverse.push('w') + else: + next_direction = start.pop() #s + if next_direction == 'n': + reverse_direction = 's' + elif next_direction == 's': + reverse_direction = 'n' #this -> n + elif next_direction == 'w': + reverse_direction = 'e' + else: # next_direction == 'e' + reverse_direction = 'w' + next_room = graph[current_room][1][next_direction] # next_room = room number (id) + if self.rooms[current_room][next_direction] == '?' or self.rooms[next_room][reverse_direction] == '?': + try: + graph[next_room][1][reverse_direction] + path.append(next_direction) + self.rooms[current_room][next_direction] = next_room + self.rooms[next_room][reverse_direction] = current_room + current_room = next_room + except: + self.rooms[current_room][next_direction] = next_room + reverse.pop() + else: + continue + if current_room in visited and len(self.get_neighbors(current_room).keys())==4: + while reverse.size() > start.size(): + reverse.pop() + continue + visited.add(current_room) + all_visited = True + exit_direction_list = list(self.get_neighbors(current_room).keys()) #[n] + for direction in exit_direction_list: #n + if self.get_neighbors(current_room)[direction] == '?': + all_visited = False + start.push(direction) + if direction == 'n': + reverse.push('s') + elif direction == 's': + reverse.push('n') + elif direction == 'w': + reverse.push('e') + else: # direction == 'e' + reverse.push('w') + return path # travels # logs that direction @@ -118,11 +198,11 @@ def dft(self, starting_room_id): # You may uncomment the smaller graphs for development and testing purposes. -map_file = "maps/test_line.txt" -# map_file = "maps/test_cross.txt" -# map_file = "maps/test_loop.txt" -# map_file = "maps/test_loop_fork.txt" -# map_file = "maps/main_maze.txt" +# map_file = dirpath + "/maps/test_line.txt" +# map_file = dirpath + "/maps/test_cross.txt" +# map_file = dirpath + "/maps/test_loop.txt" +# map_file = dirpath + "/maps/test_loop_fork.txt" +map_file = dirpath + "/maps/main_maze.txt" # Loads the map into a dictionary room_graph=literal_eval(open(map_file, "r").read()) @@ -137,8 +217,13 @@ def dft(self, starting_room_id): # traversal_path = ['n', 'n'] traversal_path = [] graph = RoomGraph() +for room in room_graph: + graph.add_room(room, list(room_graph[room][1].keys())) - +# print("Class:", graph.rooms) +# print("Neighbors:", graph.get_neighbors(0)) +# print("DFT:", graph.dft(player.current_room.id, room_graph)) +traversal_path = graph.dft(player.current_room.id, room_graph) # TRAVERSAL TEST visited_rooms = set() @@ -149,8 +234,14 @@ def dft(self, starting_room_id): player.travel(move) visited_rooms.add(player.current_room) -print("Room:::", player.current_room.id) -print("Exit:::", player.current_room.get_exits()) +# print("Room:::", player.current_room.id) +# print("Exit:::", player.current_room.get_exits()) +# print("Graph:::", room_graph) +#room_graph[player.current_room.id][1]['n'] in visited +#room_graph[player.current_room.id][1]['s'] in visited +#room_graph[player.current_room.id][1]['e'] in visited +#room_graph[player.current_room.id][1]['w'] in visited +print("Length:::", len(room_graph)) if len(visited_rooms) == len(room_graph): print(f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited") else: diff --git a/projects/graph/graph.py b/projects/graph/graph.py index b5c9197c1..f565675d0 100644 --- a/projects/graph/graph.py +++ b/projects/graph/graph.py @@ -48,6 +48,17 @@ def dft(self, starting_vertex): Print each vertex in depth-first order beginning from starting_vertex. """ + # visited = set() + # stack = Stack() + # stack.push(starting_vertex) + # while stack.size() > 0: + # current = stack.pop() + # if current not in visited: + # print(current) + # visited.add(current) + # for neighbor in self.get_neighbors(current): + # if neighbor not in visited: + # stack.push(neighbor) visited = set() stack = Stack() stack.push(starting_vertex) @@ -55,7 +66,7 @@ def dft(self, starting_vertex): current = stack.pop() if current not in visited: print(current) - visited.add(current) + visited.add(current) for neighbor in self.get_neighbors(current): if neighbor not in visited: stack.push(neighbor) @@ -188,7 +199,7 @@ def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path= 1, 2, 4, 3, 7, 6, 5 1, 2, 4, 3, 7, 5, 6 ''' - #graph.bft(1) + # graph.bft(1) ''' Valid DFT paths: @@ -197,7 +208,7 @@ def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path= 1, 2, 4, 7, 6, 3, 5 1, 2, 4, 6, 3, 5, 7 ''' - # graph.dft(1) + graph.dft(1) # graph.dft_recursive(1) ''' @@ -212,4 +223,4 @@ def dfs_recursive(self, starting_vertex, destination_vertex, visited=None, path= [1, 2, 4, 7, 6] ''' # print(graph.dfs(1, 6)) - print(graph.dfs_recursive(1, 6)) + # print(graph.dfs_recursive(1, 6)) From 3ff2b80224a9618108b52313c9c248d7cfc9fcb3 Mon Sep 17 00:00:00 2001 From: Handell Desulme Date: Mon, 17 Aug 2020 16:32:43 -0400 Subject: [PATCH 9/9] completes sprint mvp w/ try/except --- bash.exe.stackdump | 16 +++ projects/adventure/adv.py | 247 +++++++++++++++++++++----------------- 2 files changed, 154 insertions(+), 109 deletions(-) create mode 100644 bash.exe.stackdump diff --git a/bash.exe.stackdump b/bash.exe.stackdump new file mode 100644 index 000000000..6c70bb288 --- /dev/null +++ b/bash.exe.stackdump @@ -0,0 +1,16 @@ +Stack trace: +Frame Function Args +00800000010 0018006392E (0018027F9A0, 0018026AFD1, 00000000059, 000FFFFB730) +00800000010 0018004973A (00800000010, 00100000000, 00000000000, 00000000001) +00800000010 00180049772 (00000000000, 00000000000, 00000000059, 0018034FAB8) +00800000010 001800706C9 (000FFFFCCE0, 000FFFFC800, 0000000000B, 00000000000) +00800000010 00180070980 (00000000003, 000FFFFC920, 00180045BCF, 000FFFFC920) +00800000010 00180072189 (000FFFFC920, 0018026D795, 001800FDD27, 0000000000D) +00800000010 0018005B0C3 (000FFFFCA70, 00000000000, 00000000000, 008FFFFFFFF) +00800000010 0018005C475 (00000000002, 0018034F400, 00000000000, 000000303E9) +00800000010 0018005C95F (000FFFFCCE0, 00800061E20, 000FFFFCC70, 000FFFFCC70) +000FFFFCBEC 0018005CD26 (000FFFFCE00, 00000000000, 00000000030, 0000000002F) +000FFFFCCE0 00180049EE8 (00000000000, 00000000000, 00000000000, 00000000000) +000FFFFFFF0 00180048846 (00000000000, 00000000000, 00000000000, 00000000000) +000FFFFFFF0 001800488F4 (00000000000, 00000000000, 00000000000, 00000000000) +End of stack trace diff --git a/projects/adventure/adv.py b/projects/adventure/adv.py index f47d0eea3..2914773d6 100644 --- a/projects/adventure/adv.py +++ b/projects/adventure/adv.py @@ -34,6 +34,11 @@ def pop(self): return self.stack.pop() else: return None + def peek(self): + if self.size() > 0: + return self.stack[len(self.stack) - 1] + else: + return None def size(self): return len(self.stack) @@ -82,114 +87,138 @@ def get_neighbors(self, room_id): """ return self.rooms[room_id] # e.g. {'n': ?, 's': ?} - def dft(self, starting_room_id, graph): + def dft(self, starting_room_id, graph, player): """ Traverse through all rooms from a given a starting room, depth first. """ # picks a random unexplored direction - visited = set() - current_room = starting_room_id - start = Stack() - reverse = Stack() + # picks a random unexplored direction + visited = {} + backward = Stack() path = [] - all_visited = True - exit_direction_list = list(self.get_neighbors(current_room).keys()) - for direction in exit_direction_list: - if self.get_neighbors(current_room)[direction] == '?': - all_visited = False - start.push(direction) - if direction == 'n': - reverse.push('s') - elif direction == 's': - reverse.push('n') - elif direction == 'w': - reverse.push('e') - else: # direction == 'e' - reverse.push('w') - visited.add(current_room) - - while start.size() > 0: - print(len(path), ":", current_room) - if all_visited: - next_direction = reverse.pop() #n - path.append(next_direction) #n - if len(path) == 561: - print("debug") - try_again = True - while try_again: - try: - next_room = graph[current_room][1][next_direction] # next_room = room number (id) - try_again = False - except: - next_direction = reverse.pop() - if next_direction is not None: - try_again = True - current_room = next_room - exit_direction_list = list(self.get_neighbors(current_room).keys()) #['n', 's'] - for direction in exit_direction_list: - if self.get_neighbors(current_room)[direction] == '?': - if current_room in visited: - all_visited = False - break - else: - all_visited = False - start.push(direction) - if direction == 'n': - reverse.push('s') - elif direction == 's': - reverse.push('n') - elif direction == 'w': - reverse.push('e') - else: # direction == 'e' - reverse.push('w') - else: - next_direction = start.pop() #s - if next_direction == 'n': - reverse_direction = 's' - elif next_direction == 's': - reverse_direction = 'n' #this -> n - elif next_direction == 'w': - reverse_direction = 'e' - else: # next_direction == 'e' - reverse_direction = 'w' - next_room = graph[current_room][1][next_direction] # next_room = room number (id) - if self.rooms[current_room][next_direction] == '?' or self.rooms[next_room][reverse_direction] == '?': - try: - graph[next_room][1][reverse_direction] - path.append(next_direction) - self.rooms[current_room][next_direction] = next_room - self.rooms[next_room][reverse_direction] = current_room - current_room = next_room - except: - self.rooms[current_room][next_direction] = next_room - reverse.pop() - else: - continue - if current_room in visited and len(self.get_neighbors(current_room).keys())==4: - while reverse.size() > start.size(): - reverse.pop() - continue - visited.add(current_room) - all_visited = True - exit_direction_list = list(self.get_neighbors(current_room).keys()) #[n] - for direction in exit_direction_list: #n - if self.get_neighbors(current_room)[direction] == '?': - all_visited = False - start.push(direction) - if direction == 'n': - reverse.push('s') - elif direction == 's': - reverse.push('n') - elif direction == 'w': - reverse.push('e') - else: # direction == 'e' - reverse.push('w') - return path - - # travels - # logs that direction - # loops + visited[player.current_room.id] = player.current_room.get_exits() + while len(visited) < len(graph): + if len(visited) == 11: + print("start") + if player.current_room.id not in visited.keys(): + visited[player.current_room.id] = player.current_room.get_exits() + visited[player.current_room.id].remove(backward.peek()) + if len(visited) == len(graph): + return path + + try: + next_direction = visited[player.current_room.id].pop() + reverse_direction = self.reverse_direction(next_direction) + backward.push(reverse_direction) + player.travel(next_direction) + path.append(next_direction) + except: + next_direction = backward.pop() + player.travel(next_direction) + path.append(next_direction) + # visited = {set()} + # current_room = starting_room_id + # previous_room = None + # next_room = None + # forward = Stack() + # backward = Stack() + # forward_counter = 0 + # reverse_counter = 0 + # path = [] + # all_visited = True + + # # First Call + # exit_direction_list = list(self.get_neighbors(current_room).keys()) + # self.next_direction_1(exit_direction_list, current_room, forward, backward) + # visited.add(current_room) + + # print("path, room, visited") + # while len(visited) < len(graph): + # print(len(path), current_room, len(visited)) + # if len(path) == 344: + # print("here") + # if self.check_neighbors_for_dead_end(exit_direction_list, current_room) == True or next_room in visited: # Backward + # if reverse_counter == forward_counter: + # next_room = graph[current_room][1][forward.peek()] + # if next_room in visited: + # backward.pop() + # forward.pop() + # continue + # next_direction = backward.pop() + # path.append(next_direction) + # reverse_counter += 1 + # next_room = graph[current_room][1][next_direction] + # previous_room = current_room + # current_room = next_room + # exit_direction_list = list(self.get_neighbors(current_room).keys()) + # if self.check_neighbors_for_dead_end(exit_direction_list, current_room) == True: + # next_room = graph[current_room][1][backward.peek()] + # else: + # next_room = graph[current_room][1][forward.peek()] + # if next_room in visited: + # backward.pop() + # forward.pop() + # else: # Forward + # next_direction = forward.pop() + # opposite_direction = self.reverse_direction(next_direction) + # next_room = graph[current_room][1][next_direction] + # try: + # graph[next_room][1][opposite_direction] + # path.append(next_direction) + # forward_counter += 1 + # self.rooms[current_room][next_direction] = next_room + # self.rooms[next_room][opposite_direction] = current_room + # previous_room = current_room + # current_room = next_room + # visited.add(current_room) + # except: + # self.rooms[current_room][next_direction] = next_room + # backward.pop() + # exit_direction_list = list(self.get_neighbors(current_room).keys()) + # self.next_direction_1(exit_direction_list, current_room, forward, backward) + # if self.check_neighbors_for_dead_end(exit_direction_list, current_room) == False: + # next_room = graph[current_room][1][forward.peek()] + # if next_room in visited: + # backward.pop() + # forward.pop() + # self.rooms[current_room][forward.peek()] = next_room + # try: + # self.rooms[next_room][self.reverse_direction(forward.peek())] = current_room + # except: + # pass + # return path + + # def check_neighbors_for_dead_end(self, exit_direction_list, current_room): + # dead_end = True + # for direction in exit_direction_list: + # if self.get_neighbors(current_room)[direction] == '?': + # dead_end = False + # return dead_end + + # def next_direction_2(self, exit_direction_list, current_room, forward, backward, visited): + # for direction in exit_direction_list: + # if self.get_neighbors(current_room)[direction] == '?': + # if current_room in visited: + # break + # else: + # forward.push(direction) + # backward.push(self.reverse_direction(direction)) + + # def next_direction_1(self, exit_direction_list, current_room, forward, backward): + # for direction in exit_direction_list: #n + # if self.get_neighbors(current_room)[direction] == '?': + # forward.push(direction) + # backward.push(self.reverse_direction(direction)) + def reverse_direction(self, direction): + if direction == 'n': + return 's' + elif direction == 's': + return 'n' #this -> n + elif direction == 'w': + return 'e' + else: # direction == 'e' + return 'w' """ """ @@ -215,15 +244,15 @@ def dft(self, starting_room_id, graph): # Fill this out with directions to walk # traversal_path = ['n', 'n'] -traversal_path = [] +# traversal_path = [] graph = RoomGraph() for room in room_graph: graph.add_room(room, list(room_graph[room][1].keys())) -# print("Class:", graph.rooms) -# print("Neighbors:", graph.get_neighbors(0)) -# print("DFT:", graph.dft(player.current_room.id, room_graph)) -traversal_path = graph.dft(player.current_room.id, room_graph) +print("Class:", graph.rooms) +print("Neighbors:", graph.get_neighbors(0)) +# print("DFT:", graph.dft(player.current_room.id, room_graph, player)) +traversal_path = graph.dft(player.current_room.id, room_graph, player) # TRAVERSAL TEST visited_rooms = set() @@ -234,8 +263,8 @@ def dft(self, starting_room_id, graph): player.travel(move) visited_rooms.add(player.current_room) -# print("Room:::", player.current_room.id) -# print("Exit:::", player.current_room.get_exits()) +print("Room:::", player.current_room.id) +print("Exit:::", player.current_room.get_exits()) # print("Graph:::", room_graph) #room_graph[player.current_room.id][1]['n'] in visited #room_graph[player.current_room.id][1]['s'] in visited