Created
August 9, 2013 07:03
-
-
Save astynax/6191689 to your computer and use it in GitHub Desktop.
"Спираль" на Python
This file contains 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
{ | |
"metadata": { | |
"name": "spiral" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### \u0417\u0430\u0434\u0430\u0447\u0430 \"\u0421\u043f\u0438\u0440\u0430\u043b\u044c\"\n", | |
"\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0443\u0447\u0438 \u0432\u044b\u0437\u0432\u0430\u043d\u043d\u043e\u0439 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438\n", | |
"\n", | |
" 3, 3, \"123456789\"\n", | |
"\n", | |
"\u0432\u044b\u0432\u0435\u0434\u0435\u0442\n", | |
"\n", | |
" 123\n", | |
" 894\n", | |
" 765\n", | |
"\n", | |
"\u0442.\u0435. \u043c\u0430\u0442\u0440\u0438\u0446\u0443 ```3 x 3``` \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u0443\u044e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c\u0438 \u0441\u0442\u0440\u043e\u043a\u0438 ```\"123456789\"```, \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u043c\u0438 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0449\u0435\u0439\u0441\u044f \u043f\u043e \u0447\u0430\u0441\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0435\u043b\u043a\u0435 \u0441\u043f\u0438\u0440\u0430\u043b\u0438 \u0441 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0432 (0, 0).\n", | |
"\n", | |
"\u041b\u0438\u0448\u043d\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0441\u0442\u0440\u043e\u043a\u0438 (\u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043a\u0443\u0434\u0430 \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c) \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0442\u0431\u0440\u043e\u0448\u0435\u043d\u044b, \u0430 \u044f\u0447\u0435\u0439\u043a\u0438, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e, \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c ```\".\"```\n", | |
"\n", | |
"\u0415\u0449\u0451 \u043f\u0440\u0438\u043c\u0435\u0440\u044b:\n", | |
"\n", | |
"```6, 2, \"Hello,World!\"```\n", | |
"\n", | |
" Hello,\n", | |
" !dlroW\n", | |
"\n", | |
"```2, 2, \"ABCDE\"```\n", | |
"\n", | |
" AB\n", | |
" DC\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import itertools as it\n", | |
"import more_itertools as mit" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def lengths(w, h):\n", | |
" if w > 0:\n", | |
" yield w\n", | |
" for i in lengths(h - 1, w):\n", | |
" yield i" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"list(lengths(3, 3))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 3, | |
"text": [ | |
"[3, 2, 2, 1, 1]" | |
] | |
} | |
], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def moves(w, h):\n", | |
" return it.chain(*it.imap(\n", | |
" it.repeat,\n", | |
" it.cycle([(1, 0), (0, 1), (-1, 0), (0, -1)]),\n", | |
" lengths(w, h)\n", | |
" ))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"list(moves(3, 3))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 5, | |
"text": [ | |
"[(1, 0), (1, 0), (1, 0), (0, 1), (0, 1), (-1, 0), (-1, 0), (0, -1), (1, 0)]" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def drop(n, iterable):\n", | |
" iterable = iter(iterable)\n", | |
" for _ in xrange(n):\n", | |
" iterable.next()\n", | |
" return iterable" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def folder((acc, x, y), ((dx, dy), i)):\n", | |
" return acc + [((x + dx, y + dy), i)], x + dx, y + dy\n", | |
"\n", | |
"def spiral(w, h, data):\n", | |
" return dict(reduce(folder, it.izip(moves(w, h), data), ([], -1, 0))[0])\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"spiral(2, 2, \"123456789\")" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "pyout", | |
"prompt_number": 8, | |
"text": [ | |
"{(0, 0): '1', (0, 1): '4', (1, 0): '2', (1, 1): '3'}" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def render(w, h, data, default=\".\"):\n", | |
" return \"\\n\".join(\n", | |
" ''.join(data.get((x, y), default) for x in xrange(w))\n", | |
" for y in xrange(h)\n", | |
" ) " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print render(4, 4, spiral(4, 4, \"0123456789abcdef\"))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"0123\n", | |
"bcd4\n", | |
"afe5\n", | |
"9876\n" | |
] | |
} | |
], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print render(4, 4, spiral(4, 4, \">>>>vvv<<<^^>>v<\"))" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
">>>>\n", | |
"^>>v\n", | |
"^<vv\n", | |
"<<<v\n" | |
] | |
} | |
], | |
"prompt_number": 11 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment