Skip to content

Instantly share code, notes, and snippets.

@JoaoFelipe
Last active October 28, 2016 02:44
Show Gist options
  • Select an option

  • Save JoaoFelipe/a4ef9c916b175c3d88de896bf1389521 to your computer and use it in GitHub Desktop.

Select an option

Save JoaoFelipe/a4ef9c916b175c3d88de896bf1389521 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"chess_to_tuple = lambda pos: (ord(pos[0]) - ord('a') + 1, int(pos[1]))\n",
"tuple_to_chess = lambda col, lin: chr(col + ord('a') - 1) + str(lin) if 1 <= col <= 8 and 1 <= lin <= 8 else None"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"knight_moves = lambda pos: (lambda tup: filter(\n",
" lambda y: y is not None,\n",
" map(\n",
" lambda x: tuple_to_chess(x[0] + tup[0], x[1] + tup[1]),\n",
" [(1, -2), (-1, 2), (-2, -1), (1, 2), \n",
" (2, -1), (-2, 1), (-1, -2), (2, 1)]\n",
" )\n",
"))(chess_to_tuple(pos))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from collections import deque\n",
"def knight(source, target):\n",
" visited = {source: 0}\n",
" fifo = deque([source])\n",
" if source == target:\n",
" return 0\n",
" while fifo:\n",
" element = fifo.popleft()\n",
" for pos in knight_moves(element):\n",
" if not pos in visited:\n",
" visited[pos] = visited[element] + 1\n",
" fifo.append(pos)\n",
" if pos == target:\n",
" return visited[pos]\n",
"\n",
" \n",
"knight(\"h8\", \"a1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"knight = lambda source, target: (\n",
" lambda itself: lambda target, visited, fifo: itself(target, visited, fifo, itself))(\n",
" lambda target, visited, fifo, itself: (\n",
" itself(target, *(lambda element, *rest: (lambda filtered: (\n",
" {**visited, **{pos: visited[element] + 1\n",
" for pos in filtered}},\n",
" list(rest) + filtered if target not in filtered else [target]\n",
" ))(\n",
" [x for x in knight_moves(element) if x not in visited]\n",
" ))(*fifo), itself) if fifo else visited[target]\n",
" )\n",
")(target, {source: 0}, [source])\n",
" \n",
"\n",
"knight(\"h8\", \"a1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"\n",
"knight = (lambda source, target: (\n",
" lambda itself: lambda target, visited, fifo: itself(target, visited, fifo, itself))(\n",
" lambda target, visited, fifo, itself: (\n",
" (lambda chess_to_tuple, tuple_to_chess: (\n",
" (lambda knight_moves: (\n",
" itself(target, *(lambda element, *rest: (lambda filtered: (\n",
" {**visited, **{pos: visited[element] + 1\n",
" for pos in filtered}},\n",
" list(rest) + filtered if target not in filtered else [target]\n",
" ))(\n",
" [x for x in knight_moves(element) if x not in visited]\n",
" ))(*fifo), itself) if fifo else visited[target]\n",
" ))(\n",
" lambda pos: (lambda tup: filter(\n",
" lambda y: y is not None,\n",
" map(\n",
" lambda x: tuple_to_chess(x[0] + tup[0], x[1] + tup[1]),\n",
" [(1, -2), (-1, 2), (-2, -1), (1, 2), \n",
" (2, -1), (-2, 1), (-1, -2), (2, 1)]\n",
" )\n",
" ))(chess_to_tuple(pos))\n",
" )\n",
" ))(\n",
" lambda pos: (ord(pos[0]) - ord('a') + 1, int(pos[1])),\n",
" lambda col, lin: chr(col + ord('a') - 1) + str(lin) if 1 <= col <= 8 and 1 <= lin <= 8 else None\n",
" )\n",
" )\n",
")(target, {source: 0}, [source]))\n",
" \n",
"\n",
"knight(\"h8\", \"a1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"\n",
"knight = (\n",
" lambda source, target: (\n",
" lambda itself: lambda target, visited, fifo: itself(target, visited, fifo, itself))(\n",
" lambda target, visited, fifo, itself: (\n",
" (lambda chess_to_tuple, tuple_to_chess: (\n",
" (lambda knight_moves: (\n",
" itself(target, *(lambda element, *rest: (lambda filtered: (\n",
" {**visited, **{pos: visited[element] + 1\n",
" for pos in filtered}},\n",
" list(rest) + filtered if target not in filtered else [target]\n",
" ))(\n",
" [x for x in knight_moves(element) if x not in visited]\n",
" ))(*fifo), itself) if fifo else visited[target]\n",
" ))(\n",
" lambda pos: (lambda tup: filter(\n",
" lambda y: y is not None,\n",
" map(\n",
" lambda x: tuple_to_chess(x[0] + tup[0], x[1] + tup[1]),\n",
" [(1, -2), (-1, 2), (-2, -1), (1, 2), \n",
" (2, -1), (-2, 1), (-1, -2), (2, 1)]\n",
" )\n",
" ))(chess_to_tuple(pos))\n",
" )\n",
" ))(\n",
" lambda pos: (ord\n",
" (pos[0]) - ord ('a') + 1, \n",
" int(pos[1])), lambda \n",
" col, lin: chr(col + \n",
" ord('a') - 1) + str(lin) \n",
" if 1 <= col <= 8 and 1 <= lin <= 8 else None)))(target, {source: 0}, [source]))\n",
" \n",
"\n",
"knight(\"h8\", \"a1\")"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"\n",
"knight = (\n",
"\n",
" lambda \n",
" source, target: (\n",
" lambda itself:\n",
" lambda target, \n",
" visited, fifo: \n",
" itself (target,\n",
" visited, fifo, itself))(\n",
" lambda target ,visited, fifo, itself: (\n",
" (lambda chess_to_tuple, tuple_to_chess: (\n",
" (lambda knight_moves: (\n",
" itself( target, \n",
" *(lambda element, \n",
" *rest: (lambda filtered: (\n",
" {** visited, **{pos: \n",
" visited [element] \n",
" + 1 for pos in filtered\n",
" }}, list (rest) + \n",
" filtered if target \n",
" not in filtered else [\n",
" target]))([x for x in \n",
" knight_moves(element) if x not in visited]\n",
" ))(*fifo), itself) if fifo \n",
" else visited\n",
" [target]))( lambda\n",
" pos: (lambda \n",
" tup: filter\n",
" (lambda y: y \n",
" is not None,\n",
" map( lambda x: \n",
" tuple_to_chess (x[0] + tup[0], x[1] + tup[1]), [(1, -2), (-1, 2), (-2, -1), \n",
" (1, 2), (2, -1), \n",
" (-2, 1), (-1, -2), \n",
" (2, 1)]))) (chess_to_tuple\n",
" (pos))) ))(lambda pos: \n",
" (ord (pos[0])\n",
" - ord('a') + 1, int\n",
" (pos[1])), lambda col, \n",
" lin: chr(col +\n",
" ord('a') - 1) + str\n",
" (lin) if 1 <= col <= 8 and 1 <= lin <= 8 else None)))(target, {source: 0}, [source]))\n",
" \n",
"\n",
"knight(\"h8\", \"a1\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment