Skip to content
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

a star algorithm #292

Open
wants to merge 1 commit 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
Empty file added graphs_trees/a_star/__init__.py
Empty file.
319 changes: 319 additions & 0 deletions graphs_trees/a_star/astar_challenge.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,319 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook was prepared by [Author](https://github.com/). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Challenge Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Problem: Implement A* algorithm\n",
"\n",
"* [Constraints](#Constraints)\n",
"* [Test Cases](#Test-Cases)\n",
"* [Algorithm](#Algorithm)\n",
"* [Code](#Code)\n",
"* [Unit Test](#Unit-Test)\n",
"* [Solution Notebook](#Solution-Notebook)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Constraints\n",
"\n",
"* Can we assume we have a Node Class?\n",
" * No\n",
"* Can we assume the inputs are valid?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test Cases\n",
"\n",
"<table>\n",
"<tr>\n",
"<td>board</td>\n",
"<td>start</td>\n",
"<td>end</td>\n",
"<td>cost</td>\n",
"<td>output</td>\n",
"</tr>\n",
"<tr>\n",
"<td>[]</td>\n",
"<td>[0, 0]</td>\n",
"<td>[3, 5]</td>\n",
"<td>1</td>\n",
"<td>None</td>\n",
"</tr>\n",
"<tr>\n",
"<td><pre>[[0, 1, 0, 0, 0, 0],\n",
"[0, 0, 0, 0, 0, 1],\n",
"[0, 1, 0, 1, 0, 0],\n",
"[0, 1, 0, 0, 1, 0],\n",
"[0, 0, 0, 0, 1, 0]]</pre></td>\n",
"<td>[0, 0]</td>\n",
"<td>[3, 5]</td>\n",
"<td>1</td>\n",
"<td><pre>[[0, -1, -1, -1, -1, -1],\n",
"[1, 2, 3, 4, 5, -1],\n",
"[-1, -1, -1, -1, 6, 7],\n",
"[-1, -1, -1, -1, -1, 8],\n",
"[-1, -1, -1, -1, -1, -1]]</pre></td>\n",
"</tr>\n",
" <tr>\n",
"<td><pre>[[0, 1, 0, 0, 0, 0],\n",
"[0, 0, 0, 0, 0, 1],\n",
"[0, 1, 0, 1, 0, 0],\n",
"[0, 1, 0, 0, 1, 0],\n",
"[0, 0, 0, 0, 1, 0]]</pre></td>\n",
"<td>[0, 5]</td>\n",
"<td>[4, 0]</td>\n",
"<td>1</td>\n",
"<td><pre>[[-1, -1, -1, -1, 1, 0],\n",
"[-1, -1, 4, 3, 2, -1],\n",
"[-1, -1, 5, -1, -1, -1],\n",
"[-1, -1, 6, -1, -1, -1],\n",
"[ 9, 8, 7, -1, -1, -1]]</pre></td>\n",
"</tr>\n",
"<tr>\n",
"<td><pre>[[0, 1, 0, 0, 0, 0],\n",
"[0, 0, 0, 0, 0, 1],\n",
"[0, 1, 0, 1, 0, 0],\n",
"[0, 1, 0, 0, 1, 0],\n",
"[0, 0, 0, 0, 1, 0]]</pre></td>\n",
"<td>[0, 1]</td>\n",
"<td>[3, 5]</td>\n",
"<td>1</td>\n",
"<td>None</td>\n",
"</tr>\n",
"<tr>\n",
"<td><pre>[[0, 1, 0, 0, 0, 0],\n",
"[0, 0, 0, 0, 0, 1],\n",
"[0, 1, 0, 1, 0, 0],\n",
"[0, 1, 0, 0, 1, 0],\n",
"[0, 0, 0, 0, 1, 0]]</pre></td>\n",
"<td>[0, 0]</td>\n",
"<td>[2, 3]</td>\n",
"<td>1</td>\n",
"<td>None</td>\n",
"</tr>\n",
"</table>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Algorithm\n",
"\n",
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import List\n",
"class Astar(object):\n",
" def search(self, board, cost: int, start: List[int], end: List[int]) -> List[List[int]]:\n",
" # TODO: Implement me\n",
" pass "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Unit Test"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# %load test_astar.py\n",
"\n",
"from unittest import TestCase\n",
"\n",
"\n",
"class TestAstar(TestCase):\n",
" def test_astar_with_empty_board(self):\n",
" board = []\n",
" start = [0, 1]\n",
" end = [3, 5]\n",
" cost = 1 # cost is 1 per movement\n",
"\n",
" a_start = Astar()\n",
" self.assertEqual(a_start.search(board=board, cost=cost, start=start, end=end), None)\n",
" print('Success: test_astar_with_empty_board')\n",
"\n",
" def test_astar_with_valid_board(self):\n",
" board = [\n",
" [0, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 1],\n",
" [0, 1, 0, 1, 0, 0],\n",
" [0, 1, 0, 0, 1, 0],\n",
" [0, 0, 0, 0, 1, 0],\n",
" ]\n",
" start = [0, 0]\n",
" end = [3, 5]\n",
" cost = 1 # cost is 1 per movement\n",
" expected = [\n",
" [0, -1, -1, -1, -1, -1],\n",
" [1, 2, 3, 4, 5, -1],\n",
" [-1, -1, -1, -1, 6, 7],\n",
" [-1, -1, -1, -1, -1, 8],\n",
" [-1, -1, -1, -1, -1, -1]\n",
" ]\n",
"\n",
" a_start = Astar()\n",
" self.assertEqual(a_start.search(board=board, cost=cost, start=start, end=end), expected)\n",
" print('Success: test_astar_with_valid_board')\n",
"\n",
" def test_astar_with_another_valid_board(self):\n",
" board = [\n",
" [0, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 1],\n",
" [0, 1, 0, 1, 0, 0],\n",
" [0, 1, 0, 0, 1, 0],\n",
" [0, 0, 0, 0, 1, 0],\n",
" ]\n",
" start = [0, 5]\n",
" end = [4, 0]\n",
" cost = 1 # cost is 1 per movement\n",
" expected = [\n",
" [-1, -1, -1, -1, 1, 0],\n",
" [-1, -1, 4, 3, 2, -1],\n",
" [-1, -1, 5, -1, -1, -1],\n",
" [-1, -1, 6, -1, -1, -1],\n",
" [9, 8, 7, -1, -1, -1]\n",
" ]\n",
"\n",
" a_start = Astar()\n",
" self.assertEqual(a_start.search(board=board, cost=cost, start=start, end=end), expected)\n",
" print('Success: test_astar_with_another_valid_board')\n",
"\n",
"\n",
" def test_astar_start_from_danger_zone(self):\n",
" board = [\n",
" [0, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 1],\n",
" [0, 1, 0, 1, 0, 0],\n",
" [0, 1, 0, 0, 1, 0],\n",
" [0, 0, 0, 0, 1, 0],\n",
" ]\n",
" start = [0, 1]\n",
" end = [3, 5]\n",
" cost = 1 # cost is 1 per movement\n",
"\n",
" a_start = Astar()\n",
" self.assertEqual(a_start.search(board=board, cost=cost, start=start, end=end), None)\n",
" print('Success: test_astar_start_from_danger_zone')\n",
"\n",
"\n",
" def test_astar_with_danger_zone_as_end(self):\n",
" board = [\n",
" [0, 1, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0, 1],\n",
" [0, 1, 0, 1, 0, 0],\n",
" [0, 1, 0, 0, 1, 0],\n",
" [0, 0, 0, 0, 1, 0],\n",
" ]\n",
" start = [0, 0]\n",
" end = [2, 3]\n",
" cost = 1\n",
" expected = None\n",
"\n",
" a_start = Astar()\n",
" self.assertEqual(a_start.search(board=board, cost=cost, start=start, end=end), expected)\n",
" print('Success: test_astar_with_danger_zone_as_end')\n",
"\n",
"\n",
"def main():\n",
" test = TestAstar()\n",
" test.test_astar_with_empty_board()\n",
" test.test_astar_with_valid_board()\n",
" test.test_astar_with_another_valid_board()\n",
" test.test_astar_start_from_danger_zone()\n",
" test.test_astar_with_danger_zone_as_end()\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Solution Notebook\n",
"\n",
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Loading