Skip to content

Lezajackson #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@

## Objectives

* [Graph Intro](objectives/graph-intro)
* [Graph Representations](objectives/graph-representations)
* [Breadth First Search](objectives/breadth-first-search)
* [Depth First Search](objectives/depth-first-search)
* [Randomness](objectives/randomness)
* [Connected Components](objectives/connected-components)
- [Graph Intro](objectives/graph-intro)
- [Graph Representations](objectives/graph-representations)
- [Breadth First Search](objectives/breadth-first-search)
- [Depth First Search](objectives/depth-first-search)
- [Randomness](objectives/randomness)
- [Connected Components](objectives/connected-components)

## Projects

### Day 1
* [Graph Traversal and Search](projects/graph)

- [Graph Traversal and Search](projects/graph)

### Day 2
* [Earliest Ancestor](projects/ancestor)

- [Earliest Ancestor](projects/ancestor)

### Day 3
* [Random Social Network](projects/social)

- [Random Social Network](projects/social)

### Day 4
* [Adventure Map Traversal](projects/adventure)

- [Adventure Map Traversal](projects/adventure)
27 changes: 12 additions & 15 deletions projects/adventure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ You are provided with a pre-generated graph consisting of 500 rooms. You are res

Open `adv.py`. There are four parts to the provided code:

* World generation code. Do not modify this!
* An incomplete list of directions. Your task is to fill this with valid traversal directions.
* Test code. Run the tests by typing `python3 adv.py` in your terminal.
* REPL code. You can uncomment this and run `python3 adv.py` to walk around the map.
- World generation code. Do not modify this!
- An incomplete list of directions. Your task is to fill this with valid traversal directions.
- Test code. Run the tests by typing `python3 adv.py` in your terminal.
- REPL code. You can uncomment this and run `python3 adv.py` to walk around the map.


You may find the commands `player.current_room.id`, `player.current_room.get_exits()` and `player.travel(direction)` useful.
You may find the commands `player.current_room.id`, `player.current_room.get_exits()` and `player.travel(direction)` useful...

To solve this path, you'll want to construct your own traversal graph. You start in room `0`, which contains exits `['n', 's', 'w', 'e']`. Your starting graph should look something like this:

Expand Down Expand Up @@ -49,21 +48,19 @@ If all paths have been explored, you're done!

## Minimum Viable Product

* __1__: Tests do not pass
* __2__: Tests pass with `len(traversal_path) <= 2000`
* __3__: Tests pass with `len(traversal_path) < 960`
- **1**: Tests do not pass
- **2**: Tests pass with `len(traversal_path) <= 2000`
- **3**: Tests pass with `len(traversal_path) < 960`

## Stretch Problems

It is very difficult to calculate the shortest possible path that traverses the entire graph. Why?

My best path is 957 moves. Can you find a shorter path?


## Rubric
| OBJECTIVE | TASK | 1 - DOES NOT MEET Expectations | 2 - MEETS Expectations | 3 - EXCEEDS Expectations | SCORE |
| ---------- | ----- | ------- | ------- | ------- | -- |
| _Student can demonstrate applied knowledge of Graph Theory by traversing a large map_ | Complete traversal of a large Graph | Student unable to produce a valid traversal path of 2000 moves or less | Student is able to produce a valid traversal path between 960 and 2000 | Student produces a valid traversal path of 959 moves or less | |
| **FINAL SCORE** | | **0-1** | **2** | **3** | |


| OBJECTIVE | TASK | 1 - DOES NOT MEET Expectations | 2 - MEETS Expectations | 3 - EXCEEDS Expectations | SCORE |
| ------------------------------------------------------------------------------------- | ----------------------------------- | ---------------------------------------------------------------------- | ---------------------------------------------------------------------- | ------------------------------------------------------------ | ----- |
| _Student can demonstrate applied knowledge of Graph Theory by traversing a large map_ | Complete traversal of a large Graph | Student unable to produce a valid traversal path of 2000 moves or less | Student is able to produce a valid traversal path between 960 and 2000 | Student produces a valid traversal path of 959 moves or less | |
| **FINAL SCORE** | | **0-1** | **2** | **3** | |
72 changes: 59 additions & 13 deletions projects/adventure/adv.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,66 @@
map_file = "maps/main_maze.txt"

# Loads the map into a dictionary
room_graph=literal_eval(open(map_file, "r").read())
room_graph = literal_eval(open(map_file, "r").read())
world.load_graph(room_graph)

# Print an ASCII map
world.print_rooms()
print("this is my grid", len(world.rooms))

player = Player(world.starting_room)

# Fill this out with directions to walk
# traversal_path = ['n', 'n']
traversal_path = []
inverse_path = {"n": "s", "s": "n", "e": "w", "w": "e"}



def return_home(path, completely=True):
"""returns a complete path to end and back home, unless completely is false, then only the inverse is printed"""
invert_path = [inverse_path[path[i]] for i in range(len(path))[::-1]]
path.extend(invert_path)
if completely:
return path
else:
return invert_path


