-
Notifications
You must be signed in to change notification settings - Fork 0
hope this works #3
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
galjs
wants to merge
59
commits into
comments
Choose a base branch
from
master
base: comments
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 55 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
40b5179
Add project files.
galjs 25315ae
added cell and maze classes
galjs 16f8d1b
created cell class and maze class
galjs f01f2b7
fixed a bug with the proportions of the board
galjs ed26f6d
added board verification
galjs 6bd7ef9
changed printing functions to __str__ format
galjs faaa9c5
added junction calss
galjs 763d554
added Connection class
galjs 7716b6b
add Position class
galjs eaeb37e
adding to_graph methods to Maze
galjs d06c8cc
changed junction
galjs 4f2e383
sorting things out
galjs 3539bb1
graph ready
galjs 933a65f
graph is complete. cell has mark attribute
galjs 00618e6
new bfs_maze and discovered attribute for junction
galjs d8f3949
all is complete except the algorithm
galjs 56b33c2
bfs_maze is ready
galjs 0b333d2
algorithem finds the finish
galjs 71370c8
maze solved
galjs 4b7cd96
raise an error if no solution available
galjs 7f87257
new missing_number file
galjs 86d2aa1
try #2
galjs 8ef491a
working up to "this function should be in Graph"
galjs d588f5f
workung up to "This also can be a function of Graph"
galjs 548f237
test_maze, cell, bfs_graph now working
galjs 5afa6be
graph also working now
galjs b24fee5
junction working
galjs ac50ec9
maze is working
galjs 09e4cda
all corrections done
galjs 883b151
trying to ignore pyproj files
galjs 4afb869
middle of correction
galjs 753f8e4
fix complete
galjs 47ee6d1
back to original graph
galjs 1577598
original state
galjs e1cb587
trying to excule files
galjs 5b576d6
remove pyproj and sln?
galjs 9ae8386
maybe now?
galjs b665acc
no changes yet
galjs c153e56
no more changes
galjs 25f113c
working state
galjs bf75af5
halfway through...
galjs 804f53b
bfs_maze complete
galjs c66e111
complete
galjs 6f280b7
deleted trailing lines
galjs 61a52b4
bfs_search corrected
galjs c7ad1eb
everything except _update_junctions
galjs f8e32a7
more algorithms
galjs cca3508
all changes made
galjs 80d7f71
iterator permutations
galjs b02bccd
variable names changes
galjs b107665
recursive permutations
galjs 93f5db1
all changes complete
galjs ba682d4
all algorithms complete
galjs 2863363
more readable and with new test class
galjs d622bf9
completed all changes + algorithms are now in a package
galjs d88d695
all cahnges made
galjs 51364e0
fixed
galjs 34435ca
fixed again
galjs f4e4563
fixed the mistake
galjs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from math import factorial | ||
|
||
"""This module supplies fast algorithms for pattern-related creations: | ||
all permutations of a staring, duplicate-deletion.""" | ||
|
||
def create_trimmed_string(base_string): | ||
"""deletes duplicates in a string. example: aaabbaac -> abac""" | ||
trimmed_elements = [base_string[0]] | ||
for index in range(1, len(base_string)): | ||
if base_string[index] != base_string[index-1]: | ||
trimmed_elements.append(base_string[index]) | ||
return ''.join(trimmed_elements) | ||
|
||
|
||
def generate_permutations_iterably(elements): | ||
"""returns all the permutations of a string. the function is non-recursive""" | ||
permutations = set() | ||
final_permutations = set() | ||
next_index_to_be_permutated = 0 | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expectd_permutations_number = factorial(len(elements)) | ||
|
||
new_permutations = _generate_new_permutations(elements) | ||
permutations.update(new_permutations) | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
while len(permutations) < expectd_permutations_number: | ||
permutations_copy = permutations.copy() | ||
permutations = set() | ||
|
||
for permutation in permutations_copy: | ||
new_permutations = _generate_new_permutations(permutation) | ||
final_permutations.update(new_permutations) | ||
permutations.update(new_permutations) | ||
|
||
return [''.join(permutation) for permutation in final_permutations] | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _generate_new_permutations(base_state): | ||
new_permutations = [] | ||
for index in range(len(base_state)): | ||
base_state_copy = list(base_state) | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_switch_cells(base_state_copy, 0, index) | ||
new_permutations.append(base_state_copy) | ||
|
||
return [''.join(permutation) for permutation in new_permutations] | ||
|
||
|
||
def generate_permutations_recursively(base_string): | ||
"""returns all the permutations of a string. the function is recursive""" | ||
return _generate_permutations_recursively(list(base_string)) | ||
|
||
|
||
def _generate_permutations_recursively(elements): | ||
permutations = [] | ||
if len(elements) == 1: | ||
return elements | ||
|
||
for index in range(len(elements)): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
elements_copy = list(elements) | ||
_switch_cells(elements_copy, 0, index) | ||
for combination in _generate_permutations_recursively(elements_copy[1:]): | ||
new_permutation = elements_copy[0] + ''.join(combination) | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
permutations.append(new_permutation) | ||
|
||
return permutations | ||
|
||
|
||
def _switch_cells(elements, first_index, second_index): | ||
elements[first_index], elements[second_index] = elements[second_index], elements[first_index] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from bisect import insort | ||
|
||
|
||
"""This module supplies fast algorithms for pattern-related searches: | ||
longest palindrome, biggest elements in a list.""" | ||
|
||
def find_n_biggest_numbers(numbers, amount=20): | ||
"""return the nth biggest elements in the list""" | ||
if len(numbers) > amount: | ||
biggest_numbers = sorted(numbers[:amount]) | ||
else: | ||
return numbers | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
for number in numbers: | ||
if number > biggest_numbers[0]: | ||
insort(biggest_numbers, number) | ||
del biggest_numbers[0] | ||
|
||
return biggest_numbers | ||
|
||
|
||
def find_longest_palindrome(base_string): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""returns the longest palindrome in a string""" | ||
longest_palindrome_length = 0 | ||
longest_palindrome_start_index = 0 | ||
|
||
for index in range(len(base_string) - 1): | ||
current_length = _palindrome_length(base_string, index) | ||
if current_length > longest_palindrome_length: | ||
longest_palindrome_length = current_length | ||
longest_palindrome_start_index = index | ||
|
||
begining = longest_palindrome_start_index - int(longest_palindrome_length/2) | ||
end = longest_palindrome_start_index + int(longest_palindrome_length / 2) + 1 | ||
|
||
if not longest_palindrome_length % 2 == 0: | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return base_string[begining:end] | ||
else: | ||
return base_string[begining + 1:end] | ||
|
||
|
||
def _palindrome_length(base_string, middle, check_even=False): | ||
function_offset = 0 | ||
if check_even: | ||
function_offset = 1 | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
distance_from_middle = 1 | ||
while distance_from_middle + middle + function_offset < len(base_string) and middle - distance_from_middle >= 0: | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if base_string[middle + function_offset + distance_from_middle] == base_string[middle - distance_from_middle]: | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
distance_from_middle += 1 | ||
else: | ||
break | ||
if not check_even: | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return max(_palindrome_length(base_string, middle, True), (distance_from_middle * 2) - 1 + function_offset) | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return (distance_from_middle * 2) - 1 + function_offset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from exceptions import BoardIntegrityError | ||
|
||
|
||
|
||
class BFS_search(): | ||
"""Implements BFS algorithm to find the shortest path in the maze. | ||
for more inforamtion visit: | ||
https://en.wikipedia.org/wiki/Breadth-first_search.""" | ||
|
||
def __init__(self, graph): | ||
self._graph = graph | ||
self._finish_junction = self._graph.get_finish_junction() | ||
self._start_junction = self._graph.get_start_junction() | ||
|
||
def best_route_in_positions(self): | ||
self._map_best_route() | ||
best_route = self._create_best_route() | ||
positions = self._graph.convert_junctions_to_positions(best_route) | ||
return positions | ||
|
||
def _map_best_route(self): | ||
"""adds all nodes connected to the current node to a queue. | ||
repeats the process for every node in the queue | ||
until it reaches the desired node (end of maze)""" | ||
sequence_number = 0 | ||
nodes = [self._start_junction] | ||
|
||
self._start_junction.set_discovered(sequence_number) | ||
|
||
while not len(nodes) == 0: | ||
current_junction = nodes[0] | ||
del nodes[0] | ||
|
||
neighbours = current_junction.get_connections() | ||
for neighbour in neighbours: | ||
if neighbour.is_finish(): | ||
neighbour.set_discovered(sequence_number) | ||
return | ||
|
||
if not neighbour.is_discovered(): | ||
nodes.append(neighbour) | ||
sequence_number += 1 | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
neighbour.set_discovered(sequence_number) | ||
|
||
|
||
# if the algorithm checked all nodes without finding the finish node | ||
# it means there is no solution | ||
raise BoardIntegrityError("no solution!") | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def _create_best_route(self): | ||
best_route = [] | ||
current_junction = self._finish_junction | ||
|
||
best_route.append(current_junction) | ||
|
||
while not current_junction.is_start(): | ||
next_junction = self._get_smallest_sequence_number_neighbour(current_junction) | ||
best_route.append(next_junction) | ||
current_junction = next_junction | ||
|
||
return best_route | ||
|
||
def _get_smallest_sequence_number_neighbour(self, current_junction): | ||
visited_junctions = filter(lambda connection: connection.is_discovered(), current_junction.get_connections()) | ||
return min(visited_junctions, key=lambda junction: junction.get_sequence_number()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Cell(): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""Represents a square in the maze that can be a wall or a path.""" | ||
def __init__(self): | ||
self._blocked = False | ||
self._mark = None | ||
|
||
def __str__(self): | ||
if self._blocked: | ||
return 'X' | ||
return self._mark or ' ' | ||
|
||
def set_mark(self, _mark): | ||
self._mark = _mark | ||
|
||
def set_blocked(self): | ||
self._blocked = True | ||
|
||
def is_blocked(self): | ||
return self._blocked |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
class BoardIntegrityError(Exception): | ||
"""An exception to indicate an illegal board""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from junction import Junction | ||
from position import Position | ||
|
||
|
||
class Graph(): | ||
"""Stores the junction structure in graph form | ||
that a search algorithm can process.""" | ||
def __init__(self, start, finish, maze): | ||
self._start = start | ||
self._finish = finish | ||
self._maze = maze | ||
self._graph = [] | ||
|
||
self._create_graph() | ||
self._update_junctions() | ||
|
||
self._set_start() | ||
self._set_finish() | ||
|
||
def _create_graph(self): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for row in range(self._maze.get_rows()): | ||
self._graph.append([]) | ||
for column in range(self._maze.get_columns()): | ||
if self._maze.get_board()[row][column].is_blocked(): | ||
self._graph[row].append(None) | ||
else: | ||
current_junction = Junction(Position(row, column)) | ||
self._graph[row].append(current_junction) | ||
|
||
def _update_junctions(self): | ||
junctions = filter(None, [node for row in self._graph for node in row]) | ||
for junction in junctions: | ||
position = junction.get_position() | ||
|
||
# by only connecting two at a time, no duplicate calls to | ||
# get_junction_relative_to_position are made | ||
upper_junction = self._get_junction_relative_to_position(position, -1, 0) | ||
left_junction = self._get_junction_relative_to_position(position, 0, -1) | ||
|
||
if upper_junction is not None: | ||
junction.add_connection(upper_junction) | ||
upper_junction.add_connection(junction) | ||
|
||
if left_junction is not None: | ||
junction.add_connection(left_junction) | ||
left_junction.add_connection(junction) | ||
|
||
def _get_junction_relative_to_position(self, position, row_offset=0, column_offset=0): | ||
try: | ||
return self._graph[position.get_row()+row_offset][position.get_column()+column_offset] | ||
except IndexError: | ||
return None | ||
|
||
|
||
def __str__(self): | ||
display = "" | ||
for row in self._graph: | ||
for node in row: | ||
display += str(node) if node else ' ' | ||
display += '\n' | ||
return display | ||
|
||
def _set_start(self): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
designated_start = self.get_start_junction() | ||
designated_start.set_is_start() | ||
|
||
def _set_finish(self): | ||
designated_finish = self.get_finish_junction() | ||
designated_finish.set_is_finish() | ||
|
||
def get_finish_junction(self): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return self._get_junction_relative_to_position(self._finish) | ||
|
||
def get_start_junction(self): | ||
return self._get_junction_relative_to_position(self._start) | ||
|
||
def convert_junctions_to_positions(self, junctions): | ||
return [junction.get_position() for junction in junctions] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from position import Position | ||
|
||
class Junction(): | ||
"""Represents a path square in the maze. | ||
connected to all the path-squares that are adjacent to it.""" | ||
def __init__(self, position): | ||
self._connections = [] | ||
self._position = position | ||
self._discovered = False | ||
self._finish = False | ||
self._start = False | ||
self._sequence_number = None | ||
|
||
def __str__(self): | ||
if self._finish: | ||
return ' f ' | ||
if self._start: | ||
return ' s ' | ||
return ' ' + str(self._sequence_number) + ' ' | ||
|
||
def get_position(self): | ||
galjs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return Position(self._position.get_row(), self._position.get_column()) | ||
|
||
def get_connections(self): | ||
return self._connections | ||
|
||
def get_sequence_number(self): | ||
return self._sequence_number | ||
|
||
def set_postion(self, position): | ||
self._position.set_row(position.get_row()) | ||
self._position.set_column(position.get_column()) | ||
|
||
def set_discovered(self, sequence_number): | ||
self._discovered = True | ||
self._sequence_number = sequence_number | ||
|
||
def set_is_finish(self): | ||
self._finish = True | ||
|
||
def set_is_start(self): | ||
self._start = True | ||
|
||
def has_connections(self): | ||
return len(self._connections) > 0 | ||
|
||
def add_connection(self, connection): | ||
self._connections.append(connection) | ||
|
||
def is_start(self): | ||
return self._start | ||
|
||
def is_finish(self): | ||
return self._finish | ||
|
||
def is_discovered(self): | ||
return self._discovered |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.