def traverse_rooms(current_room, path=None, visited=None):
if path == None:
path = []
if visited == None:
visited = set()
visited.add(current_room.name)
current_movements = current_room.get_exits()
for direction in current_movements: # every direction I can move in
next_room = current_room.get_room_in_direction(
direction) # grab the room that I can go to
if len(current_movements) >= 3:
path = []
if len(current_movements) == 1 and current_room.get_room_in_direction(direction).name in visited:
traversal_path.extend(return_home(path))
return
elif len(current_movements) == 2 and current_room.rooms_visited(visited):
traversal_path.extend(return_home(path))
return
elif next_room.name not in visited: # If I haven't been here, let's proceed
path.append(direction)
movements = next_room.get_exits() # Now depending on where I can go
# If go either proceed or move back, let's carry the path
if len(movements) == 2:
traverse_rooms(next_room, [*path], visited)
# if there are more than 3 places to go
elif len(movements) > 2:
traversal_path.extend(path)
traverse_rooms(next_room, visited=visited)
traversal_path.extend(return_home(path, False))
else:
traverse_rooms(next_room, path, visited=visited)


traverse_rooms(world.starting_room)
# print("this is all that matters now ",traversal_path)
# TRAVERSAL TEST
visited_rooms = set()
player.current_room = world.starting_room
Expand All @@ -41,22 +87,22 @@
visited_rooms.add(player.current_room)

if len(visited_rooms) == len(room_graph):
print(f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited")
print(
f"TESTS PASSED: {len(traversal_path)} moves, {len(visited_rooms)} rooms visited")
else:
print("TESTS FAILED: INCOMPLETE TRAVERSAL")
print(f"{len(room_graph) - len(visited_rooms)} unvisited rooms")



#######
# 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.")
22 changes: 22 additions & 0 deletions projects/adventure/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ def __init__(self, name, description, id=0, x=None, y=None):
self.w_to = None
self.x = x
self.y = y

def __str__(self):
return f"\n-------------------\n\n{self.name}\n\n {self.description}\n\n{self.get_exits_string()}\n"

def print_room_description(self, player):
print(str(self))

def get_exits(self):
exits = []
if self.n_to is not None:
Expand All @@ -26,8 +29,10 @@ def get_exits(self):
if self.e_to is not None:
exits.append("e")
return exits

def get_exits_string(self):
return f"Exits: [{', '.join(self.get_exits())}]"

def connect_rooms(self, direction, connecting_room):
if direction == "n":
self.n_to = connecting_room
Expand All @@ -44,6 +49,7 @@ def connect_rooms(self, direction, connecting_room):
else:
print("INVALID ROOM CONNECTION")
return None

def get_room_in_direction(self, direction):
if direction == "n":
return self.n_to
Expand All @@ -55,5 +61,21 @@ def get_room_in_direction(self, direction):
return self.w_to
else:
return None

def get_exit_room_names(self,):
"""returns the surrounding rooms in self by name in the form of a list """
directions = self.get_exits()
return [self.get_room_in_direction(direction).name for direction in directions]

def rooms_visited(self, visited):
"""Takes in the room surrounding rooms of self and compares it against a visited collections
if ALL surrounding rooms are in visited, then it will return True
"""
rooms = self.get_exit_room_names()
for room in rooms:
if room not in visited:
return False
return True

def get_coords(self):
return [self.x, self.y]
33 changes: 33 additions & 0 deletions projects/adventure/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Note: This Queue class is sub-optimal. Why?
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)
45 changes: 44 additions & 1 deletion projects/ancestor/ancestor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,46 @@
# from graph.graph import Graph
from resources import Queue, Graph
print(Queue)


def earliest_ancestor(ancestors, starting_node):
pass
pass
# ancestors = a list of tuples that establish node/edges find the furthest nodes
#
# insertion_set = set()
graph = Graph()
for pair in ancestors: # fill the graph and adding vertex
parent = pair[0]
child = pair[1]
graph.add_vertex(child)
graph.add_vertex(parent)
graph.add_edge(child, parent)
queue = Queue()
queue.enqueue([starting_node]) # ([1])
longest_path = 1
earliest_ancestor = -1
while queue.size() > 0:
current_path = queue.dequeue()
current_node = current_path[-1]
# if not graph.get_neighbors(current_node):
# parents = graph.get_neighbors(current_node)
if len(current_path) >= longest_path and current_node < earliest_ancestor:
earliest_ancestor = current_node
longest_path = len(current_path)
if len(current_path) > longest_path:
longest_path = len(current_path)
earliest_ancestor = current_node
parents = graph.get_neighbors(current_node)
for parent in parents:
new_path = current_path.copy()
new_path.append(parent) # [6, 3] [6,5]
queue.enqueue(new_path) # queue = [[6, 3], [6,5]]
#[[6,5], [6,3,1], [6,3,2]]
# print(graph.get_neighbors(8))
return earliest_ancestor
# print(insertion_set)


test_ancestors = [(1, 3), (2, 3), (3, 6), (5, 6), (5, 7),
(4, 5), (4, 8), (8, 9), (11, 8), (10, 1)]
print(earliest_ancestor(test_ancestors, 9))
Loading